首先是lsof命令详解:
lsof只能以root权限执行。
lsof命令只能以root的权限执行
用法如下:
1. 显示开启文件abc.txt的进程
lsof abc.txt
2. 显示22端口现在被什么程序占用
lsof -i 22
3. 显示abc进程现在正在打开的文件
lsof -c abc
4. 显示归属gid的进程情况
lsof -g gid
5. 显示指定目录下被进程开启的文件,不会遍历该目录下的所有子目录
lsof +d /usr/local/
6. 显示指定目录下被进程开启的文件,会遍历该目录下得所有子目录
lsof +D /usr/local/
7. 显示使用fd为4的进程
lsof -d 4
8. 不进行域名解析,缺省会进行,比较慢
lsof -n
9. 查看进程号为12的进程打开了哪些文件
lsof -p 12
10. 让lsof重复执行,缺省15s刷新
lsof +|-r [t]
-r, lsof会永远执行,直到被中断
+r, lsof会一直执行,直到没可显示的内容
Example:
查看目前ftp连接的情况:lsof -i tcp@test.com:ftp -r
11. 列出打开文件的大小,如果大小为0,则空
lsof -s
12. 以UID,列出打开的文件
lsof -u username
13. 显示符合条件的进程情况
语法:lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
46 -- IPV4 or IPV6
protocol -- TCP or UDP
hostname -- Internet host name
hostaddr -- IP地址
service -- /etc/service中的service name(可多选)
port -- 端口号(可多选)
Example:
TCP:25 - TCP and port 25
@1.1.1.1 - IP 1.1.1.1
tcp@test.com - TCP protocol, ftp service
下面是netstat命令详解:
netstat可以不在root权限下运行
1. 查看进程占用的端口号
netstat -anp
[root@nbatest ~]# netstat -anp | grep syslog //进程名
udp 0 0 0.0.0.0:514 0.0.0.0:* 31483/syslogd
netstat -anp
[root@nbatest ~]# netstat -anp | grep 514 //port口
udp 0 0 0.0.0.0:514 0.0.0.0:* 31483/syslogd