lsof usage tutorial 1

The first is a detailed explanation of the lsof command:

lsof can only be executed with root privileges.

The lsof command can only be executed with root privileges. The
usage is as follows:
1.  Display the process that opens the file abc.txt

lsof abc.txt

 

2.  Show what program is currently occupied by port 22

lsof -i 22

 

3.  Display the files that the abc process is now opening

lsof -c abc

 

4.  Display the process status of the belonging gid

lsof -g gid

 

5.  Display the files opened by the process in the specified directory, without traversing all subdirectories in the directory

lsof +d /usr/local/

 

6.  Display the files opened by the process in the specified directory, and will traverse all subdirectories in the directory

lsof +D /usr/local/

 

7.  Show processes using fd 4

lsof -d 4

 

8.  Do not perform domain name resolution, it will be performed by default, which is relatively slow

lsof -n

 

9.  View which files are opened by the process with process number 12

lsof -p 12

 

10. Let lsof execute repeatedly, default 15s refresh

lsof +|-r [t]

-r, lsof will execute forever until interrupted

+r, lsof will continue to execute until there is nothing to display
Example:

Check the current ftp connection: lsof -i [email protected]:ftp -r

 

11. List the size of the open file, if the size is 0, it is empty

lsof -s

 

12. List open files by UID

lsof -u username

 

13. Display the conditions of the eligible processes
Syntax: lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
46 -- IPV4 or IPV6
protocol -- TCP or UDP
hostname -- Internet host name
hostaddr -- IP address
service -- service name in /etc/service (multiple choices)
port -- port number (multiple choices)
Example:
TCP:25 - TCP and port 25
@1.1.1.1 - IP 1.1.1.1
tcp @test.com - TCP protocol, ftp service

 

The following is a detailed explanation of the netstat command:

netstat can be run without root privileges

1. View the port number occupied by the process
netstat -anp
[root@nbatest ~]# netstat -anp | grep syslog //process name
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  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326458864&siteId=291194637