linux(一、常用基本命令总结)

102 阅读26分钟

centos 下载地址

1.网易镜像:mirrors.163.com/centos/7/is…

1. Linux文件与目录结构

image.png

# /bin
是Binary的缩写,这个目录存放着最经常使用的命令

# /sbin
s就是Super User的意思,这里存放的是系统管理员使用的系统管理程序

# /home
存放普通用户的主目录,在linux中每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的。

# /root
该目录为系统管理员,也称作超级权限者的用户主目录

# /lib
系统开机所需要最基本的动态连接共享库,其作用类似于Windows里的DLL文件。几乎所有的应用程序都需要用到这些共享库

# /lost + found
这个目录一般情况下是空的,当系统非法关机后,这里就存放了一些文件

# /etc
所有的系统管理所需要的配置文件和子目录

# /usr
这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下,类似于windows下的program files目录

# /boot
这里存放的是启动linux时使用的一些核心文件,包括一些连接文件以及镜像文件,自己的安装别放到这里

# /proc
这个目录是一个虚拟的目录,它是系统内存的映射,我们可以通过直接访问这个目录来获取系统信息。

# /srv
service的缩写,该目录存放一些服务启动之后需要提取的数据

# /sys
这是linux2.6内核的一个很大的变化.该目录下安装了2.6内核中新出现的一个文件系统sysfs。

# /tmp
这个目录是用来存放一些临时文件的

# /dev
类似于windows的设备管理器,把所有的硬件用文件的形式存储。

# /media(centos6)
linux系统会自动识别一些设备,例如U盘、光驱等等,当时别后,linux会把识别的设备挂载到这个目录下。
Centos7迁移到/run/media

# /mnt
系统提供该目录是为了让用户临时挂载别的文件系统的,我们可以将外部的存储挂载在/mnt/上,然后进入该目录就可以查看里面的内容了。

# /opt
这是给主机额外安装软件所摆放的目录。比如你安装一个mysql数据库则就可以放到这个目录下。默认是空的。

# /var
这个目录中存放着不断扩充着的东西,我们习惯将那些经常被修改的目录放在这个目录下。包括各种日志文件。

2. VI/VIM编辑器

2.1 概念

VI 是 Unix 操作系统和类 Unix 操作系统中最通用的文本编辑器。

VIM 编辑器是从 VI 发展出来的一个性能更强大的文本编辑器。可以主动的以字体颜色辨别语法的正确性,方便程序设计。VIM 与 VI 编辑器完全兼容。

2.2 模式

2.2.1 一般模式

以 vi 打开一个档案就直接进入一般模式了(这是默认的模式)。在这个模式中,你可以使用『上下左右』按键来移动光标,你可以使用『删除字符』或『删除整行』来处理档案内容, 也可以使用『复制、粘贴』来处理你的文件数据。常用语法:

复制/删除:

语法功能描述语法功能描述
复制一个词yw删除一个词dw
复制光标当前一行yy删除光标当前行dd
复制一段(从第几行到第几行)y 数字 y删除光标(含)后多少行d 数字 d

移动/剪切/撤销:

语法功能描述语法功能描述
移动到行头shift + 6(^)剪切一个字母,相当于 delx
移动到行尾shift + 4($)剪切一个字母,相当于 BackspaceX
移动到页头,数字1 + shift + g撤销上一步u
移动到页尾shift + g
移动到目标行数字 + shift + g
箭头移动到目的行粘贴p

2.2.2 编辑模式

在一般模式中可以进行删除、复制、粘贴等的动作,但是却无法编辑文件内容的!要等到你按下『i, I, o, O, a,A』等任何一个字母之后才会进入编辑模式。

注意了!通常在Linux中,按下这些按键时,在画面的左下方会出现『INSERT或REPLACE』的字样,此时才可以进行编辑。

而如果要回到一般模式时, 则必须要按下『Esc』这个按键即可退出编辑模式。

进入编辑模式:

案件功能
i当前光标前
a当前光标后
o当前光标行的下一行
I光标所在行最前
A光标所在行最后
O当前光标行的上一行

退出编辑模式:

按『Esc』键 退出编辑模式,之后所在的模式为一般模式。

2.2.3 指令模式

在一般模式当中,输入『: / ?』3个中的任何一个按钮,就可以将光标移动到最底下那一行。

在这个模式当中, 可以提供你『搜寻资料』的动作,而读取、存盘、大量取代字符、离开 vi 、显示行号等动作是在此模式中达成的!

命令功能
保存:w
退出:q
强制执行:!
强制保存退出:wq!
n 查找下一个,N 往上查找/要查找的词
取消高亮显示:noh
显示行号:set nu
关闭行号:set nonu
替换内容 /g 替换匹配到的所有内容:%s/old/new/g

2.2.4 模式间转换

image.png

3. 网络配置

3.1 网络IP和网关

3.1.1 ifconfig

显示所有网络接口的配置信息

3.1.2 ping

测试当前服务器是否可以连接目的主机

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=128 time=97.9 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=128 time=24.7 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 24.779/61.351/97.924/36.573 ms

3.1.3 ifcfg-ens33

修改ip配置文件(修改之后要注意使用service network restart命令重启网络)

其中比较重要的几处:

(1) BOOTPROTO="static" 意为静态分配IP 。除此以外还有none | static | bootp | dhcp (2) ONBOOT=yes 。 系统启动的时候网络接口是否有效(yes/no) (3) IPADDR=192.168.198.100 。 IP地址 (4) GATEWAY=192.168.198.2 。 网关 (5) DNS1=8.8.8.8 。 域名解析器

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO="static"
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=d5e6a5ba-3210-44d1-9bc8-61e7198dbd86
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.198.100
GATEWAY=192.168.198.2
DNS1=8.8.8.8
DNS2=8.8.4.4
PREFIX=16

# 重启网络
[root@localhost ~]# service network restart

3.1.4 常见问题

(1) 物理机能 ping 通虚拟机,但是虚拟机 ping 不通物理机,一般都是因为物理机的防火墙问题,把防火墙关闭就行

(2) 虚拟机能 Ping 通物理机,但是虚拟机 Ping 不通外网,一般都是因为 DNS 的设置有问题

(3) 虚拟机 Ping www.baidu.com 显示域名未知等信息,一般查看 GATEWAY 和 DNS 设置是否正确

(4) 如果以上全部设置完还是不行,需要关闭 NetworkManager 服务。

systemctl stop NetworkManager 关闭 、 systemctl disable NetworkManager 禁用

(5) 如果检查发现 systemctl status network 有问题 需要检查 ifcfg-ens33

3.2 配置主机名

3.2.1 修改主机名称

语法:hostname

位置:/etc/hostname

# 查看主机名
[root@localhost ~]# hostname
localhost.localdomain

# 修改主机名 (修改完成后需要重启才可生效)
[root@localhost ~]# vi /etc/hostname

3.2.2 修改hosts映射文件

3.2.2.1 修改linux的主机映射文件(hosts文件)

作用:配置多台虚拟机通常会采用主机名的方式配置,不用刻意记IP地址。

修改之后需要重启服务器。

位置:/etc/hosts

[root@test etc]# cat /etc/hosts
192.168.198.200 test200
192.168.198.201 test201

3.2.2.2 修改windows7的主机映射文件(hosts文件)

(1) 进入 C:\Windows\System32\drivers\etc 路径

(2) 打开 hosts 文件并添加如下内容

192.168.198.200 test200
192.168.198.201 test201

3.2.2.3 修改 window10 的主机映射文件(hosts 文件)

(1) 进入 C:\Windows\System32\drivers\etc 路径

(2) 拷贝 hosts 文件到桌面

(3) 打开桌面 hosts 文件并添加如下内容

(4) 将桌面 hosts 文件覆盖 C:\Windows\System32\drivers\etc 路径 hosts 文件

3.2.3 常见远程登陆软件

(1) MobaXterm (2) Xshell (3) SecureCRT (4) FinalShell等

