Port xxxx-already-in-use-error- Fixed! 🛠
Why Port xxxx was already in use in the first place?
Each computer in the network is identified by an IP address much like ports identify applications or processes running on a given machine. Basically, a port number is a way to identify a specific process to which an internet or other network message is to be forwarded when it arrives at a server. All network-connected devices come equipped with standardized ports that have an assigned number. These numbers are reserved for certain protocols and their associated function. There are 65,535 port numbers, but not all are used.
Restricted port numbers or well-known port numbers are reserved by prominent companies and range from 0 to 1023. If you want to register a specific port number can choose from 1024 to 49151. Software companies typically register these port numbers. Dynamic or private ports ranging from 49152 to 65536 are available for anyone to use.
In another scenario, a port number is assigned temporarily -- for the duration of the request and its completion -- from a range of assigned port numbers. This is called a temporary port number.
Here are some commonly used ports and their associated networking protocols:
- Ports 20 and 21. FTP is used to transfer files between a client and a server.
- Port 22. Secure Shell is one of several tunneling protocols used to build secure network connections.
- Port 25. Simple Mail Transfer Protocol (SMTP) is commonly used for email.
- Port 53. Domain name system (DNS) is a critical process that matches human-readable domain names to machine-readable IP addresses on the modern internet. It helps users load websites and applications without typing in a long list of IP addresses.
- Port 80. HTTP is the protocol that enables the World Wide Web.
- Port 123. Network Time Protocol helps computer clocks sync with each other. It's a vital process in encryption
- Port 179. Border Gateway Protocol (BGP) helps establish efficient routes between the large networks or autonomous systems that make up the internet. These large networks use BGP to broadcast which IP addresses they control.
- Port 443. HTTP Secure (HTTPS) is like HTTP but more secure. All HTTPS web traffic goes straight to port 443. Any network service that uses HTTPS for encryption, such as DNS over HTTPS, also connects directly to this port.
- Port 500. Internet Security Association and Key Management Protocol helps set up secure IP Security
- Port 3389. Remote Desktop Protocol enables users to connect to their desktop computers from another device remotely.
Hypertext Transfer Protocol (HTTP) messages, for example, always go to port 80 -- one of the most commonly used ports.
So [...] when an application wants to use a port, the OS magically bind()
one. However, when multiple applications want to use the same port that creates conflict.
Thus, in general, if you get a port xxxx was already in use
error, then it is certain that another application is already using that port and most likely due to bad configuration from your end!
Fix the error
To solve this, you have two options.
- Try to run the application on a port other than the one you are trying to
bin()
. - Identify and stop the process running on that specific port
Option 2:
Kill the process running on port xxxx
We will be using the lsof
& kill
commands to solve this issue.
lsof
(list open files), which basically used in many Unix-like systems to report a list of all open files and the processes that opened them.
First, let's find the process ID (PID) of the application or service running on port xxxx.
Run sudo lsof -i:xxxx
and sudo lsof -t -i:xxxx
respectively.
lsof -i: xxxx
will show a the whole information about the port you need.
lsof -t -i: xxxx
will give you just the PID number in this port.
- -i — show only internet connections
- -t — show only process ID
- :xxxx — given port; here port 40861
##
#
$ sudo lsof -i:40861
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Appme 267357 errormonkey 7u IPv6 1173868 0t0 TCP ip6-localhost:40861 (LISTEN)
Appme 267357 errormonkey 9u IPv4 1173869 0t0 TCP monkServer:40861 (LISTEN)
###
##
#
$ sudo lsof -t -i:40861
267357
##
##
#
... now we need to kill the port PID number using our kill
command.
##
#
$ sudo kill -9 267357
##
#
$ lsof -i:267357
// nothing to show
###
##
#
kill
: kill the process command
-9
: force to kill
$ sudo kill -9 267357
Tips
You can use the below one-line command to achieve the above same.
$ sudo kill -9 $(sudo lsof -t -i:<PORT>)
Voila!
We hope you have found this post as useful and informative as we do.
Happy day.