Why was the netstat tool replaced by ss in modern Linux distributions, and what is the preferred command for checking open and listening ports?
The netstat tool reads network details very slowly through looping through /proc/net/ text files; on high-load servers with hundreds of thousands of open connections, the netstat command may freeze or take minutes. ss uses the fast Netlink interface directly inside the kernel, giving it immediate response. The standard command is: ss -tulpn (t=TCP, u=UDP, l=Listening, p=Process, n=Numeric IPs).
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Compare and use the standard command:
# Check all listening ports with the PID number and program name
sudo ss -tulpn
# Filter sockets listening on web port 443 only
sudo ss -tulpn '( sport = :443 )'
# Check the number of connections in ESTABLISHED state.
ss -t -a state establishedSemantics of flags in ss -tulpn:
-t: Limit the scan to the TCP protocol.-u: Limit the scan to the UDP protocol.-l: Display only listening sockets (Listening) and ignore expired connections.-p: Show the process identifier (PID) and application name associated with the port.-n: Display addresses and ports as abstract numbers (Numeric) without wasting time on DNS queries.
- Forgetting to use the -n flag, forcing the command to perform slow DNS queries to translate every IP address and port number into a text name, causing significant delay.
- It is believed that the process name not appearing in the last column means a defect; the command must be run with sudo privileges to view processes owned by other users.
- What does the TIME_WAIT state mean in TCP connections and how does it affect ephemeral port drain?
- How do you filter the ss output to only display TCP connections associated with a specific port such as 443?
Explain the full DNS resolution process when executing a command like curl https://api.stripe.com on a Linux server.
The process begins with a call to the standard C library function getaddrinfo(). First the system reads the /etc/nsswitch.conf directive file to check the order of sources (usually hosts: files dns). The system first checks the local /etc/hosts file; if it does not find the name, it directs the query to the nameserver registered in /etc/resolv.conf (or the local systemd-resolved service via 127.0.0.53) which sends a recursive DNS request (Recursive Query) to the external DNS server.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Analysis path in Linux:
[Application: curl] ──> getaddrinfo()
│
▼
[/etc/nsswitch.conf]
│
┌──────────────┴──────────────┐
▼ ▼
1. files 2. dns
[/etc/hosts] [/etc/resolv.conf]
│ │
(Found? Return IP) (Systemd-resolved)
│
▼
Upstream DNS Server
(e.g., 8.8.8.8, 1.1.1.1)To permanently modify DNS settings in modern systems:
# Use resolvectl to check DNS servers for each network interface
resolvectl status
# Check DNS response in detail
dig @1.1.1.1 api.stripe.com +trace- Manually modifying the /etc/resolv.conf file on modern systems that rely on systemd-resolved or NetworkManager; the file is overwritten and the modifications are automatically canceled when the service is restarted or the DHCP lease is renewed.
- Ignoring an outdated or incorrect entry in the /etc/hosts file overrides the results of real DNS servers and misleads diagnostics.
- What is the recommended command to manage and modify DNS settings with the systemd-resolved service?
- How do you use dig to inspect A and CNAME records and trace the authorization path via the +trace flag?