Linux常用命令及参数

Linux 常用命令之记不住系列

#####以下命令均在Centos 7.5 下执行

1.查看Linux内核版本信息

1
2
[root@instance-4 ~]# uname -a
Linux instance-4 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

2.查看CPU信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@instance-4 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 2
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 62
Model name: Intel(R) Xeon(R) CPU @ 2.50GHz
Stepping: 4
CPU MHz: 2500.000
BogoMIPS: 5000.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 30720K
NUMA node0 CPU(s): 0,1

3.查看当前进程列表

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
[root@instance-4 ~]# ps aux --sort -pmem
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
shezhua 2314 3.8 19.6 4878312 1475452 ? SLsl Dec02 497:29 /usr/lib/jvm/jre-1.8.0-openjdk/bin/java
shezhua 2323 4.5 19.6 4873800 1473700 ? SLsl Dec02 593:15 /usr/lib/jvm/jre-1.8.0-openjdk/bin/java
polkitd 26706 0.1 2.3 1186388 172568 ? Ssl Dec10 2:35 mysqld --default-authentication-plugin=mysql_native_password
shezhua 2589 1.0 1.9 1310308 145396 ? Ssl Dec02 132:38 /usr/share/kibana/bin/../node/bin/node --no-warnings
root 10879 0.2 1.0 932712 76144 ? Ssl Nov30 44:12 /usr/bin/dockerd -H unix://
root 10895 0.2 0.5 577712 37588 ? Ssl Nov30 40:50 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
root 229 0.0 0.4 84136 37056 ? Ss Nov30 0:21 /usr/lib/systemd/systemd-journald
root 16251 0.0 0.3 358308 29200 ? Ssl Dec05 0:03 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
root 10887 0.2 0.3 278336 28908 ? Ssl Nov30 34:34 /usr/bin/containerd


[root@instance-4 ~]# ps -ef --sort -pid
UID PID PPID C STIME TTY TIME CMD
nginx 30773 11975 0 Dec02 ? 00:00:02 nginx: worker process
nginx 30772 11975 0 Dec02 ? 00:00:04 nginx: worker process
polkitd 26706 26689 0 Dec10 ? 00:02:35 mysqld --default-authentication-plugin=mysql_native_password
root 26689 10895 0 Dec10 ? 00:00:04 containerd-shim -namespace moby -workdir
root 26505 2 0 Dec08 ? 00:00:12 [kworker/u4:0]
root 24112 18454 0 16:17 pts/0 00:00:00 ps -ef --sort -pid
root 23832 2 0 16:12 ? 00:00:00 [kworker/0:0]
root 23631 2 0 16:10 ? 00:00:00 [kworker/1:0]


[root@instance-4 ~]# ps -aux | grep nginx
root 11975 0.0 0.0 123180 6100 ? Ss Dec01 0:00 nginx: master process nginx
root 24201 0.0 0.0 112708 976 pts/0 S+ 16:18 0:00 grep --color=auto nginx
nginx 30772 0.0 0.0 123184 5708 ? S Dec02 0:04 nginx: worker process
nginx 30773 0.0 0.0 123184 5624 ? S Dec02 0:02 nginx: worker process

4.持续查看进程统计信息

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
[root@instance-4 ~]# top
top - 16:19:43 up 11 days, 9:06, 2 users, load average: 0.01, 0.04, 0.05
Tasks: 112 total, 2 running, 110 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 3.1 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 7493724 total, 412676 free, 3559736 used, 3521312 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 3353372 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24274 root 20 0 159948 2168 1508 R 6.7 0.0 0:00.01 top
1 root 20 0 54388 6604 4016 S 0.0 0.1 1:00.35 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.56 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:23.22 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:01.64 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 R 0.0 0.0 5:33.24 rcu_sched

4.1 第一行,系统启动以来的时间,登陆用户数,负载
4.2 第二行,总任务数、运行、休眠、停止、冻结
4.3 第三行,us用户态时间、st内核态时间、wa等待时间
4.4 第四行,总内存、使用内存、空闲内存、缓存

