Find the process using a given port
This article shows how to find the process using a given port or range of ports on *nix systems.
Many a times when running socket based applications you will find the application failing with socket bind exception with a message saying address is already in use. That indicates that some other process is already using the port you are trying to use.
Without some useful utilities it would be difficult to find who is using your port. On *nix systems, there comes a command called lsof which can help you out. It may not always be installed out of the box in which case you may explicitly install it. To find the process that is using your port run the following command:
1 2 |
lsof –i :portnumber |
Below is a sample output showing the pid of the process that is using port number 17565.
1 2 3 4 5 |
$ lsof -i :17565 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Dropbox 375 XYZ 38u IPv4 0xf7251eab7cf04277 0t0 UDP *:17565 Dropbox 375 XYZ 41u IPv4 0xf7251eab87db9127 0t0 TCP *:17565 (LISTEN) |
You may also specify a protocol filter TCP or UDP ahead of the port number as follows.
1 2 3 4 5 6 7 8 |
$lsof –i TCP:17565 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Dropbox 375 XYZ 41u IPv4 0xf7251eab87db9127 0t0 TCP *:17565 (LISTEN) $ lsof -i UDP:17565 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Dropbox 375 XYZ 38u IPv4 0xf7251eab7cf04277 0t0 UDP *:17565 |
You may also specify a port range instead of exact port numbers. It will list all the processes that are using the given range of ports.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$ lsof -i TCP:1-50000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Microsoft 286 XYZ 36u IPv4 0xf7251eab7ded5857 0t0 TCP local.lan:54951->74.125.130.108:imaps (ESTABLISHED) Microsoft 286 XYZ 37u IPv4 0xf7251eab7df496b7 0t0 TCP local.lan:55006->74.125.68.109:imaps (ESTABLISHED) Microsoft 286 XYZ 42u IPv4 0xf7251eab7df146b7 0t0 TCP local.lan:55221->74.125.130.109:imaps (ESTABLISHED) Microsoft 286 XYZ 43u IPv4 0xf7251eab7ded9517 0t0 TCP local.lan:55247->74.125.130.108:imaps (ESTABLISHED) Microsoft 286 XYZ 52u IPv4 0xf7251eab869e5b97 0t0 TCP local.lan:55254->imap413.yahoo.com:imaps (ESTABLISHED) Microsoft 286 XYZ 54u IPv4 0xf7251eab7df4a857 0t0 TCP local.lan:54774->74.125.130.108:imaps (ESTABLISHED) Microsoft 286 XYZ 55u IPv4 0xf7251eab7df64517 0t0 TCP local.lan:55114->fast.nseasy.com:imaps (ESTABLISHED) Microsoft 286 XYZ 57u IPv4 0xf7251eab7dedc127 0t0 TCP local.lan:55244->74.125.130.108:imaps (ESTABLISHED) Microsoft 286 XYZ 59u IPv4 0xf7251eab7df14f87 0t0 TCP local.lan:55251->74.125.130.108:imaps (ESTABLISHED) Dropbox 375 XYZ 22u IPv4 0xf7251eab7edbf857 0t0 TCP local.lan:54773->nt-jc.dropbox.com:http (ESTABLISHED) Dropbox 375 XYZ 38u IPv4 0xf7251eab7cf04277 0t0 UDP *:17565 Dropbox 375 XYZ 41u IPv4 0xf7251eab87db9127 0t0 TCP *:17565 (LISTEN) Google 377 XYZ 93u IPv4 0xf7251eab7df63b97 0t0 TCP local.lan:55262->ec2.amazonaws.com:http (CLOSE_WAIT) Google 377 XYZ 109u IPv4 0xf7251eab7df6b517 0t0 TCP local.lan:55255->bome100.net:http (ESTABLISHED) Google 377 XYZ 140u IPv4 0xf7251eab7df4c2c7 0t0 TCP local.lan:55257->103.20.92.129:https (ESTABLISHED) Google 377 XYZ 187u IPv4 0xf7251eab7df6e9f7 0t0 TCP local.lan:54672->74.125.130.95:https (ESTABLISHED) Google 377 XYZ 232u IPv4 0xf7251eab7df679f7 0t0 TCP local.lan:54958->maae100.net:https (ESTABLISHED) Google 377 XYZ 245u IPv4 0xf7251eab7edbef87 0t0 TCP local.lan:54960->maae100.net:http (ESTABLISHED) |