Have you ever tried to start a server like MySQL and been amazed to see an error that the port is already in use? You rack your brain and try to figure out what it would be to no avail. Sometimes you do a "ps" in Linux and don't even see anything that you think would be using the port. Well, forutantely, there are some tricks to help you find out without doing a reboot. If it is a production server, a reboot may not be an option anyway!
Below are some methods to help. We will start by looking at the "fuser" utility provided with many Linux distros:
fuser -n tcp 80
80/tcp: 1029 1030 1824 1838 1839 1840 1841 13972 14136 14137 14712
This example shows a simple check of everything using port 80. What you see above is a list of PIDs that are using that port. Now we could probably just do a simple "ps" to figure out what it is. You might also want to get more info by doing something like the following to determine from where it is running:
cat /proc/1029/cmdline
/usr/local/apache/bin/httpd-kstart
Now that is useful information! If you have not used "/proc" information before, you are definitely missing out on some great details!
Another extremely helpful utility is "netstat."
netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0: LISTEN 30150/mysqld
tcp 0 0 0.0.0.0:4427 0.0.0.0: LISTEN 27775/drizzled
tcp 0 0 0.0.0.0:3307 0.0.0.0: LISTEN 30147/mysqld
tcp 0 0 0.0.0.0:3310 0.0.0.0: LISTEN 30144/mysqld
tcp 0 0 0.0.0.0:1999 0.0.0.0: LISTEN 30126/mysqlmanager
tcp 0 0 0.0.0.0:111 0.0.0.0: LISTEN 1794/rpcbind
tcp 0 0 127.0.0.1:631 0.0.0.0: LISTEN 2264/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0: LISTEN 2065/sendmail: acce
tcp 0 0 0.0.0.0:47165 0.0.0.0: LISTEN 1814/rpc.statd
tcp 0 0 :::4427 ::: LISTEN 27775/drizzled
tcp 0 0 :::80 ::: LISTEN 1029/httpd
tcp 0 0 :::22 ::: LISTEN 2029/sshd
udp 0 0 0.0.0.0:697 0.0.0.0: 1794/rpcbind
udp 0 0 0.0.0.0:718 0.0.0.0: 1814/rpc.statd
udp 0 0 0.0.0.0:49625 0.0.0.0: 1814/rpc.statd
udp 0 0 0.0.0.0:50147 0.0.0.0: 2241/avahi-daemon:
udp 0 0 0.0.0.0:5353 0.0.0.0: 2241/avahi-daemon:
udp 0 0 0.0.0.0:111 0.0.0.0: 1794/rpcbind
udp 0 0 0.0.0.0:631 0.0.0.0: 2264/cupsd
udp 0 0 192.168.1.3:123 0.0.0.0: 2044/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0: 2044/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0: 2044/ntpd
udp 0 0 fe80::21b:fcff:fe40:123 ::: 2044/ntpd
udp 0 0 ::1:123 ::: 2044/ntpd
udp 0 0 :::123 ::: 2044/ntpd
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 7341 2241/avahi-daemon: /var/run/avahi-daemon/socket
unix 2 [ ACC ] STREAM LISTENING 8211 2366/gam_server @/tmp/fam-root-
unix 2 [ ACC ] STREAM LISTENING 7445 2280/hald @/var/run/hald/dbus-tenNNgKqAh
unix 2 [ ACC ] STREAM LISTENING 6523 1919/pcscd /var/run/pcscd.comm
unix 2 [ ACC ] STREAM LISTENING 1271784 30147/mysqld /mysql/mysql/tmp/mysqld2/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 1271741 30126/mysqlmanager /mysql/mysql/tmp/mysqlmanager.sock
unix 2 [ ACC ] STREAM LISTENING 6591 1949/audispd /var/run/audispd_events
unix 2 [ ACC ] STREAM LISTENING 1271771 30144/mysqld /mysql/mysql/tmp/mysqld10/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 6732 2008/acpid /var/run/acpid.socket
unix 2 [ ACC ] STREAM LISTENING 6620 1964/dbus-daemon /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 7414 2264/cupsd /var/run/cups/cups.sock
unix 2 [ ACC ] STREAM LISTENING 7049 1976/python /var/run/setroubleshoot/setroubleshoot_server
unix 2 [ ACC ] STREAM LISTENING 6179 1794/rpcbind /var/run/rpcbind.sock
unix 2 [ ACC ] STREAM LISTENING 1271790 30150/mysqld /mysql/mysql/tmp/mysqld1/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 7088 2087/gpm /dev/gpmctl
unix 2 [ ACC ] STREAM LISTENING 7440 2280/hald @/var/run/hald/dbus-115QUHVqMh
We use this utility all of the time!
Another utility that is great is "lsof."
lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd 1029 apache 3u IPv6 6263 TCP :http (LISTEN)
httpd 1030 apache 3u IPv6 6263 TCP :http (LISTEN)
rpcbind 1794 rpc 6u IPv4 6181 UDP :sunrpc
rpcbind 1794 rpc 7u IPv4 6184 UDP :uuidgen
rpcbind 1794 rpc 8u IPv4 6185 TCP :sunrpc (LISTEN)
rpc.statd 1814 rpcuser 5u IPv4 6231 UDP :718
rpc.statd 1814 rpcuser 7u IPv4 6242 UDP :49625
rpc.statd 1814 rpcuser 8u IPv4 6245 TCP :47165 (LISTEN)
httpd 1824 root 3u IPv6 6263 TCP :http (LISTEN)
httpd 1838 apache 3u IPv6 6263 TCP :http (LISTEN)
httpd 1839 apache 3u IPv6 6263 TCP :http (LISTEN)
httpd 1840 apache 3u IPv6 6263 TCP :http (LISTEN)
httpd 1841 apache 3u IPv6 6263 TCP :http (LISTEN)
sshd 2029 root 3u IPv6 6778 TCP :ssh (LISTEN)
ntpd 2044 ntp 16u IPv4 6813 UDP :ntp
ntpd 2044 ntp 17u IPv6 6814 UDP :ntp
ntpd 2044 ntp 18u IPv6 6818 UDP localhost6.localdomain6:ntp
ntpd 2044 ntp 19u IPv6 6819 UDP [fe80::21b:fcff:fe40:2fb6]:ntp
ntpd 2044 ntp 20u IPv4 6820 UDP localhost.localdomain:ntp
ntpd 2044 ntp 21u IPv4 6821 UDP 192.168.1.3:ntp
sendmail 2065 root 4u IPv4 6994 TCP localhost.localdomain:smtp (LISTEN)
avahi-dae 2241 avahi 14u IPv4 7351 UDP :mdns
avahi-dae 2241 avahi 15u IPv4 7352 UDP :50147
cupsd 2264 root 4u IPv4 7413 TCP localhost.localdomain:ipp (LISTEN)
cupsd 2264 root 6u IPv4 7416 UDP :ipp
httpd 13972 apache 3u IPv6 6263 TCP :http (LISTEN)
httpd 14136 apache 3u IPv6 6263 TCP :http (LISTEN)
httpd 14137 apache 3u IPv6 6263 TCP :http (LISTEN)
httpd 14712 apache 3u IPv6 6263 TCP :http (LISTEN)
drizzled 27775 mysql 10u IPv6 3542261 TCP :4427 (LISTEN)
drizzled 27775 mysql 12u IPv4 3542262 TCP :4427 (LISTEN)
mysqlmana 30126 mysql 4u IPv4 1271740 TCP :tcp-id-port (LISTEN)
mysqld 30144 mysql 13u IPv4 1271770 TCP :dyna-access (LISTEN)
mysqld 30144 mysql 32u IPv4 5784887 TCP localhost.localdomain:34779->localhost.localdomain:mysql (ESTABLISHED)
mysqld 30147 mysql 16u IPv4 1271783 TCP :opsession-prxy (LISTEN)
mysqld 30150 mysql 14u IPv4 1271789 TCP :mysql (LISTEN)
mysqld 30150 mysql 56u IPv4 5784888 TCP localhost.localdomain:mysql->localhost.localdomain:34779 (ESTABLISHED)
sshd 31040 root 3r IPv6 5787976 TCP 192.168.1.3:ssh->192.168.1.152:bts-x73 (ESTABLISHED)
Anyway, we hope these will be of some value for you. While there are probably several ways to do this, these are a few of our favorites.
Let us know what you think. You may have some even better tricks.