4. 系统管理 (centos7)

4.1 systemctl

4.1.1 启动/停止/重启/状态

systemctl start/stop/restart/status 服务名 服务名后缀(.service)可选择性携带

# 启动
[root@test etc]# systemctl start firewalld.service
# 停止
[root@test etc]# systemctl stop firewalld.service
# 重启
[root@test etc]# systemctl restart firewalld.service
# 状态
[root@test etc]# systemctl status firewalld.service

4.1.2 查看所有服务名称

位置:/usr/lib/systemd/system

[root@test etc]# cd /usr/lib/systemd/system
[root@test system]# ls -al

4.1.3 后台服务自启/自停配置

systemctl enable/disable 服务名

systemctl list-unit-files 查看服务开机启动状态

# 开机自启
[root@test ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.

# 开机禁止
[root@test ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

# 查看服务开机启动状态
[root@test ~]# systemctl list-unit-files
UNIT FILE                                     STATE
......
proc-sys-fs-binfmt_misc.automount             static
firewalld.service                             disabled
sshd.service                                  enable
......

4.1.4 运行级别

查看当前运行级别:systemctl get-default

修改当前运行级别:systemctl set-default TARGET.target (这里TARGET取multi-user或者graphical)

[root@test ~]# systemctl get-default
graphical.target

[root@test ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

[root@test ~]# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

linux系统有7种运行级别(常见的是3、5)

(1) 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动

(2) 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登录

(3) 运行级别2:多用户状态(没有NFS),不支持网络

(4) 运行级别3:完全的多用户状态(有NFS),登录后进入控制台命令行模式

(5) 运行级别4:系统未使用,保留

(6) 运行级别5:X11控制台,登陆后进入图形GUI模式

(7) 运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

CentOS7的运行级别简化为:

(1) multi-user.target 等价于原运行级别3(多用户有网,无图形界面)

(2) graphical.target 等价于原运行级别5(多用户有网,有图形界面)

4.2 关机重启命令

语法:

命令描述
sync将数据由内存同步到硬盘中
halt停机,关闭系统,但不断电
poweroff关机,断电
reboot重启,相当于shutdown -r now

shutdown 选项 时间

选项功能参数功能
-H相当于--halt,停机now立刻关机
-r-r=reboot 重启时间等待多久后关机(时间单位是分钟)

Linux 系统中为了提高磁盘的读写效率,对磁盘采取了 “预读迟写”操作方式。

当用户保存文件时,Linux 核心并不一定立即将保存数据写入物理磁盘中,而是将数据保存在缓冲区中,等缓冲区满时再写入磁盘,这种方式可以极大的提高磁盘写入数据的效率。

但是,也带来了安全隐患,如果数据还未写入磁盘时,系统掉电或者其他严重问题出现,则将导致数据丢失。使用 sync 指令可以立即将缓冲区的数据写入磁盘。

4.2.1 将数据由内存同步到硬盘中

[root@test ~]# sync

4.2.2 重启

[root@test ~]# reboot

4.2.3 停机(不断电)

[root@test ~]# halt

4.2.4 N分钟后关机

计算机将在 1 分钟后关机,并且会显示在登录用户的当前屏幕中

[root@test ~]# shutdown -h 1 'This server will shutdown after 1mins'

4.2.5 立即关机

等同于poweroff

[root@test ~]# shutdown -h now

4.2.6 系统立即重启

等同于reboot

[root@test ~]# shutdown -r now

5. 常用基本命令

5.1 帮助命令

5.1.1 man获得帮助信息

语法:man 命令或配置文件

功能描述:获得帮助信息

信息功能
NAME命令的名称和单行描述
SYNOPSIS怎样使用命令
DESCRIPTION命令功能的深入讨论
EXAMPLES怎样使用命令的例子
SEE ALSO相关主题(通常是手册页)
[root@test ~]# man ls

5.1.2 help获得shell内置命令的帮助信息

一部分基础功能的系统命令是直接内嵌在 shell 中的,系统加载启动之后会随着 shell 一起加载,常驻系统内存中。这部分命令被称为“内置(built-in)命令”;相应的其它命令被称为“外部命令”。

语法:help 命令

功能描述:获得shell内置命令的帮助信息

[root@test ~]# help cd

5.1.3 常用快捷键

常用快捷键功能
ctrl + c停止进程
ctrl + l清屏,等同于clear,彻底清屏是:reset
善用tab键提示(更重要的是可以防止敲错)
上下键查找执行过的命令

5.2 文件目录类

5.2.1 pwd 显示当前工作目录的绝对路径

[root@test ~]# pwd
/root

5.2.2 ls 列出目录的内容

选项功能
-a全部的文件,连同隐藏栏(开头为.的文件)一起列出来(常用)
-l长数据串列出,包含文件的属性与权限等等数据:(常用)等价于ll

每行列出的信息依次是: 文件类型与权限 链接数 文件属主 文件属组 文件大小用byte来表示 建立或最近修改的时间 名字

[root@test /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  test  usr
boot  etc  lib   media  opt  root  sbin  sys  tmp   var

[root@test /]# ls -a
.   bin   dev  home  lib64  mnt  proc  run   srv  test  usr
..  boot  etc  lib   media  opt  root  sbin  sys  tmp   var

[root@test /]# ll 等价于 ll -a
total 28
lrwxrwxrwx.   1 root root    7 Jul 19 03:03 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jul 19 03:14 boot
drwxr-xr-x.  19 root root 3280 Dec  1 17:10 dev
drwxr-xr-x. 144 root root 8192 Dec  1 17:17 etc
drwxr-xr-x.   3 root root   16 Jul 19 03:12 home
lrwxrwxrwx.   1 root root    7 Jul 19 03:03 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Jul 19 03:03 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Apr 10  2018 media
drwxr-xr-x.   2 root root    6 Apr 10  2018 mnt
drwxr-xr-x.   3 root root   16 Jul 19 03:07 opt
dr-xr-xr-x. 198 root root    0 Dec  1 17:10 proc
dr-xr-x---.  14 root root 4096 Dec  1 17:13 root
drwxr-xr-x.  43 root root 1260 Dec  1 17:10 run
lrwxrwxrwx.   1 root root    8 Jul 19 03:03 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Apr 10  2018 srv
dr-xr-xr-x.  13 root root    0 Dec  1 17:10 sys
drwxr-xr-x.   5 root root   41 Dec  1 18:49 test
drwxrwxrwt.  16 root root 4096 Dec  1 18:49 tmp
drwxr-xr-x.  13 root root  155 Jul 19 03:03 usr
drwxr-xr-x.  21 root root 4096 Jul 19 03:13 var

[root@test /]# ll -ls
total 28
 0 lrwxrwxrwx.   1 root root    7 Jul 19 03:03 bin -> usr/bin
 4 dr-xr-xr-x.   5 root root 4096 Jul 19 03:14 boot
 0 drwxr-xr-x.  19 root root 3280 Dec  1 17:10 dev
12 drwxr-xr-x. 144 root root 8192 Dec  1 17:17 etc
 0 drwxr-xr-x.   3 root root   16 Jul 19 03:12 home
 0 lrwxrwxrwx.   1 root root    7 Jul 19 03:03 lib -> usr/lib
 0 lrwxrwxrwx.   1 root root    9 Jul 19 03:03 lib64 -> usr/lib64
 0 drwxr-xr-x.   2 root root    6 Apr 10  2018 media
 0 drwxr-xr-x.   2 root root    6 Apr 10  2018 mnt
 0 drwxr-xr-x.   3 root root   16 Jul 19 03:07 opt
 0 dr-xr-xr-x. 200 root root    0 Dec  1 17:10 proc
 4 dr-xr-x---.  14 root root 4096 Dec  1 17:13 root
 0 drwxr-xr-x.  43 root root 1260 Dec  1 17:10 run
 0 lrwxrwxrwx.   1 root root    8 Jul 19 03:03 sbin -> usr/sbin
 0 drwxr-xr-x.   2 root root    6 Apr 10  2018 srv
 0 dr-xr-xr-x.  13 root root    0 Dec  1 17:10 sys
 0 drwxr-xr-x.   5 root root   41 Dec  1 18:49 test
 4 drwxrwxrwt.  16 root root 4096 Dec  1 18:49 tmp
 0 drwxr-xr-x.  13 root root  155 Jul 19 03:03 usr
 4 drwxr-xr-x.  21 root root 4096 Jul 19 03:13 var

5.2.3 cd 切换目录

cd 参数

参数功能
cd绝对路径切换路径
cd相对路径切换路径
cd~或cd回到自己的家目录
cd -回到上一次所在目录
cd ..回到当前目录的上一级目录
cd -P跳转到实际物理路径,而非快捷方式路径

5.2.4 mkdir创建一个新的目录

mkdir 选项 要创建的目录

选项功能
-p创建多层目录
[root@test test]# rm -rf a.txt/ b.txt/ c/
[root@test test]# ls
[root@test test]# mkdir test1
[root@test test]# mkdir test1/test2
[root@test test]# mkdir test2/test2
mkdir: cannot create directory ‘test2/test2’: No such file or directory
[root@test test]# mkdir -p test2/test2
[root@test test]# 

5.2.5 rmdir 删除一个空的目录

rmdir 要删除的空目录

[root@test test]# rmdir test1
rmdir: failed to remove ‘test1’: Directory not empty
[root@test test]# mkdir test3
[root@test test]# rmdir test3
[root@test test]#

5.2.6 touch 创建空文件

touch 文件名称

[root@test test]# touch a.txt
[root@test test]# touch test2/test2/b.txt

5.2.7 cp 复制文件或目录

cp 选项 source dest 复制source文件到dest

强制覆盖不提示的方法: \cp

选项功能
-r递归复制整个文件夹
参数功能
source源文件
dest目标文件
[root@test test]# cp a.txt b.txt

[root@test test]# cp /test/a.txt c.txt

[root@test test]# cp test1 /test/test3
cp: omitting directory ‘test1’

[root@test test]# cp -r test1 /test/test3

5.2.8 rm 删除文件或目录

rm 选项 deleteFile :递归删除目录中所有内容

选项功能
-r递归删除目录中所有内容
-f强制执行删除操作,而不提示用于进行确认
-v显示指令的详细执行过程
[root@test test]# rm c.txt
rm: remove regular empty file ‘c.txt’? y

[root@test test]# rm test4/
rm: cannot remove ‘test4/’: Is a directory
[root@test test]# rm test4/a.txt
rm: remove regular empty file ‘test4/a.txt’? y

[root@test test]# rm -r a.txt
rm: remove regular empty file ‘a.txt’? y

[root@test test]# rm -rf b.txt

[root@test test]# rm -rf test1

[root@test test]# rm -r test2
rm: descend into directory ‘test2’? y
rm: descend into directory ‘test2/test2’? y
rm: remove regular empty file ‘test2/test2/b.txt’? y
rm: remove directory ‘test2/test2’? y
rm: descend into directory ‘test2/test1’? y
rm: remove directory ‘test2/test1/test2’? y
rm: remove directory ‘test2/test1’? y
rm: remove directory ‘test2’? y

[root@test test]# rm -rfv test3
removed directory: ‘test3/test2’
removed directory: ‘test3’

5.2.9 mv 移动文件与目录或重命名

重命名: mv oldNameFile newNameFile

移动文件: mv /temp/movefile /targetFolder

[root@test test]# mv a.txt b.txt
[root@test test]# mv test1/ /test1/test2

5.2.10 cat 查看文件内容

查看文件内容,从第一行开始显示。一般用来查看比较小的文件,一屏幕能展示全的。

cat 选项 要查看的文件

选项功能描述
-n显示所有行的行号,包括空行
[root@test test]# cat b.txt
a
b
c
[root@test test]# cat -n b.txt
     1  a
     2  b
     3  c

5.2.11 more 文件内容分屏查看器

more指令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more指令中内置了若干快捷键。

操作功能说明
空白键(space)代表向下翻一页
Enter代表向下翻一行
q代表立刻离开more,不再显示该文件内容
Ctrl + F向下滚动一屏
Ctrl + B返回上一屏
=输出当前行的行号
:f输出文件名和当前行的行号
[root@test test]# more /etc/profile

5.2.12 less 分屏显示文件内容

语法:less要查看的文件

less 指令用来分屏查看文件内容,它的功能与 more 指令类似,但是比 more 指令更加强大,支持各种显示终端。

less 指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。

使用SecureCRT时 pagedown和pageup可能会出现无法识别的问题

操作功能说明
空白键向下翻1页
pagedown向下翻1页
pageup向上翻1页
/字串向下搜 字符串 :n向下查 N向上查
?字串向上搜 字符串 :n向上查 N向下查
q离开less
[root@test test]# less /etc/profile

5.2.13 echo 输出内容到控制台

echo 选项 输出内容

选项 : 【-e : 支持反斜线控制的字符转换】

控制字符作用
\输出\本身
\n换行符
\t制表符,也就是tab键
[root@test test]# echo "hello\world"
hello\world
[root@test test]# echo -e "hello\world"
hello\world
[root@test test]# echo -e "hello\tworld"
hello   world
[root@test test]# echo -e "hello\nworld"
hello
world
[root@test test]# echo -e "hello\\\world"
hello\world

5.2.14 head显示文件头部内容

head 用于显示文件的开头部分内容,默认情况下 head 指令显示文件的前 10 行内容。

语法:

查看文件头10行内容: head 文件

查看文件头N行内容: head -n 5 文件

选项功能
-n<行数>指定显示头部内容的行数
[root@test test]# head /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

[root@test test]# head -n 5 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

[root@test test]#

5.2.15 tail 输出文件尾部内容

tail 用于输出文件中尾部的内容,默认情况下 tail 指令显示文件的后 10 行内容。

语法:

查看文件尾部10行内容 : tail 文件

查看文件尾部s行内容 : tail -n s 文件

实时追踪该文档的所有更新 : tail -f 文件

选项功能
-n<行数>输出文件尾部n行内容
-f显示文件最新追加的内容,监视文件变化
[root@test test]# tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

[root@test test]# tail -n 5 /etc/profile
    fi
done

unset i
unset -f pathmunge

[root@test test]# tail -f /etc/profile
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

5.2.16 > 输出重定向和 >> 追加

语法:

命令功能描述
ls -l > 文件列表的内容写入文件a.txt中(覆盖写)
ls -al >> 文件列表的内容追加到文件的末尾
cat 文件 l > 文件2将文件1的内容覆盖到文件2
echo "内容">>文件将内容追加到文件中

重点就是 : 一个符号 '>' 代表覆盖写 、 两个符号 '>>' 代表追加写

5.2.17 ln 软链接

软链接也称为符号链接,类似于 windows 里的快捷方式,有自己的数据块,主要存放了链接其他文件的路径。

语法:

命令描述
ln -s 原文件或目录 软链接名给原文件创建一个软链接
rm -rf 软链接名删除软链接

这里注意:删除软链接不是 rm -rf 软链接名/ 。即不带最后的符号 '/' 。

如果使用 rm -rf 软链接名/ 删除,会把软链接对应的真实目录下内容删掉

查询:通过 ll 就可以查看,列表属性第1位是l,尾部会有位置指向。

# 创建软链接
[root@hadoop101 ~]# mv houge.txt xiyou/dssz/
[root@hadoop101 ~]# ln -s xiyou/dssz/houge.txt ./houzi
[root@hadoop101 ~]# ll
lrwxrwxrwx. 1 root root 20 617 12:56 houzi -> xiyou/dssz/houge.txt

# 删除软链接 注意不要写后面呢/
[root@hadoop101 ~]# rm -rf houzi

# 进入软链接实际物理路径
[root@hadoop101 ~]# ln -s xiyou/dssz/ ./dssz
[root@hadoop101 ~]# cd -P dssz/

5.2.18 history 查看已经执行过历史命令

[root@test test]# history

5.3 时间日期类

语法 : data option ... + format

选项功能
-d<时间字符串>显示指定的时间字符串表示的时间,而非当前时间
-s<日期时间>设置系统日期时间

参数 :

参数功能
<+日期时间格式>指定显示时使用的日期时间格式

5.3.1 date显示当前时间

语法 :

命令功能
date显示当前时间
date +%Y显示当前年份
date +%m显示当前月份
date +%d显示当前是哪一天
date +%Y-%m-%d%H:%M:%S显示年月日时分秒
[root@test ~]# date
Sun Dec  4 16:38:41 PST 2022
[root@test ~]# date +%Y
2022
[root@test ~]# date +%Y%m
202212
[root@test ~]# date +%Y%m%d
20221204
[root@test ~]# date +%Y-%m-%d%H:%M:%S
2022-12-0416:39:49
# 这里有空格会报错
[root@test ~]# date +%Y-%m-%d %H:%M:%S
date: extra operand ‘%H:%M:%S’
Try 'date --help' for more information.
[root@test ~]#

5.3.2 date显示非当前时间

语法 :

命令功能
date -d '1 days ago'显示前一天时间
date -d '-1 days ago'显示明天时间
[root@test ~]# date -d '1 days ago'
Sat Dec  3 16:43:00 PST 2022
[root@test ~]# date -d '-1 days ago'
Mon Dec  5 16:43:07 PST 2022
[root@test ~]# date -d '-1 days ago' +%Y-%m-%d%H:%M:%S
2022-12-0516:44:15

5.3.3 date设置系统时间

语法 :date -s 字符串时间 (设置系统当前时间)

[root@test ~]# date -s "2022-12-5 8:46:00"
Mon Dec  5 08:46:00 PST 2022
[root@test ~]# date -s "2022-12-5"
Mon Dec  5 00:00:00 PST 2022
[root@test ~]# date -s "8:46:00"
Mon Dec  5 08:46:00 PST 2022

5.3.4 cal查看日历

语法 : cal 选项 (不加选项,显示本月日历)

选项功能
具体某一年显示这一年的日历
[root@test ~]# cal
    December 2022
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

[root@test ~]# cal 2017
                               2017

       January               February                 March
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7             1  2  3  4             1  2  3  4
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
15 16 17 18 19 20 21   12 13 14 15 16 17 18   12 13 14 15 16 17 18
22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
29 30 31               26 27 28               26 27 28 29 30 31

        April                   May                   June
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1       1  2  3  4  5  6                1  2  3
 2  3  4  5  6  7  8    7  8  9 10 11 12 13    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   14 15 16 17 18 19 20   11 12 13 14 15 16 17
16 17 18 19 20 21 22   21 22 23 24 25 26 27   18 19 20 21 22 23 24
23 24 25 26 27 28 29   28 29 30 31            25 26 27 28 29 30
30
        July                  August                September
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                   1  2
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    3  4  5  6  7  8  9
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   10 11 12 13 14 15 16
16 17 18 19 20 21 22   20 21 22 23 24 25 26   17 18 19 20 21 22 23
23 24 25 26 27 28 29   27 28 29 30 31         24 25 26 27 28 29 30
30 31
       October               November               December
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7             1  2  3  4                   1  2
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
                                              31

5.4 用户管理命令

5.4.1 /etc/passwd 查看创建了哪些用户

[root@test ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
...
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
...
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
...
tm:x:1000:1000:base-learn:/home/tm:/bin/bash

5.4.2 id 查看用户是否存在

语法 : id + 用户名

[root@test ~]# id tm
uid=1000(tm) gid=1000(tm) groups=1000(tm)
[root@test ~]# id ttt
id: ttt: no such user

5.4.3 su 切换用户

(su : switch user的缩写)

语法功能
su 用户名称切换用户,只能获得用户的执行权限,不能获得环境变量
su - 用户名称切换到用户并获得该用户的环境变量及执行权限
[root@test ~]# su tm
[tm@test root]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[tm@test root]$ exit
exit
[root@test ~]# su - tm
Last login: Mon Dec  5 08:57:53 PST 2022 on pts/0
[tm@test ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tm/.local/bin:/home/tm/bin
[tm@test ~]$ exit
logout

5.4.4 useradd 添加新用户

语法功能
useradd 用户名添加新用户
useradd -g 组名 用户名添加新用户到某个组
[root@test ~]# useradd tmtest
[root@test ~]# ll /home/
total 4
drwx------. 15 tm     tm     4096 Jul 19 03:28 tm
drwx------.  3 tmtest tmtest   78 Dec  5 09:00 tmtest
[root@test ~]# ls /home/
tm  tmtest
[root@test ~]# id tmtest
uid=1001(tmtest) gid=1001(tmtest) groups=1001(tmtest)
[root@test ~]# id tm
uid=1000(tm) gid=1000(tm) groups=1000(tm)
[root@test ~]# useradd -g 1000 tm1
[root@test ~]# id tm1
uid=1002(tm1) gid=1000(tm) groups=1000(tm)

5.4.5 passwd 设置用户密码

语法 : passwd 用户名 (设置用户密码)

# 为用户tm1设置密码
[root@test ~]# passwd tm1 
Changing password for user tm1.
New password: # 这里输入123456 下面提示少于8位
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: # 这里重复输入密码 输入12345678 与123456 不等 提示错误
Sorry, passwords do not match.
New password: # 重新输入密码并确认
Retype new password:
passwd: all authentication tokens updated successfully.

5.4.6 userdel 删除用户

语法功能
userdel 用户名删除用户但保存用户主目录
userdel -r 用户名用户和用户主目录,都删除
选项功能
-r删除用户的同时,删除与用户相关的所有文件
[root@test ~]# ll /home/
total 4
drwx------. 15 tm     tm     4096 Jul 19 03:28 tm
drwx------.  3 tm1    tm       78 Dec  5 09:01 tm1
drwx------.  3 tmtest tmtest   78 Dec  5 09:00 tmtest
[root@test ~]# userdel tmtest
[root@test ~]# ll /home/
total 4
drwx------. 15 tm   tm   4096 Jul 19 03:28 tm
drwx------.  3 tm1  tm     78 Dec  5 09:01 tm1
drwx------.  3 1001 1001   78 Dec  5 09:00 tmtest
[root@test ~]# userdel -r tm1
[root@test ~]# ll /home/
total 4
drwx------. 15 tm   tm   4096 Jul 19 03:28 tm
drwx------.  3 1001 1001   78 Dec  5 09:00 tmtest

5.4.7 who 查看登录用户信息

语法功能
whoami显示自身用户名称
who am i显示登录用户的用户名以及登陆时间
[root@test ~]# whoami
root
[root@test ~]# who am i
root     pts/0        2022-12-04 16:38 (192.168.198.1)

5.4.8 sudo 设置普通用户具有root权限

# 切换到tm用户
[root@test ~]# su tm
# 用tm普通用户创建一个文件夹module
[tm@test root]$ sudo mkdir module
[sudo] password for tm:
tm is not in the sudoers file.  This incident will be reported. # 这里提示报错

# 切回root用户
[tm@test root]$ su root
Password:
# 修改sudoers文件
[root@test ~]# vim /etc/sudoers

# 两种修改
# 1.添加内容 tm     ALL=(ALL)   ALL
root   ALL=(ALL)   ALL
tm     ALL=(ALL)   ALL
# 2.添加内容 tm     ALL=(ALL)   NOPASSWD:ALL (即配置成 采用sudo命令时,不需要输入密码)
root   ALL=(ALL)   ALL
tm     ALL=(ALL)   NOPASSWD:ALL

# 重复上面操作,发现可以创建成功了
[root@test ~]# su tm
[tm@test root]$ sudo mkdir module
[sudo] password for tm:
[tm@test root]$ su root
Password:
[root@test ~]# cd module/

5.4.9 usermod 修改用户

语法 : usermod -g 用户组 用户名

选项功能
-g修改用户的初始登录组,给定的组必须存在。默认组 id 是 1
# 非root用户 权限不支持
[tm@test module]$ usermod -g root tm
usermod: Permission denied.
usermod: cannot lock /etc/passwd; try again later.
[tm@test module]$ su root
Password:
[root@test module]# ll /home/
total 4
drwx------. 15 tm   tm  4096 Dec  5 09:30 tm
drwx------.  3 1002 tm    78 Dec  5 09:01 tm1
drwx------.  3 tm1  tm1   78 Dec  5 09:00 tmtest
[root@test module]# usermod -g root tm
[root@test module]# ll /home/
total 4
drwx------. 15 tm   root 4096 Dec  5 09:30 tm
drwx------.  3 1002 tm     78 Dec  5 09:01 tm1
drwx------.  3 tm1  tm1    78 Dec  5 09:00 tmtest

5.5 用户组管理命令

5.5.1 /etc/group 查看创建了哪些组

[root@test module]# cat /etc/group
root:x:0:
bin:x:1:
...
disk:x:6:
mail:x:12:postfix
video:x:39:
ftp:x:50:
nobody:x:99:
sshd:x:74:
...
tm:x:1000:
tm1:x:1001:

5.5.2 groupadd 新增组

语法 : groupadd 组名

[root@test module]# groupadd tmgroup
[root@test module]# cat /etc/group
root:x:0:
...
tm:x:1000:
tm1:x:1001:
tmgroup:x:1002:

5.5.3 groupdel 删除组

语法 : groupdel 组名

[root@test module]# groupdel tmgroup

5.5.4 groupmod 修改组

语法 : groupmod -n 新组名 老组名

[root@test module]# groupmod -n tmnewgroup tmgroup
[root@test module]# cat /etc/group
root:x:0:
...
tm:x:1000:
tm1:x:1001:
tmnewgroup:x:1002:

5.6 搜索查找类

5.6.1 find 查找文件或者目录

find 指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件显示在终端。

语法 : find 搜索范围 选项

选项功能
-name<查询方式>按照指定的文件名查找模式查找文件
-user<用户名>查找属于指定用户名所有文件
-size<文件大小>按照指定的文件大小查找文件,单位为:b->块(512 字节)c->字节 w->字(2 字节) k->千字节 M->兆字节 G->吉字节
# 准备
[root@test ~]# touch a.txt
[root@test ~]# touch b.txt
[root@test ~]# mkdir txtFile
[root@test ~]# cd txtFile/
[root@test txtFile]# touch c.txt
[root@test txtFile]# touch a.txt

# 按文件名查
[root@test txtFile]# find /root -name "*.txt"
/root/a.txt
/root/b.txt
/root/txtFile/c.txt
/root/txtFile/a.txt
[root@test txtFile]# find /root/txtFile/ -name "*.txt"
/root/txtFile/c.txt
/root/txtFile/a.txt

# 按拥有者查
[root@test txtFile]# find /root/txtFile/ -user tm
[root@test txtFile]# find /root/txtFile/ -user root
/root/txtFile/
/root/txtFile/c.txt
/root/txtFile/a.txt

# 按文件大小查 +大于  -小于  不加->等于
# 查home目录下 文件size 大于 200M的文件
[root@test txtFile]# find /home -size +204800
# 查home目录下 文件size 小于 200M的文件
[root@test txtFile]# find /home -size -204800
# 查home目录下 文件size 等于 200M的文件
[root@test txtFile]# find /home -size 204800
# 查home目录下 文件size 大于 20G的文件
[root@test txtFile]# find /home -size +10G

5.6.2 locate快速定位文件路径

locate 指令利用事先建立的系统中所有文件名称及路径的 locate 数据库实现快速定位给定的文件。Locate 指令无需遍历整个文件系统,查询速度较快。为了保证查询结果的准确度,管理员必须定期更新 locate 时刻。

由于 locate 指令基于数据库进行查询,所以第一次运行前,必须使用 updatedb 指令创建 locate 数据库。

语法 : locate 搜索文件

[root@test ~]# locate a*.txt
/root/a.txt
...
[root@test ~]# updatedb
[root@test ~]# locate a*.txt
/root/a.txt
/root/txtFile/a.txt
...

5.6.3 grep 过滤查找及 "|" 管道符

管道符,"|",表示将前一个命令的处理结果输出传递给后面的命令处理。

语法 : grep 选项 查找内容 源文件

选项功能
-n显示匹配行及行号
# 查找某文件在第几行
[root@test ~]# ls
anaconda-ks.cfg  b.txt    Documents  module  original-ks.cfg  Public     txtFile
a.txt            Desktop  Downloads  Music   Pictures         Templates  Videos
[root@test ~]# ls | grep a
anaconda-ks.cfg
a.txt
Downloads
original-ks.cfg
Templates
[root@test ~]# ls | grep -n a
1:anaconda-ks.cfg
2:a.txt
6:Downloads
9:original-ks.cfg
12:Templates

# 查a.txt文件中内容包括1的位置
[root@test ~]# cat a.txt
1
2
3
4
11
12
[root@test ~]# cat a.txt | grep -n 1
1:1
5:11
6:12

5.7 压缩和解压缩类

5.7.1 gzip/gunzip 压缩

语法 :

标题
gzip 文件压缩文件,只能将文件压缩为*.gz文件
gunzip 文件.gz解压缩文件命令

注意 :

(1) 只能压缩文件不能压缩目录

(2) 不保留原来的文件

(3) 同时多个文件会产生多个压缩包

[root@test txtFile]# ll
-rw-r--r--. 1 root root 0 Dec  5 14:55 c.txt

[root@test txtFile]# gzip c.txt
[root@test txtFile]# ll
-rw-r--r--. 1 root root 26 Dec  5 14:55 c.txt.gz

[root@test txtFile]# gunzip c.txt.gz
[root@test txtFile]# ll
-rw-r--r--. 1 root root 0 Dec  5 14:55 c.txt

5.7.2 zip/unzip 压缩

语法 :

命令功能
zip 选项 xx.zip 将要压缩的内容压缩文件和目录的命令
unzip 选项 xx.zip解压缩文件

选项说明 :

zip选项功能
-r压缩目录
unzip选项功能
-d<目录>指定解压后文件的存放目录

zip压缩命令在windows和linux下都通用,可以压缩目录且保留源文件

压缩 :

# 压缩一个文件 压缩包名为zipName.zip
[root@test txtFile]# zip zipName1.zip c.txt

# 压缩多个文件 
[root@test txtFile]# zip zipName2.zip c.txt d.txt

# 压缩目录 不带 -r 选项 里面目录结构为 root文件夹下有一个txtFile文件夹,txtFile文件夹下是空的
[root@test ~]# zip zipName3.zip /root/txtFile/
  adding: root/txtFile/ (stored 0%)
[root@test ~]# ll
-rw-r--r--. 1 root root  176 Dec  5 15:07 zipName3.zip

# 压缩目录 带 -r 选项 里面目录结构为 root文件夹下有一个txtFile文件夹,txtFile文件夹下不是空的
[root@test ~]# zip -r zipName4.zip /root/txtFile/
updating: root/txtFile/ (stored 0%)
  adding: root/txtFile/d.txt (stored 0%)
  adding: root/txtFile/e.txt (stored 0%)
  adding: root/txtFile/f.txt (stored 0%)
  adding: root/txtFile/c.txt (stored 0%)
  adding: root/txtFile/zipName.zip (stored 0%)
  adding: root/txtFile/zipName1.zip (stored 0%)

解压 :

# 解压单个文件
[root@test module]# unzip zipName1.zip
Archive:  zipName1.zip
 extracting: c.txt

# 解压多个文件
[root@test module]# unzip zipName2.zip
Archive:  zipName1.zip
replace c.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: c.txt
 extracting: d.txt

# 解压目录 不带-d
[root@test module]# unzip zipName3.zip
Archive:  zipName3.zip
   creating: root/txtFile/
[root@test module]# ls
root  zipName3.zip  zipName4.zip

# 解压目录 带-d 即指定解压目录
[root@test module]# unzip zipName3.zip -d /opt
Archive:  zipName3.zip
   creating: /opt/root/txtFile/
[root@test module]# cd /opt/root/txtFile/
[root@test txtFile]# ls
[root@test txtFile]# 

5.7.3 tar 打包

语法 :tar 选项 xx.tar.gz 将要打包进去的内容

功能 :打包目录,压缩后的文件格式.tar.gz

选项功能
-c产生.tar打包文件
-v显示详细信息
-f指定压缩后的文件名
-z打包同时压缩
-x解包.tar文件
-C解压到指定目录
# 压缩单个/多个文件
[root@test txtFile]# tar -zcvf tarName1.tar.gz c.txt d.txt
c.txt
d.txt

# 压缩目录
[root@test txtFile]# tar -zcvf tarName2.tar.gz /root/txtFile/
tar: Removing leading `/' from member names
/root/txtFile/
/root/txtFile/d.txt
/root/txtFile/e.txt
/root/txtFile/f.txt
/root/txtFile/c.txt
/root/txtFile/zipName.zip
/root/txtFile/zipName1.zip
/root/txtFile/tarName1.tar.gz
/root/txtFile/tarName2.tar.gz
tar: /root/txtFile: file changed as we read it

# 解压到当前目录
[root@test txtFile]# tar -zxvf tarName2.tar.gz
root/txtFile/
root/txtFile/d.txt
root/txtFile/e.txt
root/txtFile/f.txt
root/txtFile/c.txt
root/txtFile/zipName.zip
root/txtFile/zipName1.zip
root/txtFile/tarName1.tar.gz
root/txtFile/tarName2.tar.gz
[root@test txtFile]# ls
c.txt  d.txt  e.txt  f.txt  root  tarName1.tar.gz  tarName2.tar.gz  zipName1.zip  zipName.zip

# 解压到指定目录
[root@test txtFile]# tar -zxvf tarName2.tar.gz -C /opt
root/txtFile/
root/txtFile/d.txt
root/txtFile/e.txt
root/txtFile/f.txt
root/txtFile/c.txt
root/txtFile/zipName.zip
root/txtFile/zipName1.zip
root/txtFile/tarName1.tar.gz
root/txtFile/tarName2.tar.gz

5.8 文件权限类

5.8.1 文件属性

Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限。为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定。在Linux中我们可以使用ll或者ls -l命令来显示一个文件的属性以及文件所属的用户和组。

从左到右10个字符表示:

image.png

文件类型属主权限属组权限其他用户权限
01 2 34 5 67 8 9
dr w xr - xr - x
目录文件读 写 执行读 写 执行读 写 执行

5.8.1.1 0-9 解释

从左到右用0-9这些数字来表示:

(1) 0首位表示类型 linux中第一个字符代表这个文件是目录、文件或链接文件等

  • 代表 文件 、 d 代表 目录 、 l 链接文档 (link file)

(2) 1-3位确定属主(该文件的所有者) 拥有该文件的权限 。 --User

(3) 4-6位确定属组(所有者的同组用户) 拥有该文件的权限。 --Group

(4) 7-9位确定其他用户拥有该文件的权限 --Other

5.8.1.2 rwx 解释

rwx作用文件和目录有着不同的解释

(1) 作用到文件

  • r代表可读(read) :可以读取、查看。

  • w代表可写(write) : 可以修改,但是不代表可以删除该文件,删除一个文件的前提是对该文件所在目录有写权限,才能删除该文件。

  • x代表可执行(execute) :可以被系统执行。

(2) 作用到目录

  • r代表可读(read) :可以读取,ls查看目录内容

  • w代表可写(write) : 可以修改,目录内创建 + 删除 + 重命名目录

  • x代表可执行(execute) :可以进入该目录

[root@localhost ~]# ll
drwxr-xr-x. 2 root root    6 Jul 19 03:28 Desktop

# 含义解释 从左往右

1. d -> 文件类型与权限 -> 代表这是个目录
2. rwx -> 文件属主权限 -> 代表该文件的所有者拥有读写和执行的权限
3. r-x -> 文件属组权限 -> 代表这个文件的所有者的同组用户拥有读和执行的权限
4. r-x -> 其他用户权限 -> 代表其他用户拥有该文件读和执行的权限
5. 2 -> 代表链接数
6. root -> 文件属主
7. root -> 文件属组
8. 6 -> 文件大小
9. Jul 19 03:28 -> 建立或最近修改的时间
10. Desktop -> 文件的名字

如果查看到是文件:链接数指的是硬链接个数。

如果查看的是文件夹:链接数指的是子文件夹个数。

5.8.2 chmod 改变权限

image.png

第一种方式变更权限 : chmod [{ugoa}{+-=}{rwx}] 文件或目录

第二种方式变更权限 : chmod [mode=421 ] [文件或目录]

u:所有者 g:所有组 o:其他人 a:所有人(u、g、o的总和)

r = 4 、 w = 2 、 x = 1 、 rwx = 4+2+1 = 7

# 准备
[root@localhost ~]# mkdir /test
[root@localhost ~]# cd /test/
[root@localhost test]# touch a.txt
[root@localhost test]# ll
-rw-r--r--. 1 root root 0 Dec  5 22:37 a.txt

# 修改文件使其所属主用户具有执行权限
[root@localhost test]# chmod u+x a.txt
[root@localhost test]# ll
-rwxr--r--. 1 root root 0 Dec  5 22:37 a.txt

# 修改文件使其所属组用户具有执行权限
[root@localhost test]# chmod g+x a.txt
[root@localhost test]# ll
-rwxr-xr--. 1 root root 0 Dec  5 22:37 a.txt

# 修改文件所属主用户执行权限,并使其他用户具有执行权限
[root@localhost test]# chmod u-x,o+x a.txt
[root@localhost test]# ll
-rw-r-xr-x. 1 root root 0 Dec  5 22:37 a.txt

# 采用数字的方式,设置文件所有者、所属组、其他用户都具有可读可写可执行权限。
[root@localhost test]# chmod 777 a.txt
[root@localhost test]# ll
-rwxrwxrwx. 1 root root 0 Dec  5 22:37 a.txt

# 修改整个文件夹里面的所有文件的所有者、所属组、其他用户都具有可读可写可执行权限
[root@localhost test]# chmod -R 777 /test/
[root@localhost test]# ll
-rwxrwxrwx. 1 root root 0 Dec  5 22:37 a.txt

5.8.3 chown 改变所有者

语法 : chown [选项] [最终用户] [文件或目录] -> 改变文件或者目录的所有者

选项功能
-R递归操作
# 修改文件所有者
[root@localhost ~]# ll
-rwxrwxrwx. 1 root   root    0 Dec  5 22:34 a.txt
[root@localhost ~]# chown tm a.txt
[root@localhost ~]# ll
-rwxrwxrwx. 1 tm   root    0 Dec  5 22:34 a.txt

# 递归改变文件所有者和所有组
[root@localhost ~]# ll
drwxr-xr-x. 2 root root    6 Dec  5 22:50 test
[root@localhost ~]# chown -R tm:tm test/
[root@localhost ~]# ll
drwxr-xr-x. 2 tm   tm      6 Dec  5 22:50 test

5.8.4 chgrp 改变所属组

语法 : chgrp [最终用户组] [文件或目录] -> 改变文件或者目录的所属组

[root@localhost ~]# ll
-rwxrwxrwx. 1 tm   root    0 Dec  5 22:34 a.txt

# 修改文件的所属组
[root@localhost ~]# chgrp tm a.txt
[root@localhost ~]# ls -al
-rwxrwxrwx.  1 tm   tm    0 Dec  5 22:34 a.txt

5.9 磁盘查看和分区类

5.9.1 du 查看文件和目录占用的磁盘空间

du : disk usage 磁盘占用情况

语法 : du 目录/文件 -> 显示目录下每个子目录的磁盘使用情况

选项功能
-h以人们较易阅读的 GBytes, MBytes, KBytes 等格式自行显示;
-a不仅查看子目录大小,还要包括文件
-c显示所有的文件和子目录大小后,显示总和
-s只显示总和
--max-depth=n指定统计子目录的深度为第n层
# 查看当前用户主目录占用的磁盘空间大小
[root@localhost ~]# du -s
4316    .
[root@localhost ~]# du -sh
4.3M    .

5.9.2 df 查看磁盘空间使用情况

df : disk free 空余磁盘

语法 : df 选项 -> 列出文件系统的整体磁盘使用量,检查文件系统的磁盘空间占用情况

选项功能
-h以人们较易阅读的 GBytes, MBytes, KBytes 等格式自行显示;
# 查看磁盘使用情况
[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        895M     0  895M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M   11M  900M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda3        18G  4.4G   14G  25% /
/dev/sda1       297M  163M  135M  55% /boot
tmpfs           182M   36K  182M   1% /run/user/0

5.9.3 df 查看设备挂载情况

语法 : lsblk -> 查看设备挂载情况

选项功能
-f查看详细的设备挂载情况,显示文件系统信息
[root@localhost ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
├─sda1   8:1    0  300M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 17.7G  0 part /
sr0     11:0    1 1024M  0 rom

5.9.4 fdisk 分区

语法 :

语法功能
fdisk -l查看磁盘分区详情
fdisk 硬盘设备名对新增硬盘进行分区操作

选项 :

选项功能
-l显示所有硬盘的分区列表

注意 : 该命令必须在root用户下才能使用

1)Linux 分区
        Device:分区序列
        Boot:引导
        Start:从X磁柱开始
        End:到Y磁柱结束
        Blocks:容量
        Id:分区类型ID
        System:分区类型
(2)分区操作按键说明
        m:显示命令列表
        p:显示当前磁盘分区
        n:新增分区
        w:写入分区信息并退出
        q:不保存分区信息直接退出
# 查看系统分区情况
[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b5665

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    41943039    18566144   83  Linux

5.9.5 mount/umount 挂载/卸载

对于Linux用户来讲,不论有几个分区,分别分给哪一个目录使用,它总归就是一个根目录、一个独立且唯一的文件结构。

Linux中每个分区都是用来组成整个文件系统的一部分,它在用一种叫做“挂载”的处理方法,它整个文件系统中包含了一整套的文件和目录,并将一个分区和一个目录联系起来,要载入的那个分区将使它的存储空间在这个目录下获得。

挂在前准备 : 必须要有光盘或者已经连接镜像文件。

image.png

语法功能
mount[-tvfstype][-ooptions]devicedir挂载设备
umount设备文件名或挂载点卸载设备

参数

-t vfstype : 指定文件系统的类型,通常不必指定。mount会自动选择正确的类型。常用类型有:

(1)光盘或光盘镜像:iso9660

(2)DOSfat16文件系统:msdos

(3)Windows9xfat32文件系统:vfat

(4)WindowsNTntfs文件系统:ntfs

(5)MountWindows文件网络共享:smbfs

(6)UNIX(LINUX)文件网络共享:nfs

-o options : 主要用来描述设备或档案的挂接方式。常用的参数有:

(1)loop:用来把一个文件当成硬盘分区挂接上系统

(2)ro:采用只读方式挂接设备

(3)rw:采用读写方式挂接设备

(4)iocharset:指定访问文件系统所用字符集

device : 要挂载(mount)的设备

dir : 设备在系统上的挂载点(mount point)

# 挂载光盘镜像文件

[root@hadoop101 ~]# mkdir /mnt/cdrom/
# 建立挂载点 设备 /dev/cdrom 挂载到 挂载点 : /mnt/cdrom 中
[root@hadoop101 ~]# mount -t iso9660 /dev/cdrom /mnt/cdrom/
[root@hadoop101 ~]# ll /mnt/cdrom/
--------------------------------------------------------------
# 卸载光盘镜像文件
[root@hadoop101 ~]# umount /mnt/cdrom
--------------------------------------------------------------
# 设置开机自动挂载
[root@hadoop101 ~]# vi /etc/fstab
# 添加红框中内容,保存退出

image.png

5.10 进程管理类

进程是正在执行的一个程序或命令,每一个进程都是一个运行的实体,都有自己的地址空间,并占用一定的系统资源

5.10.1 ps 查看当前系统进程状态

ps : process status 进程状态

语法:

  • ps aux | grep xxx -> 查看系统中所有进程

  • ps -ef | grep xxx -> 可以查看子父进程之间的关系

选项功能
a列出带有终端的所有用户的进程
x列出当前用户的所有进程,包括没有终端的进程
u面向用户友好的显示风格
-e列出所有进程
-u列出某个用户关联的所有进程
-f显示完整格式的进程列表

功能说明 :

ps aux 显示信息说明

标题描述
USER该进程是由哪个用户产生的
PID进程的 ID 号
%CPU该进程占用 CPU 资源的百分比,占用越高,进程越耗费资源
%MEM该进程占用物理内存的百分比,占用越高,进程越耗费资源
VSZ该进程占用虚拟内存的大小,单位 KB
RSS该进程占用实际物理内存的大小,单位 KB
TTY该进程是在哪个终端中运行的。对于 CentOS 来说,tty1 是图形化终端
tty2-tty6是本地的字符界面终端。pts/0-255 代表虚拟终端
STAT进程状态。常见的状态有:R:运行状态、S:睡眠状态、T:暂停状态、Z:僵尸状态、s:包含子进程、l:多线程、+:前台显示
START该进程的启动时间
TIME该进程占用 CPU 的运算时间,注意不是系统时间
COMMAND产生此进程的命令名

ps -ef 显示信息说明

标题描述
UID用户 ID
PID进程 ID
PPID父进程 ID
CCPU 用于计算执行优先级的因子。数值越大,表明进程是 CPU 密集型运算,执行优先级会降低;数值越小,表明进程是 I/O 密集型运算,执行优先级会提高
STIME进程启动的时间
TTY完整的终端名称
TIMECPU 时间
CMD启动进程所用的命令和参数

如果想查看进程的 CPU 占用率和内存占用率,可以使用 aux 。 如果想查看进程的父进程 ID 可以使用 ef 。

[root@localhost ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3 193960  7100 ?        Ss   Dec05   0:03 /usr/lib/systemd/systemd --switched-root
root          2  0.0  0.0      0     0 ?        S    Dec05   0:00 [kthreadd]

[root@localhost ~]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Dec05 ?        00:00:03 /usr/lib/systemd/systemd --switched-root --system --deser
root          2      0  0 Dec05 ?        00:00:00 [kthreadd]

5.10.2 kill 终止进程

语法功能
kill 选项 进程号通过进程号杀死进程
killall 进程名称通过进程名称杀死进程,也支持通配符,这在系统因负载过大而变得很慢时很有用
选项功能
-9表示强迫进程立即停止
# 杀死浏览器进程
[root@localhost ~]# kill -9 5102

# 通过进程名杀死进程
[root@localhost ~]# killall firefox

5.10.3 pstree 查看进程树

语法 : pstree 选项

选项功能
-p显示进程的PID
-u显示进程的所属用户
# 显示进程pid
[root@localhost ~]# pstree -p

# 显示进程所属用户
[root@localhost ~]# pstree -u

5.10.4 top 实时监控系统进程状态

基本命令 : top 选项

选项功能
-d秒数指定 top 命令每隔几秒更新。默认是 3 秒在 top 命令的交互模式当中可以执行的命令
-i使 top 不显示任何闲置或者僵死进程
-p通过指定监控进程 ID 来仅仅监控某个进程的状态
操作功能
P以 CPU 使用率排序,默认就是此项
M以内存的使用率排序
N以PID排序
q退出top

查询结果字段解释 :

第一行信息为任务队列信息

image.png

第二行为进程信息

image.png

第三行为cpu信息

image.png

第四行为物理内存信息

image.png

# ----------------------------------------------------------------------
[root@localhost ~]# top -d 1
top - 00:34:43 up  5:17,  3 users,  load average: 0.00, 0.01, 0.05
Tasks: 228 total,   1 running, 227 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1863040 total,   117944 free,   794816 used,   950280 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   877112 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
   650 root      20   0   90568   3184   2320 S   1.0  0.2   0:02.41 rngd
     1 root      20   0  193960   7100   4200 S   0.0  0.4   0:03.14 systemd
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.02 kthreadd
     4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
     6 root      20   0       0      0      0 S   0.0  0.0   0:00.35 ksoftirqd/0
     7 root      rt   0       0      0      0 S   0.0  0.0   0:00.06 migration/0
     8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
     9 root      20   0       0      0      0 S   0.0  0.0   0:02.22 rcu_sched
    10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-drain
    11 root      rt   0       0      0      0 S   0.0  0.0   0:00.14 watchdog/0
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.14 watchdog/1
    13 root      rt   0       0      0      0 S   0.0  0.0   0:00.09 migration/1
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.19 ksoftirqd/1
    16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H
    17 root      rt   0       0      0      0 S   0.0  0.0   0:00.17 watchdog/2
# ----------------------------------------------------------------------
[root@localhost ~]# top -i
top - 00:35:24 up  5:18,  3 users,  load average: 0.00, 0.01, 0.05
Tasks: 230 total,   1 running, 228 sleeping,   1 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1863040 total,   117276 free,   795476 used,   950288 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   876448 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
   319 root      20   0       0      0      0 S   0.3  0.0   0:02.86 xfsaild/sda3
 54431 root      20   0  162284   2384   1580 R   0.3  0.1   0:00.02 top
# ----------------------------------------------------------------------
[root@localhost ~]# top -p 2575
top - 00:36:42 up  5:19,  3 users,  load average: 0.00, 0.01, 0.05
Tasks:   0 total,   0 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1863040 total,   117152 free,   795600 used,   950288 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   876360 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND

执行上述命令后,可以按 P、M、N 对查询出的进程结果进行排序。

5.10.5 netstat 显示网络状态和端口占用信息

语法功能
netstat -anpgrep 进程号查看该进程网络信息
netstat –nlpgrep 端口号查看网络端口号占用情况

image.png

# 通过进程号查看sshd进程的网络信息
[root@localhost ~]# netstat -anp | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1104/sshd
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      3246/sshd: root@pts
tcp        0      0 192.168.198.100:22      192.168.198.1:60905     ESTABLISHED 3250/sshd: root@not
tcp        0      0 192.168.198.100:22      192.168.198.1:60902     ESTABLISHED 3246/sshd: root@pts
tcp6       0      0 :::22                   :::*                    LISTEN      1104/sshd
tcp6       0      0 ::1:6010                :::*                    LISTEN      3246/sshd: root@pts
unix  3      [ ]         STREAM     CONNECTED     26796    1104/sshd
unix  2      [ ]         DGRAM                    43089    3246/sshd: root@pts
unix  2      [ ]         DGRAM                    43120    3250/sshd: root@not

# 查看某端口号是否被占用
[root@localhost ~]# netstat -nltp | grep 22
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1474/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1104/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      1104/sshd

5.11 系统定时任务

重启crond服务 : systemctl restart crond.service

语法 : crontab 选项

image.png

参数说明 :

[root@localhost ~]# crontab -e

(1)进入 crontab 编辑界面。会打开 vim 编辑你的工作。

          • 执行的任务

image.png

(2)特殊符号

image.png

(3)特定时间执行命令

image.png

# 每隔 1 分钟,向/root/bailongma.txt 文件中添加一个 11 的数字
[root@localhost ~]# */1 * * * * /bin/echo ”11” >> /root/bailongma.txt

6. RPM

6.1 RPM概述

RPM(RedHat Package Manager),RedHat软件包管理工具,类似windows里面的setup.exe 是Linux这系列操作系统里面的打包安装工具,它虽然是RedHat的标志,但理念是通用的。 RPM包的名称格式。Apache-1.3.23-11.i386.rpm

  • “apache” 软件名称
  • “1.3.23-11”软件的版本号,主版本和此版本
  • “i386”是软件所运行的硬件平台,Intel 32位处理器的统称
  • “rpm”文件扩展名,代表RPM包

6.2 RPM查询(rpm -qa)

rpm -qa 查询所安装的所有rpm软件包

由于软件包比较多,一般都会采取过滤。rpm -qa | grep rpm软件包

[root@localhost ~]# rpm -qa | grep firefox
firefox-68.5.0-2.el7.centos.x86_64

6.3 RPM卸载(rpm -e)

语法功能
rpm -e RPM软件包卸载软件包
rpm -e --nodeps RPM软件包卸载软件时,不检查依赖。这样的话,那些使用该软件包的软件在此之后可能就不能正常工作了
[root@localhost ~]# rpm -e firefox

6.4 RPM安装命令(rpm -ivh)

rpm -ivh RPM包全名

选项功能
-iinstall,安装
-v--verbose,显示详细信息
-h--hash,进度条
--nodeps安装前不检查依赖
[root@localhost ~]# rpm -ivh firefox-45.0.1-1.el6.centos.x86_64.rpm
warning: firefox-45.0.1-1.el6.centos.x86_64.rpm: Header V3 RSA/SHA1
Signature, key ID c105b9de: NOKEY
Preparing... ###########################################
[100%]
1:firefox ###########################################
[100%]

7. YUM

YUM(全称为 Yellow dog Updater, Modified)是一个在 Fedora 和 RedHat 以及 CentOS中的 Shell 前端软件包管理器。

基于 RPM 包管理,能够从指定的服务器自动下载 RPM 包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。

YUM类似于我们java开发中的maven工具,额可以从镜像网站上下载应用程序,并直接安装。

7.1 YUM的常用命令

语法 : yum 选项 参数

选项功能
-y对所有提问都回答 yes

参数:

参数功能
install安装rpm软件包
update更新rpm软件包
check-update检查是否有可用的更新rpm软件包
remove删除指定的rpm软件包
list显示软件包信息
clean清理yum过期的缓存
deplist显示yum软件包的所有依赖关系
# 采用yum方式安装firefox
[root@localhost ~]# yum -y install firefox

7.2 修改网络YUM源

默认的系统 YUM 源,需要连接国外 apache 网站,网速比较慢,可以修改关联的网络YUM 源为国内镜像的网站,比如网易 163,aliyun 等。

# 1. 安装 wget, wget 用来从指定的 URL 下载文件
[root@localhost ~] yum install wget

# 2. 在/etc/yum.repos.d/目录下,备份默认的 repos 文件
[root@localhost yum.repos.d] pwd
/etc/yum.repos.d
[root@localhost yum.repos.d] cp CentOS-Base.repo CentOS-Base.repo.backup

# 3. 下载网易 163 或者是 aliyun 的 repos 文件,任选其一
[root@localhost yum.repos.d] wget http://mirrors.aliyun.com/repo/Centos-7.repo //阿里云
[root@localhost yum.repos.d] wget http://mirrors.163.com/.help/CentOS7-Base-163.repo //网易 163

# 4. 使用下载好的 repos 文件替换默认的 repos 文件 例如:用 CentOS7-Base-163.repo 替换 CentOS-Base.repo
[root@localhost yum.repos.d]# mv CentOS7-Base-163.repo CentOS-Base.repo

# 5. 清理旧缓存数据,缓存新数据
[root@localhost yum.repos.d]#yum clean all
# yum makecache 就是把服务器的包信息下载到本地电脑缓存起来
[root@localhost yum.repos.d]#yum makecache

# 6. 测试
[root@localhost yum.repos.d]# yum list | grep firefox
[root@localhost ~]#yum -y install firefox