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

What are the tips for quickly detecting ports in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the tips for rapid port detection in Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is an open port

The listening port is the network port on which the application listens. The list of listening ports you want to get can usually be queried on the network stack on the system through commands such as ss, netstat, or lsof. Each listening port can be opened or closed (filtered) using a firewall.

In general, an open port is a network port that accepts incoming packets from a remote location.

For example, if you are running a Web server listening on ports 80,443, and open these ports to anyone on the firewall. Using the browser, you will be able to access the Web site hosted on the Web server. In this case, both 80 and 443 are open ports.

Open ports can pose a security risk because attackers can use each open port to exploit vulnerabilities or perform any other type of attack. You should expose only the ports required for application functionality, and then close all other ports.

Use the Nmap command to check the open port

Nmap is a powerful network scanning tool that can scan individual hosts and large networks. It is mainly used for security audit and penetration testing.

Nmap is the preferred tool for port scanning. In addition to port scanning, Nmap can also detect Mac address, operating system type, kernel version, and so on.

From the console, issue the following command to determine which ports are listening for TCP connections from the network:

$sudo nmap-sT-p-10.10.8.8

The-sT option tells Nmap to scan TCP ports, and-p-scan all ports (65535). If not, only 1000 ports will be scanned.

Starting Nmap 7.60 (https://nmap.org) at 2019-07-09 23:10 CEST Nmap scan report for 10.10.8.8 Host is up (0.0012s latency). Not shown: 998 closed ports PORT STATE SERVICE 22/tcp open ssh 80/tcp open http MAC Address: 08 host up 00 host up 27 7 05 15 49 15 23 (Oracle VirtualBox virtual NIC) Nmap done: 1 IP address (1 host up) 0.41 seconds

As shown above, only ports 22, 80, and 8069 are open on the target system.

To scan the UDP port, use-sU instead of-sT:

$sudo nmap-sU-p-10.10.8.8

For more information, visit the Nmap man page and learn about all other powerful features of this tool.

Use the Netcat command to check the open port

Netcat (or nc) is a command-line tool that uses the TCP or UDP protocol to read and write data across network connections.

Use netcat to scan a single port or port range.

For example, to scan for TCP ports open between 20 and 80 on a remote computer with an IP address of 10.10.8.8, you can use the following command:

$nc-z-v 10.10.8.8 20-80

The-z option instructs nc to scan only open ports without sending any data, and-v is used for more details.

The output will look like this:

Nc: connect to 10.10.8.8 port 20 (tcp) failed: Connection refused nc: connect to 10.10.8.8 port 21 (tcp) failed: Connection refused Connection to 10.10.8.8 22 port [tcp/ssh] succeeded!. Connection to 10.10.8.8 80 port [tcp/http] succeeded!

If you only want to print the lines with the above open ports on the screen, you can use the grep command to filter the results.

$nc-z-v 10.10.8.8 20-80 2 > & 1 | grep succeeded Connection to 10.10.8.8 22 port [tcp/ssh] succeeded! Connection to 10.10.8.8 80 port [tcp/http] succeeded!

To scan the UDP port, pass the-u option to the nc command:

$nc-z-v-u 10.10.8.8 20-80 2 > & 1 | grep succeeded

Use Bash pseudo devices to check open ports

Another way to check whether a port is open or closed is to use Bash Shell check / dev/tcp/.. Or / dev/udp/.. The pseudo-device under.

When you execute the command on the / dev/$PROTOCOL/$HOST/$IP pseudo device, Bash opens a TCP or UDP connection to the specified host on the specified port.

The following if..else statement checks whether port 443 is open in kernel.org:

If timeout 5 bash-c'/ dev/null' then echo "Port is open" else echo "Port is closed" fi

The output will look like this:

Port is open

How does the above code work?

The default timeout when using pseudo devices to connect to the port is very long, so we use the timeout command to terminate the test command after 5 seconds. If a kernel.org port connection is established, the 443 test command returns true. You can also use the for loop to check the specified port range:

For PORT in {20... 80}; do timeout 1 bash-c "/ dev/null" & & echo "port $PORT is open" done

The output will look like this:

Port 22 is open port 80 is open "what are the tips for quickly detecting ports in Linux"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report