交互快捷键
数字1:显示或隐藏CPU详情
SHIFT + p: 按CPU使用率排序
SHIFT + m: 按内存使用率排序
SHIFT + e: 切换内存列,单位k,m,g,...
按k字符: 输入进程名,杀进程
按u字符:输入用户名,过滤进程
按q,退出

5.虚拟内存统计

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@instance-4 ~]# vmstat -w 1 10
procs -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu--------
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 329604 2080 3612840 0 0 1 110 5 0 4 1 95 0 0
0 0 0 329364 2080 3612840 0 0 0 0 481 524 2 1 97 0 0
0 0 0 329364 2080 3612840 0 0 0 0 402 483 1 1 99 0 0
0 0 0 329168 2080 3612840 0 0 0 140 691 748 5 2 93 1 0
1 0 0 329116 2080 3613024 0 0 0 0 731 670 8 5 88 0 0
0 0 0 327828 2080 3614132 0 0 0 176 822 603 14 2 84 1 0
0 0 0 328772 2080 3612840 0 0 0 0 555 597 3 1 96 0 0
0 0 0 328744 2080 3612780 0 0 0 20 451 492 2 1 98 0 0
0 0 0 329068 2080 3612780 0 0 0 0 563 621 3 1 97 0 0
0 0 0 328992 2080 3612780 0 0 0 0 423 489 1 1 98 0 0

6.从CPU和IO统计设备和分区信息:

1
2
3
4
5
6
7
8
[root@instance-4 ~]# iostat -m
Linux 3.10.0-862.14.4.el7.x86_64 (instance-4) 12/11/2018 _x86_64_ (2 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle
3.65 0.00 0.96 0.08 0.00 95.30

Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda 5.79 0.00 0.21 1885 205937

7.查看可用内存 (free -wht)

1
2
3
4
5
[root@instance-4 ~]# free -wht
total used free shared buffers cache available
Mem: 7.1G 3.4G 402M 32M 2.0M 3.3G 3.2G
Swap: 0B 0B 0B
Total: 7.1G 3.4G 402M

8.统计当前目录占用磁盘空间

1
2
3
4
5
6
7
8
9
[root@instance-4 log]# du -h -d 1
8.0K ./tuned
0 ./ntpstats
20M ./audit
0 ./qemu-ga
0 ./chrony
252K ./nginx
900K ./sa
25M .

9.查看文件系统磁盘使用率

1
2
3
4
5
6
7
8
[root@instance-4 log]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 40G 9.1G 31G 23% /
devtmpfs 3.6G 0 3.6G 0% /dev
tmpfs 3.6G 0 3.6G 0% /dev/shm
tmpfs 3.6G 33M 3.6G 1% /run
tmpfs 3.6G 0 3.6G 0% /sys/fs/cgroup
tmpfs 732M 0 732M 0% /run/user/1000

10.查看网络连接、路由表、接口统计信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@instance-4 log]# netstat -lantp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 16144/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 11975/nginx: master
tcp 0 0 10.140.0.3:443 66.71.248.226:46260 SYN_RECV -
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11975/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1047/sshd
tcp 0 0 10.140.0.3:22 171.217.50.119:17564 ESTABLISHED 20046/sshd: shezhua
tcp 0 0 10.140.0.3:59684 169.254.169.254:80 ESTABLISHED 13000/python
tcp 0 0 10.140.0.3:59680 169.254.169.254:80 CLOSE_WAIT 13003/python
tcp 0 0 10.140.0.3:59682 169.254.169.254:80 ESTABLISHED 13007/python
tcp 0 52 10.140.0.3:22 171.217.50.119:18546 ESTABLISHED 18400/sshd: shezhua
tcp 0 0 10.140.0.3:59686 169.254.169.254:80 ESTABLISHED 13003/python
tcp6 0 0 ::1:25 :::* LISTEN 16144/master
tcp6 0 0 :::5601 :::* LISTEN 2568/docker-proxy
tcp6 0 0 :::9200 :::* LISTEN 2278/docker-proxy
tcp6 0 0 :::80 :::* LISTEN 11975/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1047/sshd