Four Methods for querying whether a port is occupied in Linux and four methods for linux
One interview question uses three different methods to check which process occupies 8080. The familiar methods are netstat and lsof, but what else can be done.
1. netstat or ss Command
netstat -anlp | grep 80
2. lsof command
This command is used to view the files occupied by the process.
lsof -i:80
3. fuser command
The fuser command is the opposite of the lsof command to check which process occupies a file. In Linux, everything is a file, so you can view common files, socket files, and file systems. The socket file contains the port number. For example, check port 22.
fuser 22/tcp -v USER PID ACCESS COMMAND22/tcp: root 1329 F.... sshd root 1606 f.... sshd
4. nmap Tool
Nmap always scans ports by default. It is very convenient to scan local ports.
nmap localhostStarting Nmap 5.51 ( http://nmap.org ) at 2018-03-03 18:00 CSTNmap scan report for localhost (127.0.0.1)Host is up (0.0000020s latency).Other addresses for localhost (not scanned): 127.0.0.1Not shown: 998 closed portsPORT STATE SERVICE22/tcp open ssh25/tcp open smtpNmap done: 1 IP address (1 host up) scanned in 0.06 seconds
Note: If you think this article is not bad, please click the recommendation in the lower right corner. Your support can stimulate the author's enthusiasm for writing. Thank you very much!