Linux: find used ports quickly
If you work with Linux most likely you had to troubleshoot why and application is not starting, one reason would be that the port must bind…
If you work with Linux most likely you had to troubleshoot why and application is not starting, one reason would be that the port must bind is already used by another process, lets see how you can easily identify if is already used.
The ss command
The ss command is used to print socket statistics, it is installed by default in most modern Linux distros, the command that has replaced was netstat, i will not explain how ss can be used in general but i will present you an one-liner that can be used as an alias to your bashrc profile for quick investigations.
The one liner
ss -tulpn | grep LISTEN | tr -s " " | cut -d " " -f5 | rev | cut -d ":" -f1 | revThis command will do the following
- ss will print all listening TCP and UDP ports
- grep will print only LISTEN sockets
- tr will squeeze multiple spaces to one
- we select only the fifth column
- we reverse the string to make it easier to get only the port number
- using cut again allows us to select the port number which is
:delimited - we reverse text again
This is the expected output, each number is a port already used by a process.
ss -tulpn | grep LISTEN | tr -s " " | cut -d " " -f5 | rev | cut -d ":" -f1 | rev
9099
10256
45393
53
631
19001
1338
10248
10249
10257
10259
3350
631
16443
3389
25000
10250How to make this one-liner an alias
Open ~/.bashrc using your favorite editor and add the following to the end of the file
alias usedports='ss -tulpn | grep LISTEN | tr -s " " | cut -d " " -f5 | rev | cut -d ":" -f1 | rev'Reload ~/.basrc for changes to take effect
$ source ~/.bashrcUsing the alias is very straight forward, just type the name and press enter
$ usedports
9099
10256
45393
53
631
19001
1338
10248
10249
10257
10259
3350
631
16443
3389
25000
10250I hope you enjoyed this short article!