Hurriyet

8 Ocak 2015 Perşembe

Linux / Unix : Port Checking - Port Control - Closing Ports - Açık Portları Görme

To find out which process is listening which port, we can use netstat.

There are various commands but netstat is enough:

Netstat - a command-line tool that displays network connections, routing tables, and a number of network interface statistics.

Fuser - a command line tool to identify processes using files or sockets.

lsof - a command line tool to list open files under Linux / UNIX to report a list of all open files and the processes that opened them.


[applmgr@T1VSTEBSAPP01 bea]$ netstat -tulpn   
 (Not all processes could be identified, non-owned process info  
  will not be shown, you would have to be root to see it all.)  
 Active Internet connections (only servers)  
 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   -            
 tcp    0   0 0.0.0.0:1626        0.0.0.0:*          LISTEN   1545/tnslsnr      
 tcp    0   0 0.0.0.0:5902        0.0.0.0:*          LISTEN   -            
 tcp    0   0 0.0.0.0:5903        0.0.0.0:*          LISTEN   -            
 tcp    0   0 0.0.0.0:5904        0.0.0.0:*          LISTEN   1202/Xvnc        
 tcp    0   0 0.0.0.0:6002        0.0.0.0:*          LISTEN   -            
 tcp    0   0 0.0.0.0:6003        0.0.0.0:*          LISTEN   -            
 tcp    0   0 127.0.0.1:6100       0.0.0.0:*          LISTEN   1121/opmn        
 tcp    0   0 0.0.0.0:6004        0.0.0.0:*          LISTEN   1202/Xvnc        
 tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   -            
 tcp    0   0 :::6200           :::*            LISTEN   1121/opmn        
 tcp    0   0 ::ffff:10.222.8.56:7801   :::*            LISTEN   3132/java        
 tcp    0   0 ::ffff:10.222.8.56:12345  :::*            LISTEN   2994/java        
 tcp    0   0 ::ffff:10.222.8.56:7001   :::*            LISTEN   566/java        
 tcp    0   0 :::4443           :::*            LISTEN   1195/httpd.worker    
 tcp    0   0 :::8000           :::*            LISTEN   1195/httpd.worker    
 tcp    0   0 ::ffff:10.222.8.56:7201   :::*            LISTEN   3174/java        
 tcp    0   0 ::ffff:10.222.8.56:7401   :::*            LISTEN   3152/java        
 tcp    0   0 :::9999           :::*            LISTEN   1195/httpd.worker    
 tcp    0   0 ::ffff:10.222.8.56:7601   :::*            LISTEN   3147/java        
 tcp    0   0 :::6002           :::*            LISTEN   -            
 tcp    0   0 :::6003           :::*            LISTEN   -            
 tcp    0   0 ::1:6100          :::*            LISTEN   1121/opmn        
 tcp    0   0 ::ffff:10.222.8.56:5556   :::*            LISTEN   550/java        
 tcp    0   0 :::6004           :::*            LISTEN   1202/Xvnc        
 tcp    0   0 :::22            :::*            LISTEN   -           

To directly find out the information, the command below will be helpful:

$ netstat -tulpn | grep :80

One other helpful information is that by finding out he process number from the command above, we could verify which program is using this connection. For example port 1626 is used by the PID:1545 and program "tnslsnr". The command above can let us check it even further, although it is written in the output of the netstat command.

 ls -l /proc/1138/exe

How to kill the process listening the open port?

Actually it is quite easy after you find the process id which is listening the port. For example according to the following output, we take the process id next to the service.

 tcp    0   0 ::1:6100          :::*            LISTEN   1121/opmn   

We then kill the process with kill command:

kill -9 1121

After we issue the netstat -a | grep 6100 command again, we wouldn't be  seeing any service that is listening to that port.

References:

1- Finding out which process is listening upon a port: http://www.cyberciti.biz/faq/what-process-has-open-linux-port/

Hiç yorum yok:

Yorum Gönder