Linux 下查詢埠號由哪個程式執行

Michael_DD發表於2014-11-18
Linux 下查詢埠號由哪個程式執行


可以使用lsof命令查詢
lsof命令引數解釋
 -P : 這個選項約束著網路檔案的埠號到埠名稱的轉換。約束轉換可以使lsof執行得更快一些。
 -n : 這個選項約束著網路檔案的埠號到主機名稱的轉換。約束轉換可以使lsof的執行更快一些。
 -l : 這個選項約束著使用者ID號到登入名的轉換。在登入名的查詢不正確或很慢時,這個選項就很有用。
 +M : 此選項支援本地TCP和UDP埠對映程式的註冊報告。
 -i4 :僅列示IPv4協議下的埠。
 -i6 : 僅列示IPv6協議下的埠。

lnp三個引數對netstat同樣適用

測試:
1. 使用netstat檢視埠
[root@test ~]# netstat -ano
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       Timer
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      off (0.00/0/0)
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      off (0.00/0/0)
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      off (0.00/0/0)
tcp        0      0 127.0.0.1:199               0.0.0.0:*                   LISTEN      off (0.00/0/0)
tcp        0     52 172.25.3.188:22             172.25.1.19:2462            ESTABLISHED on (0.40/0/0)
tcp        0      0 :::64269                    :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 :::111                      :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 :::22                       :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 :::45080                    :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 ::ffff:127.0.0.1:45180      :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 :::45980                    :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 :::5666                     :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 :::63785                    :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:59884   ::ffff:172.25.0.78:1528     ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:57661   ::ffff:172.26.1.11:45036    TIME_WAIT   timewait (40.13/0/0)
tcp        0      0 ::ffff:172.25.3.188:58883   ::ffff:172.26.1.12:45036    TIME_WAIT   timewait (40.14/0/0)
tcp        0      0 ::ffff:172.25.3.188:55898   ::ffff:172.25.3.16:45017    ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:57670   ::ffff:172.26.1.11:45036    ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:57581   ::ffff:172.25.100.34:1528   ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:63958   ::ffff:172.25.3.15:45017    ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:57761   ::ffff:172.25.100.34:1528   ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:57526   ::ffff:172.25.100.34:1528   TIME_WAIT   timewait (14.61/0/0)
tcp        0      0 ::ffff:172.25.3.188:55874   ::ffff:172.25.3.16:45017    TIME_WAIT   timewait (10.12/0/0)
tcp        0      0 ::ffff:172.25.3.188:57630   ::ffff:172.25.100.34:1528   ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:59740   ::ffff:172.25.0.78:1528     TIME_WAIT   timewait (15.34/0/0)
tcp        0      0 ::ffff:172.25.3.188:55315   ::ffff:172.25.100.26:1528   ESTABLISHED off (0.00/0/0)

#可以看到大量埠號,這裡只檢視80
[root@test ~]# netstat -ano | grep 80
tcp        0      0 :::45080                    :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 ::ffff:127.0.0.1:45180      :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 :::45980                    :::*                        LISTEN      off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:57680   ::ffff:172.25.100.34:1528   ESTABLISHED off (0.00/0/0)
tcp        0      0 ::ffff:172.25.3.188:45080   ::ffff:172.26.1.12:50023    TIME_WAIT   timewait (57.87/0/0)


2. 使用lsof檢視45080
#ipv4 上無
[root@test ~]# lsof -Pnl +M -i4 | grep 45080
[root@test ~]#
#ipv6可以檢視到部分程式資訊
[root@test ~]# lsof -Pnl +M -i6 | grep 45080
java    27821     3000   36u  IPv6 1368444      0t0  TCP *:45080 (LISTEN)
java    27821     3000   68u  IPv6 3173691      0t0  TCP 172.25.3.188:45080->172.26.1.11:59835 (ESTABLISHED)
java    27821     3000   69u  IPv6 3173692      0t0  TCP 172.25.3.188:45080->172.26.1.12:50131 (ESTABLISHED)
java    27821     3000   83u  IPv6 3172950      0t0  TCP 172.25.3.188:45080->10.0.6.6:49553 (ESTABLISHED)
java    27821     3000   84u  IPv6 3172951      0t0  TCP 172.25.3.188:45080->10.0.6.6:49554 (ESTABLISHED)
java    27821     3000   85u  IPv6 3172952      0t0  TCP 172.25.3.188:45080->10.0.6.6:49555 (ESTABLISHED)

使用埠45080是ipv6的協議。使用者號:3000   程式號pid:27821

3. 檢視使用者號為3000的使用者是mwuser
[root@test ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nagios:x:500:500::/home/nagios:/bin/bash
mwuser:x:3000:3000::/home/mwuser:/bin/bash
logop:x:3001:3001::/home/logop:/bin/bash

4. 檢視程式號:27821  的程式 tomcat
[root@test ~]# ps -ef | grep 27821
mwuser   27821     1  0 Nov11 ?        00:43:38 /mwbase/jdk/jdk1.7.0_67/jre/bin/java -Djava.util.logging.config.file=/mwbase/tomcat/。。。。
root     31789 31130  0 09:47 pts/0    00:00:00 grep 27821
[root@test ~]#

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29500582/viewspace-1337134/,如需轉載,請註明出處,否則將追究法律責任。

相關文章