Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the nc command in Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use the nc command in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Preface

Netcat (nc for short) is a simple but powerful network command line tool that performs any operation related to TCP, UDP, or Unix sockets in Linux.

Netcat can be used for port scanning, port redirection, and starting port listeners; it can also be used to open remote connections and many other things. In addition, you can use it as a backdoor to access the target server.

two。 How to install and use netcat in Linux

To install netcat packages on your system, use the default package manager for the Linux distribution.

Install nc commands based on CentOS or RHEL system

Yum install nc

Install the nc command based on Fedora 22 + or RHEL 8:

Dnf install nc

Install the nc command based on Debian or Ubuntu:

Sudo apt-get install netcat

Once the netcat package is installed, you can learn more about the use of the netcat command in the following example.

3. Use the nc command to scan the port

Netcat can be used for port scanning: find out which ports are open and the service is running on the target server. It can scan one or more or a series of open ports.

Use the nc command to scan the port

Here is an example where the-z option sets nc to scan only for daemons without actually sending them any data. The-v option enables verbose mode, while-w specifies a timeout that cannot establish a connection.

Scan a single port:

Nc-v-w 2 z 192.168.56.1 22

Scan multiple ports:

Nc-v-w 2 z 192.168.56.1 22 80

Scan port range:

Nc-v-w 2 z 192.168.56.1 20-25

4. Transfer files between Linux servers

Netcat allows you to transfer files between two Linux servers, both of which must have nc installed.

For example, to copy an ISO image file from one computer to another and monitor the transfer (using the pv utility), run the following command on the sender (where the ISO file exists).

Install the pv command first, such as the following command on the CentOS system to install the pv program:

Yum-y install pv

This will run nc in listening mode (- l option) on port 3000 and the following command on the server:

The IP address of the server is 192.168.192.134

Tar-zcf-zcwyou.rar | pv | nc-l-p 3000

On the receiver / client computer, run the following command to get the file.

Nc 192.168.192.134 3000 | pv | tar-zxf-

5. Create a command line chat server

You can also use netcat to create a simple command-line messaging server immediately. As with the previous usage example, nc must be installed on both systems used in the chat room.

On one system, run the following command to create a chat server that listens on port 5000.

Nc-l-vv-p 5000

Create a command line chat server

On another system, run the following command to start a chat session on the machine running the messaging server.

Nc 192.168.192.134 5000

Command line chat client

6. Create a basic Web server

Use the-l option of the nc command to create a basic, insecure web server. To demonstrate this, create the html file shown below.

Vim index.html

Add the following HTML line to the file:

Test Page

Serving this file using Netcat Basic HTTP server!

Save the changes in the file and exit.

Create a simple Web server using the nc command

Then provide the HTTP service by running the following command, which will enable the HTTP server to run continuously.

While:; do (echo-ne "HTTP/1.1 200OK\ r\ n"; cat index.html;) | nc-l-p 8080; done

Use the nc command to provide HTTP services

Then open a web browser and you can access the content using the following address:

Http://192.168.1.1:8080

Replace 192.168.1.1 with the real service address

Note that you can stop the Netcat HTTP server by pressing [Ctrl+ C].

7. Resolve Linux server connection issues

Another useful function of netcat is to solve server connection problems. Here, you can use netcat to verify what data the server sends in response to commands issued by the client.

Use the following command to view the home page of example.com:

Printf "GET / HTTP/1.0\ r\ n\ r\ n" | nc text.example.com 80

The output of the above command includes the header sent by the web server and can be used for troubleshooting purposes.

8. Find the services running on the port

You can also use netcat to get port details. In this case, it will tell you what service is running behind a port. For example, to find out what type of service is running behind port 22 on a particular server, run the following command (replace 192.168.56.110 with the IP address of the target server). The option-n means that DNS parsing is disabled.

Nc-v-n 192.168.56.11080

9. Create a stream socket

Netcat also supports the creation of unix stream sockets. The following command creates and listens for a unix stream socket.

Nc-lU / var/tmp/mysocket &

Check:

Ss-lpn | grep "/ var/tmp/"

10. Create a back door

You can also use netcat as a back door. However, this requires more work. If netcat is installed on the target server, you can use it to create a backdoor to get a remote command prompt.

To enable a backdoor, you need netcat to listen on a selected port (for example, port 3001), and then you can connect to this port from your computer as follows.

Run the following command on the remote server where the-d option disables reading from the stdin and the-e option specifies the command to run on the target system.

Nc-L-p 3001-d-e cmd.exe is all the contents of this article entitled "how to use nc commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report