1 文件管理
1.1 ls:list
显示指定工作目录下的内容及属性信息,不加参数默认显示当前工作目录下的文件
**语法格式:**ls [选项] 文件名或目录名
常用选项:
选项 | 作用 |
---|---|
-a | 列出目录所有文件,包含以.开始的隐藏文件 |
-A | 列出除.及…的所有文件 |
-r | 反序排列 |
-R | 递归列出所有子目录 |
-t | 以文件修改时间排序 |
-s | 以文件大小排序 |
-l | 除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来 |
-d | 仅列出目录本身,而不是列出目录内的文件数据 |
-h | 将文件容量以人类较易读的方式列出来(如GB、KB等) |
实例:
- 列出当前目录下的目录:
[root@localhost /]# ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- 列出当前目录下的所有目录
[root@localhost /]# ls -a
. .. bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- 列出文件的详细信息
[root@localhost /]# ls -l
total 58
lrwxrwxrwx. 1 root root 7 Jan 15 04:14 bin -> usr/bin
dr-xr-xr-x. 6 root root 1024 Jan 15 04:17 boot
drwxr-xr-x. 19 root root 3160 Jan 16 01:19 dev
drwxr-xr-x. 73 root root 4096 Jan 16 01:58 etc
drwxr-xr-x. 3 root root 4096 Jan 16 01:58 home
lrwxrwxrwx. 1 root root 7 Jan 15 04:14 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Jan 15 04:14 lib64 -> usr/lib64
drwx------. 2 root root 16384 Jan 15 04:14 lost+found
drwxr-xr-x. 2 root root 4096 Nov 5 2016 media
drwxr-xr-x. 2 root root 4096 Nov 5 2016 mnt
drwxr-xr-x. 2 root root 4096 Nov 5 2016 opt
dr-xr-xr-x. 89 root root 0 Jan 16 01:19 proc
dr-xr-x---. 2 root root 4096 Jan 16 01:35 root
drwxr-xr-x. 21 root root 580 Jan 16 01:19 run
lrwxrwxrwx. 1 root root 8 Jan 15 04:14 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Nov 5 2016 srv
dr-xr-xr-x. 13 root root 0 Jan 16 01:19 sys
drwxrwxrwt. 7 root root 4096 Jan 16 03:17 tmp
drwxr-xr-x. 13 root root 4096 Jan 15 04:14 usr
drwxr-xr-x. 19 root root 4096 Jan 15 04:27 var
- 列出指定路径下的目录
[root@localhost /]# ls /root
anaconda-ks.cfg
- 按修改时间排序
[root@localhost /]# ls -lt
total 58
drwxrwxrwt. 11 root root 4096 Jan 16 04:33 tmp
drwxr-xr-x. 73 root root 4096 Jan 16 01:58 etc
drwxr-xr-x. 3 root root 4096 Jan 16 01:58 home
dr-xr-x---. 2 root root 4096 Jan 16 01:35 root
drwxr-xr-x. 21 root root 580 Jan 16 01:19 run
drwxr-xr-x. 19 root root 3160 Jan 16 01:19 dev
dr-xr-xr-x. 13 root root 0 Jan 16 01:19 sys
dr-xr-xr-x. 89 root root 0 Jan 16 01:19 proc
drwxr-xr-x. 19 root root 4096 Jan 15 04:27 var
dr-xr-xr-x. 6 root root 1024 Jan 15 04:17 boot
drwxr-xr-x. 13 root root 4096 Jan 15 04:14 usr
lrwxrwxrwx. 1 root root 8 Jan 15 04:14 sbin -> usr/sbin
lrwxrwxrwx. 1 root root 7 Jan 15 04:14 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Jan 15 04:14 lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 7 Jan 15 04:14 bin -> usr/bin
drwx------. 2 root root 16384 Jan 15 04:14 lost+found
drwxr-xr-x. 2 root root 4096 Nov 5 2016 media
drwxr-xr-x. 2 root root 4096 Nov 5 2016 mnt
drwxr-xr-x. 2 root root 4096 Nov 5 2016 opt
drwxr-xr-x. 2 root root 4096 Nov 5 2016 srv
1.2 cp:copy
复制文件和目录
**语法格式:**cp [选项] 源文件 目标文件
常用选项
选项 | 作用 |
---|---|
-a | 相当于-dr –preserve=all,递归复制,复制的数据会保留原有数据的时间戳等信息 |
-d | 若源文件为硬链接或软连接,则复制连接文件属性而非文件本身 |
-f | 强制复制 |
-i | 若目标文件已存在时,在覆盖时会先询问操作的进行 |
-p | 连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性(备份常用) |
-r | 递归复制,用于目录的复制操作 |
-l | 进行硬链接,而非复制文件本身 |
-s | 进行符号连接(软连接),即“快捷方式” |
–preserve=all | 除了-p的权限相关参数外,还加入SELinux属性,links、xattr等也复制 |
实例
- 复制文件
[root@localhost /]# cp /etc/fstab /tmp/
[root@localhost /]# ls /tmp
fstab ks-script-sAZCIi yum.log
- 复制目录 多级目录不能直接复制,要递归复制
[root@localhost /]# cp /boot /tmp
cp: omitting directory ‘/boot’
[root@localhost /]# cp -r /boot /tmp
1.3 mkdir:make directories
用于创建目录
**语法格式:**mkdir [选项] 目录
常用选项
选项 | 作用 |
---|---|
-p | 递归创建多级目录 |
-m | 建立目录的同时设置目录权限 |
实例
- 在当前目录下建立一个名为tmp1的目录
[root@localhost tmp]# mkdir tmp1
- 在/tmp目录下建立一个名为tmp2的目录
[root@localhost tmp]# mkdir /tmp/tmp2
- 建立一个存在的目录
[root@localhost tmp]# mkdir tmp1
mkdir: cannot create directory ‘tmp1’: File exists
- 递归创建多级目录
[root@localhost tmp]# mkdir tmp3/tmp/tmp
mkdir: cannot create directory ‘tmp3/tmp/tmp’: No such file or directory
[root@localhost tmp]# mkdir -p tmp3/tmp/tmp
- 在/tmp目录下创建子目录temp4,并且设置文件属主有读、写和执行权限,其他人无权访问
[root@localhost /]# mkdir -m 700 /tmp/tmp4
[root@localhost /]# ls -l /tmp
total 28
dr-xr-xr-x. 6 root root 4096 Jan 16 04:24 boot
-rw-r--r--. 1 root root 501 Jan 16 04:03 fstab
-rwx------. 1 root root 836 Jan 15 04:17 ks-script-sAZCIi
drwxr-xr-x. 2 root root 4096 Jan 16 04:31 tmp1
drwxr-xr-x. 2 root root 4096 Jan 16 04:31 tmp2
drwxr-xr-x. 3 root root 4096 Jan 16 04:33 tmp3
drwx------. 2 root root 4096 Jan 16 05:44 tmp4
-rw-------. 1 root root 0 Jan 15 04:14 yum.log
1.4 cat:concatenate files and print on the standard output
查看纯文本文件
**语法格式:**cat [选项] 文件
常用选项
选项 | 作用 |
---|---|
-n | 显示行数(空行也编号) |
-b | 显示行数(空行不编号) |
-A | 相当于-vET,可列出一些特殊字符而不是空白而已 |
-E | 将结尾的换行符以$显示出来 |
-T | 将[tab]键以^I显示出来 |
-v | 列出一些看不出来的特殊字符 |
用例
- 查看/etc/fstab
[root@localhost /]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jan 15 04:14:26 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=e3ee29aa-3286-4515-bae2-1558e60a0c7e / ext4 defaults 1 1
UUID=4111b6a8-bc5a-4781-b3d3-5c8167353b7f /boot ext4 defaults 1 2
UUID=e60a9f33-df7b-467d-a284-b18b731565ae swap swap defaults 0 0
- 使用-n选项查看/etc/fstab
[root@localhost /]# cat -n /etc/fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Wed Jan 15 04:14:26 2020
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=e3ee29aa-3286-4515-bae2-1558e60a0c7e / ext4 defaults 1 1
10 UUID=4111b6a8-bc5a-4781-b3d3-5c8167353b7f /boot ext4 defaults 1 2
11 UUID=e60a9f33-df7b-467d-a284-b18b731565ae swap swap defaults 0 0
- 使用-b选项查看/etc/fstab
[root@localhost /]# cat -b /etc/fstab
1 #
2 # /etc/fstab
3 # Created by anaconda on Wed Jan 15 04:14:26 2020
4 #
5 # Accessible filesystems, by reference, are maintained under '/dev/disk'
6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7 #
8 UUID=e3ee29aa-3286-4515-bae2-1558e60a0c7e / ext4 defaults 1 1
9 UUID=4111b6a8-bc5a-4781-b3d3-5c8167353b7f /boot ext4 defaults 1 2
10 UUID=e60a9f33-df7b-467d-a284-b18b731565ae swap swap defaults 0 0
1.5 rm:remove
删除文件或目录
**语法格式:**rm [选项] 文件或目录
常用选项
选项 | 作用 |
---|---|
-i | 交互模式,删除前会询问用户是否操作 |
-r | 递归删除,无此选项无法删除目录 |
-f | 强制删除文件或目录 |
-v | 显示指令的详细执行过程 |
用例
- 删除文件
[root@localhost /]# rm /tmp/fstab
rm: remove regular file ‘/tmp/fstab’? y
- 使用-i选项删除文件
[root@localhost /]# rm -i /tmp/fstab
rm: remove regular file ‘/tmp/fstab’? y
- 使用-f选项删除文件
[root@localhost /]# rm -f /tmp/fstab
- 删除目录
[root@localhost /]# rm /tmp/tmp1
rm: cannot remove ‘/tmp/tmp1’: Is a directory
[root@localhost /]# rm -r /tmp/tmp1
rm: remove directory ‘/tmp/tmp1’? y
- 使用-v选项删除目录
[root@localhost /]# rm -rfv /tmp/tmp3
removed directory: ‘/tmp/tmp3/tmp/tmp’
removed directory: ‘/tmp/tmp3/tmp’
removed directory: ‘/tmp/tmp3’
1.6 mv:move
移动文件或目录、改名
**语法格式:**mv [选项] 文件或目录
常用选项
选项 | 作用 |
---|---|
-i | 若存在同名文件,则向询问是否覆盖 |
-f | 覆盖已有文件时也不进行任何提示 |
-b | 当文件已存在时覆盖前为其创建一个备份 |
-u | 当源文件比目标文件新,或者目标文件不存在时才执行此移动 |
用例
- 将/tmp目录下的fstab文件移到/tmp/tmp1下
[root@localhost /]# mv /tmp/fstab tmp/tmp1
- 使用-i选项尝试覆盖
[root@localhost /]# mv -i /tmp/fstab tmp/tmp1
mv: overwrite ‘./tmp1’? y
- 使用-f选项尝试覆盖
[root@localhost /]# mv -f /tmp/fstab tmp/tmp1
- 使用-b选项尝试覆盖
[root@localhost /]# mv -b /tmp/fstab /tmp/tmp1
mv: overwrite ‘/tmp/tmp1/fstab’? y
[root@localhost /]# ls tmp/tmp1
fstab fstab~
1.7 touch
修改文件时间或创建新文件
**语法格式:**touch [选项] 文件
常用选项
选项 | 作用 |
---|---|
-a | 仅自定义access time |
-c | 修改文件的时间,若该文件不存在则不建立新文件 |
-d/–date | 接自定义的日期而非当前日期 |
-m | 仅修改mtime |
-t | 接自定义的时间而不使用当前时间,格式[YYYMMDDhhmm] |
2 工作目录
2.1 pwd:print working directory
显示当前工作目录的绝对路径
**语法结构:**pwd [选项]
常用选项
选项 | 作用 |
---|---|
-L | 显示逻辑路径 |
用例
[root@localhost ~]# pwd
/root
2.2 cd:change directory
切换工作目录
**语法结构:**cd [选项] 目录
用例
- 进入用户主目录
[root@localhost ~]# cd
[root@localhost ~]# cd ~
[root@localhost ~]#
- 返回进入此目录之前所在的目录
[root@localhost sysconfig]# cd -
/root
[root@localhost ~]#
- 返回上级目录
[root@localhost ~]# cd ..
[root@localhost /]#
- 进入当前目录的子目录
[root@localhost tmp]# cd ./tmp1
[root@localhost tmp1]#
3 用户管理
3.1 useradd
创建用户
**语法结构:**useradd [选项] 用户名
常用选项
选项 | 作用 |
---|---|
-d | 指定用户的家目录(默认为/home/username) |
-u | 指定用户的默认UID |
-g | 指定一个初始的用户基本组(必须已存在) |
-G | 指定一个或多个拓展用户组 |
-m | 指定创建家目录 |
-M | 指定不创建家目录 |
用例
创建一个名为alice的用户,不创建家目录,指定UID为6666
[root@localhost ~]# useradd -M -u 6666 alice
[root@localhost ~]# su alice
bash-4.2$
bash-4.2$ cd
bash: cd: /home/alice: No such file or directory
bash-4.2$ id alice
uid=6666(alice) gid=6666(alice) groups=6666(alice)
3.2 userdel
删除用户
- 若当前用户是由前一个用户A通过su命令切换而来,则可能无法删除A,需要ctrl+d退出A
**语法结构:**userdel [选项] 用户名
常用选项
选项 | 作用 |
---|---|
-f | 强制删除用户 |
-r | 同时删除用户及用户家目录 |
实例
删除用户alice
[root@localhost ~]# userdel alice
3.3 passwd
修改密码
**语法结构:**passwd [选项] 用户名
常用选项
选项 | 作用 |
---|---|
-l | 锁定用户,禁止其登录 |
-u | 解除锁定,允许用户登录 |
-e | 强制用户在下次登录时修改密码 |
实例
- 修改密码
[root@localhost ~]# passwd alice
Changing password for user alice.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
- 锁定用户alice,会将其密码删除
[root@localhost ~]# passwd -l alice
Locking password for user alice.
passwd: Success
- 解除alice的锁定
[root@localhost ~]# passwd -u alice
Unlocking password for user alice.
passwd: Success
- 强制alice在下次登录时需要修改密码
[root@localhost ~]# passwd -e alice
Expiring password for user alice.
passwd: Success
//尝试使用alice登录
You are required to change your password immediately (root enforced)
Last login: Thu Jan 16 07:44:39 2020 from 192.168.159.1
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user alice.
Changing password for alice.
(current) UNIX password:
New password:
BAD PASSWORD: The password is the same as the old one
4 网络管理
4.1 ifconfig:network interfaces configuring
配置和显示Linux内核中网络接口的网络参数
**语法结构:**ifconfig [选项] [接口]
常用选项
选项 | 作用 |
---|---|
-a | 显示全部接口信息 |
-s | 显示摘要信息,类似netstat -i |
-v | 对于某些错误条件,-v更详细 |
用例
- 显示激活的网卡信息
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.159.129 netmask 255.255.255.0 broadcast 192.168.159.255
inet6 fe80::f74f:5221:c828:ed5c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:59:90:81 txqueuelen 1000 (Ethernet)
RX packets 24376 bytes 14980522 (14.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12518 bytes 5578785 (5.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 显示所有网卡信息(单网卡,同上)
- 显示简要的网卡信息
[root@localhost ~]# ifconfig -s
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33 1500 24460 0 0 0 12555 0 0 0 BMRU
lo 65536 0 0 0 0 0 0 0 0 LRU
4.2 ping:Packet Internet Groper
主要用来测试主机之间网络的连通性
语法结构: ping [选项] 目标主机
常用选项
选项 | 作用 |
---|---|
-4 | 使用IPv4地址 |
-6 | 使用IPv6地址 |
-b | 允许ping广播地址 |
-c count | 指定发报文的次数 |
-i interval | 指定发送间隔时间,默认1s |
-s packetsize | 指定要发送数据字节数 |
-t ttl | 设置IP生存时间值 |
-w deadline | ping命令的存活时间 |
用例
- 测试与baidu.com的连通性
[root@localhost ~]# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=76.6 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=99.0 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=62.2 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=143 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 62.258/95.239/143.072/30.568 ms
- 指定发送报文次数为2,间隔时间为5s
[root@localhost ~]# ping -c 2 -i 5 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.042 ms
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 5000ms
rtt min/avg/max/mdev = 0.026/0.034/0.042/0.008 ms
4.3 netstat
用于显示网络状态,监听统计TCP、UDP等协议数据,管理进程内存
**语法结构:**netstat [选项]
常用选项
选项 | 作用 |
---|---|
-a/–all | 显示所有连线中的socket |
-A<网络类型> | 列出该网络类型连线中的相关地址 |
-c/–continuous | 持续列出网络状态 |
-C/–cache | 显示路由器配置的快取信息 |
-e/–extend | 显示网络其他相关信息 |
-g/–groups | 显示多重广播功能群组组员名单 |
-i/–interfaces | 显示网卡信息 |
-l/–listenning | 显示监控中的服务器socket |
-n/–numeric | 不做域名解析,直接使用ip地址 |
-o/–timers | 显示计时器 |
-p/–programs | 显示正在使用socket的程序识别码和程序名称 |
-r/–route | 显示路由表 |
-t/–tcp | 显示TCP传输协议的连线状况 |
-u/–udp | 显示UDP传输协议的连线状况 |
用例
- 显示网卡列表
[root@localhost ~]# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33 1500 2770 0 0 0 902 0 0 0 BMRU
lo 65536 0 0 0 0 0 0 0 0 LRU
virbr0 1500 0 0 0 0 0 0 0 0 BMU
- 显示组播组的关系
[root@localhost ~]# netstat -g
IPv6/IPv4 Group Memberships
Interface RefCnt Group
--------------- ------ ---------------------
lo 1 all-systems.mcast.net
ens33 1 224.0.0.251
ens33 1 all-systems.mcast.net
virbr0 1 224.0.0.251
virbr0 1 all-systems.mcast.net
lo 1 ff02::1
lo 1 ff01::1
ens33 1 ff02::1:ff28:ed5c
ens33 1 ff02::1
ens33 1 ff01::1
virbr0 1 ff02::1
virbr0 1 ff01::1
virbr0-nic 1 ff02::1
virbr0-nic 1 ff01::1
- 显示网络统计(只截取了一部分)
[root@localhost ~]# netstat -s
Ip:
1055 total packets received
0 forwarded
0 incoming packets discarded
775 incoming packets delivered
681 requests sent out
32 dropped because of missing route
- **常用选项:**tcp、udp、监控中、不做域名解析、显示进程名
[root@localhost ~]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1049/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1254/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1285/sshd: root@pts
- 显示路由信息
[root@localhost ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default gateway 0.0.0.0 UG 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.159.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
4.4 kill
中止一个进程
**语法格式:**kill [-signal] [pid]
常用选项(kill的重点不在选项,大多数都是信号)
选项 | 作用 |
---|---|
-e | 要求匹配进程名称 |
-i | 杀死进程前会先询问用户 |
-I | 忽略大小写 |
-L | 列出所有信号值含义 |
-q | 禁止输出警告信息 |
-s | 发送指定的信号 |
-1 | HUP 终端断线 |
-2 | INT 中断(同Ctrl+C) |
-3 | QUIT 退出(同Ctrl+D) |
-15 | TERM 终止 |
-9 | KILL 强迫终止 |
-18 | CONT 持续,与STOP相反 |
-19 | STOP 暂停,与CONT相反(同Ctrl+Z) |
5 其他常用命令
5.1 echo
在终端设备上输出字符串或变量提取后的值,或在变量前加上 符 号 的 方 式 提 取 出 变 量 的 值 , 例 如 : 符号的方式提取出变量的值,例如: 符号的方式提取出变量的值,例如:PATH,然后再用echo命令予以输出
**语法格式:**echo [选项] [参数]
常用选项
选项 | 作用 |
---|---|
-n | 不输出结尾的换行符 |
-e | 激活转义字符(\t、\n) |
用例
- 输出一段字符串
[root@localhost ~]# echo "hello world"
hello world
- 输出变量提取后的值
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
- 对内容进行转译,不让$符号的提取变量值功能生效
[root@localhost ~]# echo \$PATH
$PATH
- 输出带有换行符的内容
[root@localhost ~]# echo -e "a\nb\nc"
a
b
c
5.2 tty:teletypewriter
显示终端机连接标准输入设备的文件名称
**语法结构:**tty
实例
- 通过本地登录
[root@localhost ~]# tty
/dev/tty1
- 通过远程登录
[root@localhost ~]# tty
/dev/pts/0
5.3 mount
挂载文件系统到指定的挂载点
**语法结构:**mount [选项] <参数类型>[参数]
常用选项
选项 | 作用 |
---|---|
-a | 挂载文件/etc/fstab中描述的所有文件系统 |
-l | 显示已加载的文件系统列表 |
-r | 将文件系统挂载为只读模式 |
-w | 将文件系统挂载为读写模式,默认 |
-n | 挂载没有写入文件/etc/mtab中的文件系统 |
-t | 挂载指定类型的文件系统 |
用例
- 挂载/dev/cdrom到/mnt(光驱文件有写保护,挂载为只读模式)
[root@localhost ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
- 启动所有挂载
[root@localhost ~]# mount -a
- 显示已挂载的文件系统
[root@localhost ~]# mount -l
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=490216k,nr_inodes=122554,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_prio,net_cls)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type ext4 (rw,relatime,seclabel,data=ordered)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=31,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=11288)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=99972k,mode=700)
/dev/sr0 on /mnt type iso9660 (ro,relatime)
/dev/sr0 on /dev type iso9660 (ro,relatime)
- 将/dev/cdrom以只读模式挂载到/mnt
[root@localhost ~]# mount -r /dev/cdrom /mnt
5.4 umount
卸载文件系统
**语法结构:**umount [选项] <参数类型>[参数]
常用选项
选项 | 作用 |
---|---|
-a | 卸载所有文件系统 |
-n | 卸载时不将信息存入/etc/mtab文件中 |
-r | 若无法卸载成功,则尝试以只读的方式重新挂载该文件系统 |
-v | 执行时显示详细的信息 |
-f | 强制卸载文件系统 |
用例
卸载所有文件系统,并显示执行时的详细信息
[root@localhost ~]# umount -av
/mnt : successfully umounted
/run/user/0 : successfully umounted
/boot : successfully umounted
/dev/mqueue : successfully umounted
/dev/hugepages : successfully umounted
/sys/kernel/debug : successfully umounted
/proc/sys/fs/binfmt_misc : successfully umounted
/sys/fs/selinux : ignored
umount: /: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
/ : successfully umounted
/sys/kernel/config : successfully umounted
/sys/fs/cgroup/perf_event: successfully umounted
/sys/fs/cgroup/memory : successfully umounted
/sys/fs/cgroup/cpuset : successfully umounted
/sys/fs/cgroup/pids : successfully umounted
/sys/fs/cgroup/hugetlb : successfully umounted
/sys/fs/cgroup/devices : successfully umounted
/sys/fs/cgroup/net_cls,net_prio: successfully umounted
/sys/fs/cgroup/freezer : successfully umounted
/sys/fs/cgroup/blkio : successfully umounted
/sys/fs/cgroup/cpu,cpuacct: successfully umounted
/sys/fs/pstore : successfully umounted
umount: /sys/fs/cgroup/systemd: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
/sys/fs/cgroup/systemd : successfully umounted
umount: /sys/fs/cgroup: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
/sys/fs/cgroup : successfully umounted
umount: /run: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
/run : successfully umounted
/dev/pts : ignored
/dev/shm : successfully umounted
/sys/kernel/security : successfully umounted
umount: /dev: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
/dev : successfully umounted
/proc : ignored
/sys : ignored
5.5 type
显示指定命令的类型,判断给出的命令是内建命令还是外部命令。一个命令的类型可能是如下之一
- alias 别名
- keyword 关键字,shell保留字
- fuction 函数,shell函数
- builtin 内建命令,shell内建命令
- file 文件,磁盘文件,外部命令
- unfound 没有找到
**语法结构:**type [选项] [参数]
常用选项
选项 | 作用 |
---|---|
-a | 返回所有可能的类型 |
-p | 只返回外部命令,并显示绝对路径 |
用例
- 判断命令pwd的类型
[root@localhost ~]# type pwd
pwd is a shell builtin
- 判命令pwd所有可能的类型
[root@localhost ~]# type -a pwd
pwd is a shell builtin
pwd is /usr/bin/pwd
- 只返回外部命令
[root@localhost ~]# type -p cd
[root@localhost ~]# type -p ifconfig
/usr/sbin/ifconfig
5.6 which
用于查找文件,会在环境变量$PATH设置的目录里查找符合条件的文件
**语法结构:**which [参数]
用例
- 查找bash
[root@localhost ~]# which bash
/usr/bin/bash
- 查找ifconfig
[root@localhost ~]# which ifconfig
/usr/sbin/ifconfig
5.7 man
查看命令帮助
**语法结构:**man [选项] [参数]
常用选项
选项 | 作用 |
---|---|
-a | 在所有的man帮助手册中搜索 |
-f | 相当于whatis指令,显示给定关键字的简短描述信息 |
用例
- 查看man的说明文件
[root@localhost ~]# man man
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-33miPnAW-1583725726386)(1.3 Linux入门-常用命令/image-20200117112551969.png)]
- 只查看简述信息
[root@localhost ~]# man -f ls
ls (1) - list directory contents
[root@localhost ~]# whatis ls
ls (1) - list directory contents
5.8 init
进程初始化工具,init进程是所有Linux进程的父进程,是由内核引导运行的用户级进程,系统中的第一个进程
**语法结构:**init [选项] [参数]
运行级别 | 说明 |
---|---|
0 | 几乎所有进程都关闭,停机状态 |
1 | 单用户模式,root账户进行操作 |
2 | 多用户模式,不能使用NFS |
3 | 完全多用户模式,一部分启动,一部分不启动,命令行界面 |
4 | 未使用、未定义的保留模式 |
5 | 可视化模式,3级别中启动的进程都启动,并且会启动一部分图形界面进程 |
6 | 停止所有进程,卸载文件系统,重新启动 |
5.9 shutdown
可用用来进行关机程序
**语法结构:**shutdown [选项] <选项参数>
常用选项
选项 | 作用 |
---|---|
-t | 设定在几秒钟之后进行关机程序 |
-k | 不会真的关机,将警告许仙你传送给所有使用者 |
-r | 关机后重新启动 |
-h | 关机后停机 |
-n | 不采用正常程序关机(init),用强迫的方式杀死所有执行中的程序后自行关机 |
-f | 关机时不执行fsck |
-F | 关机时强制执行fsck |
5.10 reboot
重新启动计算机
**语法结构:**reboot [选项]
常用选项
选项 | 作用 |
---|---|
-n | 在重开机前不做将记忆体资料写回硬盘的动作 |
-w | 并不会真的重开机,只是把记录写到 /var/log/wtmp 档案里 |
-d | 不把记录写到 /var/log/wtmp 档案里(-n 这个参数包含了 -d) |
-f | 强迫重开机,不呼叫 shutdown 这个指令 |
-i | 在重开机之前先把所有网络相关的装置先停止 |
5.11 history
显示与操纵历史命令
**语法结构:**history [选项] [参数]
常用选项
选项 | 作用 |
---|---|
-a | 将当前会话的历史记录写入历史命令文件中 |
-c | 清空当前历史命令 |
-d | 删除历史命令列表中指定序号的命令 |
-r | 将历史命令文件中的命令读入当前历史命令缓冲区 |
-w | 将最近的历史缓冲写入历史命令文件 |
用例
- 打印最近10条历史记录
[root@localhost ~]# history 10
234 man man
235 man ls
236 man -a ls
237 man -f ls
238 whatis ls
239 man history
240 history -h
241 help history
242 history
243 history 10
- 清空当前历史命令
[root@localhost ~]# history -c
[root@localhost ~]# history
1 history