
The nc (Netcat) command in Linux is a versatile networking utility for reading and writing data between networks. The nc command is also referred to as the "Swiss army knife" of networking because it performs various tasks. These networking tasks include transferring files, scanning ports, encrypting data, opening shells, and more.
This article explains how to use the nc command in Linux to administer a system network.
Before you begin:
main and client servers respectively.nc Command SyntaxThe following is a basic nc command syntax:
In the above nc command:
[options]: Includes optional flags that modify the command's behavior[hostname]: Specifies the target host.[port]: Specifies the target port to connect.The nc command in Linux works in two modes:
host and port parameters.host option.Explore the egrep command in Linux to efficiently search for specific text patterns in files.
nc Command OptionsYou can use the following command options when working with the nc command:
nc Command to Perform Tasks in LinuxFollow the steps below to use the nc command to perform basic tasks in Linux, such as creating a chat server, transferring files, and scanning open network ports.
Allow network connection on the custom 12345 TCP port through the default firewall.
Reload the firewall to apply changes.
Create a new chat server using the nc command on the main server which listens for connections on port 12345.
The above command runs the nc command in listen mode using the -l option on the TCP port 12345. The -v option enables verbose mode to display detailed output.
Establish an SSH connection in the client server and connect to the chat server. Replace Server-IP with the main server's public IP address.
Output:
Enter Hello World in the nc command prompt to send a message to the main server.
Verify that the Hello World message displays on the main server, and enter Greetings from Vultr! as a reply.
Press Ctrl + C to exit the nc command prompt.
Output:
Create a new file1.txt file on the main server.
Redirect the file to the nc command to transfer it to any connecting client.
Download the file using the nc command on the client server.
Output:
List files in the working directory on the client server and verify that file1.txt file is available.
Output:
Scan all open ports between 20 and 25 using the z option without sending data to the main server.
Output:
Filter the port results using the grep command to display only open ports.
Output:
Create a new index.html file using a file editor such as nano to set up a basic web application on the main server.
Add the following contents to the file.
Save and close the file.
Serve the web application file using a loop and the nc command.
The above command starts an infinite while loop that listens for connections on the TCP port 12345 and serves the index.html web application file when you access the application using the server's public IP address.
Output:
Run the web application by accessing the main server's IP address on port 12345 using a web browser such as Chrome.
Output:
Test access to a specific port on a domain or server IP using the nc command. For example, test access to the HTTPS port 443 on google.com and specify the z option to disable persistence.
Output:
Send an HTTP request to a domain using the nc command. For example, use the print command to send a request to google.com on the HTTP port 80.
Output:
Explore how the top command in Linux provides real-time monitoring for system performance.
nc Command with CompressionYou can use the nc command with compression tools to speed up remote file transfers. Follow the steps below to use the nc command with compression to transfer files.
Create a new dir directory on the main server.
Add new files to the dir directory.
Switch to the dir directory.
Compress the directory files using tar and send the contents to the nc command.
Run the following command on the client server to receive and decompress the files from the main server.
Output:
You have used the nc command in Linux to perform basic networking tasks, such as transferring files, scanning open ports, and running application files. You can use the nc command to manage and troubleshoot network connections between two or more Linux hosts. For more information and options, run the man nc command to view the nc command's manual page.
0 Comments
Be the first to comment and share your perspective with the community.