Sobes.tech
Middle

How can you view open ports and determine which IP addresses can access them without using the netstat command?

sobes.tech AI

Answer from AI

You can use the ss (socket statistics) utility or lsof (list open files).

Comparison of netstat and ss:

Feature netstat ss
Speed Slower under heavy traffic Faster, works directly with Netlink
Inode information No Yes
Protocol support Limited Wide

Example of using ss:

# Show all listening TCP sockets
ss -tuln

To determine IP addresses from which access to ports is possible, check firewall rules. Usually, iptables or firewalld are used.

Example of viewing iptables rules for the INPUT chain:

# Show all rules in the INPUT chain with line numbers
sudo iptables -L INPUT -n --line-numbers

In the output, look for rules allowing traffic on the relevant ports. These rules may specify the source IP address or subnet (-s).

Example of a rule allowing TCP traffic on port 22 from IP 192.168.1.10:

# Example output of iptables -L INPUT
# 1    ACCEPT tcp -- 192.168.1.10 anywhere tcp dpt:ssh

Example of using firewalld:

# Show active zones
sudo firewall-cmd --get-active-zones

# Show allowed services and ports for a specific zone (e.g., public)
sudo firewall-cmd --zone=public --list-all

The output of firewall-cmd --zone=<zone> --list-all will list allowed services and ports. To determine source IP addresses, look at the "rich rules," which provide more detailed control.

Example of a rich rule allowing access to TCP port 80 from IP 192.168.1.0/24:

# Example output of firewall-cmd --zone=public --list-all (section rich rules)
# rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="80" accept

Thus, to determine open ports and addresses from which access is possible, a combination of socket viewing utilities (ss) and firewall management tools (iptables, firewalld) is used.