博客PS示例
一显示指定真正用户名(RUID)或用户ID的进程
打开另外一个终端
[root@centos72 ~]# id wang
uid=1000(wang) gid=1000(wang) groups=1000(wang)
[root@centos72 ~]# su - wang
Last login: Thu May 9 16:22:21 CST 2019 on pts/1
[wang@centos72 ~]$ passwd
Changing password for user wang.
Changing password for wang.
(current) UNIX password:
uid,euid都表示有效用户是root,真正的用户是wang
因为这是因为普通用户具有suid权限
[root@centos72 ~]# ps axo pid,cmd,ni,%cpu,uid,euid,ruid | tail
1252 [kworker/0:2] 0 0.0 0 0 0
1269 [kworker/0:0] 0 0.0 0 0 0
1270 [kworker/1:0] 0 0.0 0 0 0
1273 [kworker/1:2] 0 0.0 0 0 0
1276 su - wang 0 0.0 0 0 0
1277 -bash 0 0.0 1000 1000 1000
1300 passwd 0 0.1 0 0 1000
1305 [kworker/0:1] 0 0.0 0 0 0
1308 ps axo pid,cmd,ni,%cpu,uid, 0 0.0 0 0 0
1309 tail 0 0.0 0 0 0
显示wang用户,会显示真正发起进程的用户以及有效用户
-f: 显示完整格式程序信息
-U userlist 指定真正的用户ID或名称
从下面可以看出执行passwd进程的真正用户是root
[root@centos72 ~]# ps -fU wang
UID PID PPID C STIME TTY TIME CMD
wang 2135 2134 0 00:49 pts/3 00:00:00 -bash
root 2195 2135 0 00:55 pts/3 00:00:00 passwd
[wang@centos72 ~]$ passwd
Changing password for user wang.
Changing password for wang.
(current) UNIX password:
二显示指定有效用户名(RUID)或用户ID的进程
ps -fu wang 或者ps -fu 1000显示指定有效用户名(EUID)或用户ID的进程
-u userlist 指定有效的用户ID或名称
[root@centos72 ~]# ps -fu wang
UID PID PPID C STIME TTY TIME CMD
wang 2135 2134 0 00:49 pts/3 00:00:00 -bash
3100
查看以root用户权限(实际和有效ID)运行的每个进程:
ps -U root -u root
[root@centos72 ~]# ps -U root -u root | head
PID TTY TIME CMD
1 ? 00:00:01 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
6 ? 00:00:00 kworker/u256:0
7 ? 00:00:00 migration/0
8 ? 00:00:00 rcu_bh
9 ? 00:00:00 rcu_sched
10 ? 00:00:00 lru-add-drain
[root@centos72 ~]# ps -U root -u root | tail
2053 ? 00:00:00 sshd
2057 pts/2 00:00:00 bash
2110 ? 00:00:00 sshd
2114 pts/3 00:00:00 bash
2134 pts/3 00:00:00 su
2189 ? 00:00:00 kworker/0:0
2195 pts/3 00:00:00 passwd
2214 ? 00:00:00 kworker/0:2
2227 pts/2 00:00:00 ps
2228 pts/2 00:00:00 tail
[root@centos72 ~]# ps -U root -u root | wc
108 432 3822
三列出实际组真正组拥有的所有进程(实际组ID:RGID或名称)
[wang@centos72 ~]$ passwd
Changing password for user wang.
Changing password for wang.
(current) UNIX password:
[root@centos72 ~]# id wang
uid=1000(wang) gid=1000(wang) groups=1000(wang)
[root@centos72 ~]# ps -fG wang
UID PID PPID C STIME TTY TIME CMD
wang 2135 2134 0 00:49 pts/3 00:00:00 -bash
root 2195 2135 0 00:55 pts/3 00:00:00 passwd
[root@centos72 ~]# ps -fG 1000
UID PID PPID C STIME TTY TIME CMD
wang 2135 2134 0 00:49 pts/3 00:00:00 -bash
root 2195 2135 0 00:55 pts/3 00:00:00 passwd
四列出有效组名称(或会话)所拥有的所有进程
很明显实际和有效是有区别的
[wang@centos72 ~]$ passwd
Changing password for user wang.
Changing password for wang.
(current) UNIX password:
[root@centos72 ~]# ps -fg 1000
UID PID PPID C STIME TTY TIME CMD
[root@centos72 ~]# ps -fG 1000
UID PID PPID C STIME TTY TIME CMD
wang 2135 2134 0 00:49 pts/3 00:00:00 -bash
root 2195 2135 0 00:55 pts/3 00:00:00 passwd
五通过进程ID来显示所属的进程
-p pid 显示指pid的进程
[root@centos72 ~]# ps -fp 1
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 00:28 ? 00:00:01 /usr/lib/systemd/systemd --switched-root --system --de
显示指定PID的多个进程:
ps -fp 多个进程编号,以逗号分开
[root@centos72 ~]# ps -fp 1,2110
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 00:28 ? 00:00:01 /usr/lib/systemd/systemd --switched-root --system --de
root 2110 785 0 00:49 ? 00:00:00 sshd: root@pts/3
六以父进程ID来显示其下所有的进程
ps -f --ppid
[root@centos72 ~]# pstree -p
systemd(1)─┬─NetworkManager(544)─┬─{NetworkManager}(591)
│ └─{NetworkManager}(593)
├─VGAuthService(545)
├─agetty(558)
├─auditd(515)───{auditd}(516)
├─crond(555)
├─dbus-daemon(540)───{dbus-daemon}(542)
├─irqbalance(538)
├─master(869)─┬─pickup(870)
│ └─qmgr(871)
├─polkitd(539)─┬─{polkitd}(541)
│ ├─{polkitd}(543)
│ ├─{polkitd}(569)
│ ├─{polkitd}(570)
│ └─{polkitd}(573)
├─rsyslogd(787)─┬─{rsyslogd}(790)
│ └─{rsyslogd}(791)
├─sshd(785)─┬─sshd(1421)───bash(1425)
│ ├─sshd(1916)───bash(1920)
│ ├─sshd(2053)───bash(2057)───pstree(2255)
│ └─sshd(2110)───bash(2114)───su(2134)───bash(2135)───passwd(2195)
├─systemd-journal(357)
├─systemd-logind(548)
├─systemd-udevd(388)
├─tuned(784)─┬─{tuned}(1026)
│ ├─{tuned}(1027)
│ ├─{tuned}(1028)
│ └─{tuned}(1041)
└─vmtoolsd(546)───{vmtoolsd}(580)
第1个进程的子进程
[root@centos72 ~]# ps -f --ppid 1
UID PID PPID C STIME TTY TIME CMD
root 357 1 0 00:28 ? 00:00:00 /usr/lib/systemd/systemd-journald
root 388 1 0 00:28 ? 00:00:01 /usr/lib/systemd/systemd-udevd
root 515 1 0 00:28 ? 00:00:00 /sbin/auditd
root 538 1 0 00:28 ? 00:00:00 /usr/sbin/irqbalance --foreground
polkitd 539 1 0 00:28 ? 00:00:00 /usr/lib/polkit-1/polkitd --no-debug
dbus 540 1 0 00:28 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nof
root 544 1 0 00:28 ? 00:00:00 /usr/sbin/NetworkManager --no-daemon
root 545 1 0 00:28 ? 00:00:00 /usr/bin/VGAuthService -s
root 546 1 0 00:28 ? 00:00:02 /usr/bin/vmtoolsd
root 548 1 0 00:28 ? 00:00:00 /usr/lib/systemd/systemd-logind
root 555 1 0 00:28 ? 00:00:00 /usr/sbin/crond -n
root 558 1 0 00:28 tty1 00:00:00 /sbin/agetty --noclear tty1 linux
root 784 1 0 00:28 ? 00:00:00 /usr/bin/python -Es /usr/sbin/tuned -l -P
root 785 1 0 00:28 ? 00:00:00 /usr/sbin/sshd -D
root 787 1 0 00:28 ? 00:00:00 /usr/sbin/rsyslogd -n
root 869 1 0 00:28 ? 00:00:00 /usr/libexec/postfix/master -w
├─sshd(785)─┬─sshd(1421)───bash(1425)
│ ├─sshd(1916)───bash(1920)
│ ├─sshd(2053)───bash(2057)───pstree(2261)
│ └─sshd(2110)───bash(2114)───su(2134)───bash(2135)───passwd(2195)
├─systemd-journal(357)
├─systemd-logind(548)
├─systemd-udevd(388)
├─tuned(784)─┬─{tuned}(1026)
│ ├─{tuned}(1027)
│ ├─{tuned}(1028)
│ └─{tuned}(1041)
└─vmtoolsd(546)───{vmtoolsd}(580)
[root@centos72 ~]# ps -f --ppid 785
UID PID PPID C STIME TTY TIME CMD
root 1421 785 0 00:29 ? 00:00:00 sshd: root@pts/0
root 1916 785 0 00:34 ? 00:00:00 sshd: root@pts/1
root 2053 785 0 00:39 ? 00:00:00 sshd: root@pts/2
root 2110 785 0 00:49 ? 00:00:00 sshd: root@pts/3
七按tty显示所属进程:ps -ft
好处就是杀死非法的终端进程
实际上只要把父进程杀死就可以了
[root@centos72 ~]# tty
/dev/pts/2
[root@centos72 ~]# ps -ft
PID TTY STAT TIME COMMAND
2057 pts/2 Ss 0:00 -bash
2265 pts/2 R+ 0:00 \_ ps -ft
[root@centos72 ~]#
[root@centos72 ~]#
[root@centos72 ~]# ps -ft /dev/pts/2
UID PID PPID C STIME TTY TIME CMD
root 2057 2053 0 00:39 pts/2 00:00:00 -bash
root 2269 2057 0 01:22 pts/2 00:00:00 ps -ft /dev/pts/2
[root@centos72 ~]# ps -ft pts/2
UID PID PPID C STIME TTY TIME CMD
root 2057 2053 0 00:39 pts/2 00:00:00 -bash
root 2270 2057 0 01:22 pts/2 00:00:00 ps -ft pts/2
八以进程树显示系统中的进程如何相互链接
ps -e --forest
[root@centos72 ~]# ps -e --forest
PID TTY TIME CMD
2 ? 00:00:00 kthreadd
3 ? 00:00:00 \_ ksoftirqd/0
5 ? 00:00:00 \_ kworker/0:0H
6 ? 00:00:00 \_ kworker/u256:0
7 ? 00:00:00 \_ migration/0
8 ? 00:00:00 \_ rcu_bh
9 ? 00:00:00 \_ rcu_sched
10 ? 00:00:00 \_ lru-add-drain
11 ? 00:00:00 \_ watchdog/0
12 ? 00:00:00 \_ watchdog/1
13 ? 00:00:00 \_ migration/1
14 ? 00:00:00 \_ ksoftirqd/1
15 ? 00:00:00 \_ kworker/1:0
16 ? 00:00:00 \_ kworker/1:0H
18 ? 00:00:00 \_ kdevtmpfs
19 ? 00:00:00 \_ netns
20 ? 00:00:00 \_ khungtaskd
21 ? 00:00:00 \_ writeback
22 ? 00:00:00 \_ kintegrityd
23 ? 00:00:00 \_ bioset
24 ? 00:00:00 \_ kblockd
25 ? 00:00:00 \_ md
26 ? 00:00:00 \_ edac-poller
32 ? 00:00:00 \_ kswapd0
33 ? 00:00:00 \_ ksmd
34 ? 00:00:00 \_ khugepaged
35 ? 00:00:00 \_ crypto
43 ? 00:00:00 \_ kthrotld
45 ? 00:00:00 \_ kmpath_rdacd
46 ? 00:00:00 \_ kaluad
47 ? 00:00:00 \_ kworker/1:1
48 ? 00:00:00 \_ kpsmoused
50 ? 00:00:00 \_ ipv6_addrconf
63 ? 00:00:00 \_ deferwq
94 ? 00:00:00 \_ kauditd
104 ? 00:00:00 \_ kworker/1:2
236 ? 00:00:00 \_ ata_sff
239 ? 00:00:00 \_ mpt_poll_0
241 ? 00:00:00 \_ mpt/0
245 ? 00:00:00 \_ scsi_eh_0
246 ? 00:00:00 \_ scsi_tmf_0
247 ? 00:00:00 \_ kworker/u256:2
248 ? 00:00:00 \_ scsi_eh_1
249 ? 00:00:00 \_ scsi_tmf_1
250 ? 00:00:00 \_ scsi_eh_2
251 ? 00:00:00 \_ scsi_tmf_2
253 ? 00:00:00 \_ ttm_swap
254 ? 00: