8 Practical Linux Netcat NC Command Examples
Netcat or nc is a networking utility for debugging and investigating the network.
This utility can be used for creating TCP/UDP connections and investigating them. The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.
1. Netcat in a Server-Client Architecture
The netcat utility can be run in the server mode on a specified port listening for incoming connections.
$ nc -l 2389
Also, it can be used in client mode trying to connect on the port(2389) just opened
$ nc localhost 2389
2. Use Netcat to Transfer Files
The netcat utility can also be used to transfer files. At the client side, suppose we have a file named ‘testfile’ containing :
Now, we run the server as :
$ nc -l 2389 > test
and run the client as :
cat testfile | nc localhost 2389
3. Netcat Supports Timeouts
There are cases when we do not want a connection to remain open forever. In that case, through ‘-w’ switch we can specify the timeout in a connection. So after the seconds specified along with -w flag, the connection between the client and server is terminated.
Server :
nc -l 2389
Client :
$ nc -w 10 localhost 2389
4. Netcat Supports IPV6 Connectivity
$ nc -4 -l 2389
$ nc -4 localhost 2389
$ nc -6 -l 2389
$ nc -6 localhost 2389
5. Disable Reading from STDIN in Netcat
This functionality can be achieved by using the flag -d.
$ nc -d localhost 2389
6. Force Netcat Server to Stay Up
If the netcat client is connected to the server and then after sometime the client is disconnected then normally netcat server also terminates.
This behavior can be controlled by using the -k flag at the server side to force the server to stay up even after the client has disconnected.
$ nc -k -l 2389
8. Use Netcat with UDP Protocol
$ nc -4 -u -l 2389
$ netstat | grep 2389
Read full article from 8 Practical Linux Netcat NC Command Examples
Netcat or nc is a networking utility for debugging and investigating the network.
This utility can be used for creating TCP/UDP connections and investigating them. The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.
1. Netcat in a Server-Client Architecture
The netcat utility can be run in the server mode on a specified port listening for incoming connections.
$ nc -l 2389
Also, it can be used in client mode trying to connect on the port(2389) just opened
$ nc localhost 2389
2. Use Netcat to Transfer Files
The netcat utility can also be used to transfer files. At the client side, suppose we have a file named ‘testfile’ containing :
Now, we run the server as :
$ nc -l 2389 > test
and run the client as :
cat testfile | nc localhost 2389
3. Netcat Supports Timeouts
There are cases when we do not want a connection to remain open forever. In that case, through ‘-w’ switch we can specify the timeout in a connection. So after the seconds specified along with -w flag, the connection between the client and server is terminated.
Server :
nc -l 2389
Client :
$ nc -w 10 localhost 2389
4. Netcat Supports IPV6 Connectivity
$ nc -4 -l 2389
$ nc -4 localhost 2389
$ nc -6 -l 2389
$ nc -6 localhost 2389
5. Disable Reading from STDIN in Netcat
This functionality can be achieved by using the flag -d.
$ nc -d localhost 2389
6. Force Netcat Server to Stay Up
If the netcat client is connected to the server and then after sometime the client is disconnected then normally netcat server also terminates.
This behavior can be controlled by using the -k flag at the server side to force the server to stay up even after the client has disconnected.
$ nc -k -l 2389
8. Use Netcat with UDP Protocol
$ nc -4 -u -l 2389
$ netstat | grep 2389
Read full article from 8 Practical Linux Netcat NC Command Examples