networking tcp netcat
61,002
Solution 1
Background
When you're attempting to use nc
in this manner it's continuing to keep the TCP port open, waiting for the destination to acknowledge the receiving of the done request. This is highlighted in the TCP article on Wikipedia.
TIME-WAIT
(either server or client) represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. [According to RFC 793 a connection can stay in TIME-WAIT for a maximum of four minutes known as a MSL (maximum segment lifetime).]
You can see the effects of this when I use nc
similarly:
$ nc -p 8140 -v -n 192.168.1.105 80
Looking at the state of port 8140:
$ netstat -anpt | grep 8140tcp 0 0 192.168.1.3:8140 192.168.1.105:80 TIME_WAIT -
In fact on most Linux systems this TIME_WAIT
is set to 60 seconds.
$ cat /proc/sys/net/ipv4/tcp_fin_timeout60
If you want to see the effect yourself you can use this snippet to watch when the port becomes released.
$ date; nc -p 8140 -v -n 192.168.1.105 80 -w 1; date; \ while netstat -anpt | grep 8140; do date; sleep 10; done; dateTue Mar 25 09:46:59 EDT 2014Connection to 192.168.1.105 80 port [tcp/*] succeeded!Tue Mar 25 09:47:00 EDT 2014tcp 0 0 192.168.1.3:8140 192.168.1.105:80 TIME_WAIT -Tue Mar 25 09:47:00 EDT 2014tcp 0 0 192.168.1.3:8140 192.168.1.105:80 TIME_WAIT -Tue Mar 25 09:47:10 EDT 2014tcp 0 0 192.168.1.3:8140 192.168.1.105:80 TIME_WAIT -Tue Mar 25 09:47:20 EDT 2014tcp 0 0 192.168.1.3:8140 192.168.1.105:80 TIME_WAIT -Tue Mar 25 09:47:30 EDT 2014tcp 0 0 192.168.1.3:8140 192.168.1.105:80 TIME_WAIT -Tue Mar 25 09:47:40 EDT 2014tcp 0 0 192.168.1.3:8140 192.168.1.105:80 TIME_WAIT -Tue Mar 25 09:47:50 EDT 2014Tue Mar 25 09:48:00 EDT 2014$
Method #1 - using nc
The releasing of the port 8140 takes some time to occur. You'll either need to wait until it's been fully released (putting some sleeps in between would be 1 easy way) or by using a different port.
If you just want to see if the port @ host is open or not you could just drop the -p 8140
.
$ nc -zv -n 10.X.X.9 9090-9093
Example
$ nc -zv -n 192.168.1.200 2024-50000 |& grep -v refuConnection to 192.168.1.200 5672 port [tcp/*] succeeded!Connection to 192.168.1.200 35766 port [tcp/*] succeeded!
NOTE: You might be tempted to try adding the -w
option to nc
, which instructs it to only wait a certain period of time. By default nc
will wait forever. So your command would be something like this:
$ nc -p 8140 -zv -n 10.X.X.9 9090 -w 1
However in my testing on a CentOS 5.9 system using 1.84 it still continued to keep the port in use afterwards, so the best you'd be able to do is use -w 60
since that's the shortest amount of time until TIME_WAIT
takes effect.
Method #2 - using nmap
If you want to use a more appropriate app for scanning a set of ports then I'd suggest using nmap
instead.
$ sudo nmap -sS --source-port 8140 -p 9090-9093 10.X.X.9
Example
$ sudo nmap -sS --source-port 8140 -p 80-85 homerStarting Nmap 6.40 ( http://nmap.org ) at 2014-03-24 21:22 EDTNmap scan report for homer (192.168.1.105)Host is up (0.0059s latency).PORT STATE SERVICE80/tcp open http81/tcp closed hosts2-ns82/tcp closed xfer83/tcp closed mit-ml-dev84/tcp closed ctf85/tcp closed mit-ml-devMAC Address: 00:18:51:43:84:87 (SWsoft)Nmap done: 1 IP address (1 host up) scanned in 11.36 seconds
Here I've setup a filter using iptraf
to prove the traffic is going out to these ports using the source port of 8140.
NOTE: Pay special attention to #1 in the diagram, that shows the source port 8140, while #2 shows a couple of my destination ports that I selected, mainly 80 & 83.
References
- Nmap Cheat Sheet
- Bind: Address Already in Use - Or How to Avoid this Error when Closing TCP Connections
Solution 2
I guess it might as well be an answer so it is at least readable:
i=0 ; until { nc -p 8140 -av -n 10.X.X.9 909${i} && [ $((i=i+1)) -gt 4 ]} 2>&- do sleep 1 done
Truth is, @slm's got a point about nmap
- for handling your network and pen-testing it's an indispensable tool and it's definitely worth your while to get familiar with it. But for testing connectivity between only two machines in a 5:1 port config, it might be a little much. Then again, maybe this is a perfect opportunity to learn to use it.
Anyway, if you haven't got it at your disposal, netcat
is extremely flexible. And, perhaps more importantly, nc
doesn't require su root
to do 0 i/o ops as does nmap
.
In the above answer I just use $i
to substitute the last digit of your target port with 0
for your first round and $i
's incremented value thereafter. Because, as I believe, nc
should return you a true
so long as it can bind your local port, the variable $i
will only increment if 8140 is open and nc
can use it to dial out.
$i
is incremented in a test to ensure you don't go beyond the 9094
port range you've specified, at which time it will finally return true
thereby satisfying until
and terminating the loop.
Speaking of which, until
you can bind
the local 8140 -p
ort your loop will sleep 1
second (as @slm recommends) before trying again.
I just drop stderr
with 2>&-
so you don't have to look at couldn't bind... messages spamming your terminal. As written though, this may never be necessary, as sleep
is called each time $i
is incremented as well - excepting the last when the loop breaks anyway - so the 1 second pause between iterations is maybe enough to drop the bound local port and open it again for use in the next iteration.
If I've somehow mistaken the nc
return value and it returns false
even if it can secure 8140 locally but the target port is closed, that is easily handled with a little more specific use of $?
- just let me know.
Share:
61,002
Related videos on Youtube
03 : 18
Unix & Linux: nc: bind failed: Address already in use (2 Solutions!!)
03 : 40
java.net.BindException: Address already in use, JVM already in use,
JavaA2Z
19
06 : 25
Fix "java.net.BindException: Address already in use" Error inside Anypoint Studio/Eclipse
Jason Estevan
7
07 : 52
Fix "java.net.BindException: Address already in use" | bind exception in Java Socket Programming
Comrevo
4
03 : 24
Address already in use bind port Errno EADDRINUSE | How to fix it
Dota Smart Stats
2
Comments
-
saurav 3 months
I am trying to execute
nc
command from a script , my script is executingnc
command on different ports of Destination using the same source port.e.g:
nc -p 8140 -z -v -n 10.X.X.9 9090nc -p 8140 -z -v -n 10.X.X.9 9091nc -p 8140 -z -v -n 10.X.X.9 9092nc -p 8140 -z -v -n 10.X.X.9 9093and so on ...
After the 1st nc execution , for the remaining of all the lines I am getting below mentioned error message.
nc: bind failed: Address already in usenc: bind failed: Address already in usenc: bind failed: Address already in use
Is there any way of avoiding this situation?
- (Video) netcat udp address already in use
saurav over 8 years
Actually I have to do the outbound testing of ports on the source to the destination.In my case source port is 8140 , so i have to test that with 8140 whether it is able to connect or not. Is there any other way in which outbound of source port and inbound of the destination port can be tested ?
-
slm over 8 years
Use
nmap
instead would be my advice.nc
isn't really up to what you're doing IMO. -
mikeserv over 8 years
@slm
nc
could be up to what he wants, you've just gotta know how to tell it what to do. -
saurav over 8 years
Thanks slm for such a wonderful explanation , actually in my case as you mentioned correctly TIME_WAIT value is 60 , so for me right now 1st solution is not feasible because i have around 100+ source and destination ports , that means
(100*100) mins wait
, it is definitely not going to work in my case. Is there anyway to override TIME_WAIT value ? otherwise I have to go with thenmap
solution. -
slm over 8 years
Yes you can change the TIME_WAIT
echo 2 > /proc/sys/net/ipv4/tcp_fin_timeout
, would set it to 2 seconds. Use caution w/ this method though since you're altering the timeout for the entire box. -
saurav over 8 years
Thanks mike for your answer, actually in my case i have around 100+ source and destination ports , that means (100*100) mins wait , it is definitely not going to work in my case. As I have checked the TIME_WAIT value , it is 60 secs , so it will wait for at least 60 secs before releasing the port.
-
saurav over 8 years
yeah correct , I have to just reset it only for the time when my script is executing later on i will reset it to the original value. - thanks
-
mikeserv over 8 years
(Video) Unix & Linux: Failed to bind to '127.0.0.1:6600': Address already in useYeah, that wont work as is. Youll need to do some & backgrounding to do anything like this.
-
mikeserv over 8 years
@tech-idiot you should bind nc to the port you want to dial out with as a proxy service with -l , then direct requests to localhost ports to be proxied out through the one proxy process. Ill update my answer a little later.
-
psusi about 8 years
Actually I believe this is a bug in linux. While it is true that you can not reuse the same src/dst touple while the previous connection is in TIME_WAIT, by changing the destination port you have selected a different, and therefore non conflicting touple.
Recents
Why Is PNG file with Drop Shadow in Flutter Web App Grainy?
How to troubleshoot crashes detected by Google Play Store for Flutter app
Cupertino DateTime picker interfering with scroll behaviour
Why does awk -F work for most letters, but not for the letter "t"?
Flutter change focus color and icon color but not works
How to print and connect to printer using flutter desktop via usb?
Critical issues have been reported with the following SDK versions: com.google.android.gms:play-services-safetynet:17.0.0
Flutter Dart - get localized country name from country code
navigatorState is null when using pushNamed Navigation onGenerateRoutes of GetMaterialPage
Android Sdk manager not found- Flutter doctor error
Flutter Laravel Push Notification without using any third party like(firebase,onesignal..etc)
How to change the color of ElevatedButton when entering text in TextField
FAQs
What is failed binding address already in use? ›
The Error “address already in use” occurred because some process was already running on the same port. So we can resolve the issue just by killing the process. To stop the process, we need the process ID (PID), which we can fetch using the lsof command.
How do I resolve a port already in use Linux? ›- Open a terminal application i.e. shell prompt.
- Run any one of the following command on Linux to see open ports: $ sudo lsof -i -P -n | grep LISTEN. $ sudo netstat -tulpn | grep LISTEN. $ sudo ss -tulpn | grep LISTEN. ...
- For the latest version of Linux use the ss command. For example, ss -tulw.
If bind() causes error 98 (EADDRINUSE) or 10048 (WSAEADDRINUSE), it means you are trying to bind to a local port that is already in use. This is usually caused by one of three reasons: Another program is already using that port. Your program is already running and you've attempted to start it a second time.
How do I fix port binding error? ›The most common reason for Minecraft failed to bind to port error comes from the incorrect IP configuration. If you have entered something besides the “server-ip=” in the server. properties file, you will encounter the “failed to bind to port Minecraft server” error. You can fix the issue by changing the server.
Why is my server failing to bind to port? ›The failed to bind to Port Minecraft server error occurs when a server attempts to use a service port (i.e 25565) that is already in use by another server. Since the majority of Minecraft hosting is Shared Hosting with shared IPs, the only way to make each server accessible is by giving it a unique port.
How do you stop the port which is already in use? ›- Run command-line as an Administrator. Then run the below mention command. netstat -ano | findstr : port number. ...
- Then you execute this command after identify the PID. taskkill /PID typeyourPIDhere /F.
- On MS Windows, select Start > All Programs > Accessories > System Tools >Resource Monitor.
- Expand the Network Tab.
- Move to the section for Listening Ports.
- Look in the Port column and scroll to find entry for port 8080.
- Select the given process and delete/kill the process.
The only way to forcefully close a listening port is to kill the process that is listening on it. Use lsof , netstat , fuser – as root – to find out the process ID. Once the process exits (by itself or by being killed), the kernel will automatically clean up all sockets it had open.
How to install bind on Linux? ›- Prerequisites.
- Install the latest updates.
- Install BIND 9 on the DNS server.
- Edit the named.conf.options file.
- Edit the named.conf.local file.
- Create a directory for your zone files.
- Create the forward zone file.
- Create the reverse zone file.
If the error message you're getting is Address already in use – bind(2) for “127.0. 0.1” port 3000 (Errno::EADDRINUSE) , the fix is pretty easy. It means you still have a server running, though it may be hanging or stuck in a loop.
How do I bind my IP address? ›
Step 1 Open the web browser and type the IP address of the router (default is 192.168. 0.1) into the address bar and then Press Enter. Step 2 Type the username and password in the login page, the default username and password both are admin. Step 3 Click on Firewall->Anti ARP Spoofing->IP-MAC Binding on the left side.
What port does bind use? ›This configuration sets the BIND service to run on default UDP port 53 on the server's localhost and public IP address (172.16. 1.10). At the same time, it allows queries from any host to the BIND DNS server using the Cloudflare DNS 1.1.
Is port binding the same as port forwarding? ›It absolutely is not the same thing. The code you supplied binds the port on the local machine. This action is independent of forwarding all traffic on a port to a specific client. You can bind a port on every client on your network but you can only forward the traffic on that port to a single client on the network.
What is a binding error? ›The binding error occurs because the columns the view is expecting to find in its SELECT statement no longer exist.
Can you bind the same port twice? ›Yes. Multiple listening TCP sockets, all bound to the same port, can co-exist, provided they are all bound to different local IP addresses. Clients can connect to whichever one they need to.
How do I reset my server port? ›- Open the Integration Server Administrator if it is not already open.
- Go to Security > Ports.
- Click Reset to Default Access Settings.
- From the Start menu, click Control Panel, click System and Security, and then click Windows Firewall. ...
- Click Advanced Settings.
- Click Inbound Rules.
- Click New Rule in the Actions window.
- Click Rule Type of Port.
- Click Next.
- On the Protocol and Ports page click TCP.
If you get this error: 'Port 4200 is already in use. Use '–port' to specify a different port', you might have left your Angular development environment without closing the node. js webserver's port. Here's how you can close it without having to reboot your computer or change your application's port.
How do I know if a port is already in use? ›- For Microsoft Windows: netstat -ano | find "1234" | find "LISTEN" tasklist /fi "PID eq 1234"
- For Linux: netstat -anpe | grep "1234" | grep "LISTEN"
So in general, If you get a “port 8080 was already in use” error, then it is certain that another application is already using that port. This is most likely due to bad configuration from your end, running the application multiple times, or not using proper startup and shutdown scripts.
How do I unblock port 8080? ›
- Locate your router's IP address.
- Head over to your router's settings.
- Enter your credentials (username and password).
- Look around for the Port Forwarding tab.
- Open your preferred port—for example, type 8080 to open port 8080.
- Save your settings.
If you see the error "Port 8080 already in use" when starting the Tomcat server, it means that another process is already using port 8080 on your machine. To solve this problem, you can either stop the process that is using port 8080 or change the port number used by the Tomcat server.
How do I know if port 8080 is blocked? ›- Hold down the Windows key and press the R key to open the Run dialog.
- Type “cmd” and click OK in the Run dialog.
- Verify the Command Prompt opens.
- Type “netstat -a -n -o | find "8080"". A list of processes using port 8080 are displayed.
The fuser command combined with the -k (kill) option will end all associated processes that are listening on a TCP or UDP port. Simply provide the port number and type (TCP or UDP) in the fuser command. You can use the lsof command to verify that processes are no longer running on the target port.
How do I unbind an IP address in Linux? ›- To add data addresses to the IPMP group, type the following command: # ifconfig ipmp-interface addif ip-address up.
- To remove an address from the IPMP group, type the following command: # ifconfig ipmp-interface removeif ip-address.
- Launch a Web browser and typer "192.168. ...
- Click "Port Forwarding" under the Advanced section.
- Uncheck the "Enable" box next to each port forwarding rule you wish to disable.
BIND is a nameserver service responsible for performing domain-name-to-IP conversion on Linux-based DNS servers. [root@servera ~] # yum install bind. The BIND package provides the named service. It reads the configuration from the /etc/named and /etc/named.
How to use bind command in Linux? ›The bind command in Linux is a built-in command which comes with the Bash shell. This command is used to assign functions and macros to a key or a sequence of keys. Thus, this command allows us to create hotkeys instead of having to type in the entire command/function/macro name every time you need to use one.
Where is the bind config file? ›The BIND nameserver named server uses the /etc/named. conf file for configuration. All zone files are placed in the /var/named/ directory.
Why is 127.0 0.1 refused to connect? ›If DNS collects too many records or some of the records get corrupt, it may fail to function. Such an issue can result in localhost refusing to connect. The best way to ensure DNS is not preventing the localhost connection is to clear the DNS cache and delete all saved DNS lookup information.
What does bind 127.0 0.1 :: 1 mean? ›
If you bind a socket for receiving data to a specific address you can only receive data sent to this specific IP address. For example, if you bind to 127.0. 0.1 you will be able to receive data from your own system but not from some other system on the local network, because they cannot send data to your 127.0.
What is the difference between bind port and connect port? ›bind() associates the socket with its local address [that's why server side binds, so that clients can use that address to connect to server.] connect() is used to connect to a remote [server] address, that's why is client side, connect [read as: connect to server] is used.
What does it mean to bind a router? ›You can make the outbound traffic to a specified destination leave the router from a specified WAN port through Protocol Binding or Static Routing. Protocol Binding is used to define specific IP addresses or specific application service ports to go through a user-assigned WAN for external connections.
Why do we bind IP addresses? ›MAC-binding prevents unauthorized access to your network, as only those with approved MAC addresses would be allowed entry. If you change your IP or MAC address, you will be unable to access the network. Measures such as these make your network more stable and secure.
How to bind IP address from DHCP? ›- Enter system view.
- Enter DHCP address pool view. dhcp server ip-pool pool-name.
- Configure a static binding. static-bind ip-address ip-address [ mask-length | mask mask ] { client-identifier client-identifier | hardware-address hardware-address [ ethernet | token-ring ] } ...
- (Optional.)
The Address Binding refers to the mapping of computer instructions and data to physical memory locations. Both logical and physical addresses are used in computer memory. It assigns a physical memory region to a logical pointer by mapping a physical address to a logical address known as a virtual address.
What does it mean to bind an IP address? ›The bind parameter specifies the IP address or name of the host to which the protocol handler is bound. bind=<name> Description. Set the value to the IP address or name of the host. If you specify an IP address, it can be in the IPv6 format; for example, 3ffe:307:8:0:260:97ff:fe40:efab.
What does it mean to bind address in socket? ›When a socket has both an IP address and a port number it is said to be 'bound to a port', or 'bound to an address'. A bound socket can receive data because it has a complete address. The process of allocating a port number to a socket is called 'binding'.
What is error 10048 in function bind address already in use? ›This error occurs when your OS cannot allocate new sockets. On Windows the default limitation is 5000 - 1024 open ports (5000 is the default max user port setting and the first 1024 ports are reserved). You can increase the default maximum socket value in the registry.
What does binding property mean? ›noun:plural. properties that enable one molecule to bind to another.
Is your IP tied to your device? ›
The internet knows your IP address because it's assigned to your device and is required to browse the internet. Your IP address changes, though, every time you connect to a different Wi-Fi network or router.
What does it mean to bind a device to a network? ›defined device binding as “a way of coupling two devices by explicitly or implicitly creating a software plus network connection between them.” [10]. Today, most common way of device binding is using wireless communication protocols such as Bluetooth, WiFi, NFC etc.
What does bind mean in networking? ›BIND is an acronym for Berkeley Internet Name Domain. BIND is Domain Name System (DNS) software for computers or services connected to the internet. The system acts as a decentralised categoriser which associates names with various information.
How do you bind a socket to an address? ›To bind a socket
Call the bind function, passing the created socket and sockaddr structure returned from the getaddrinfo function as parameters. Check for general errors. Once the bind function is called, the address information returned by the getaddrinfo function is no longer needed.
...
You can actually trim the method down to:
- Shutdown.
- Close.
- Remove from your list.
What is BIND? Berkeley Internet Name Domain (BIND) is the most popular Domain Name System (DNS) server in use today. It was developed in the 1980s at the University of Berkley and is currently in version 9. BIND is an open source system free to download and use, offered under the Mozilla Public License.
What is network error 10048? ›In short, SQL server error 10048 occurs when the TCP port where SQL server must run is used by other processes.
What is error code 10048? ›The return code 10048 is a Windows return codes that states: Only one usage of each socket address (protocol/network address/port) is normally permitted. The error ANR8208W states that the TCPport specified is already used by another application.
What does failed to bind only one usage of each socket address protocol network address port is normally permitted? ›What does ""Only one usage of each socket address (protocol/network address/port) is normally permitted" mean? This error means all available ports on the machine are being exhausted. By default the OS only has around 4000 ports available that are not reserved by the system.