Network Commands on Linux¶
A list of useful network commands and how to use them.
Commands¶
tcpdump
¶
Use tcpdump to dump all the packets that are coming through a network interface.
Capture all packets on an interface.
ip
¶
Replaces ifconfig
, route
, netstat
, and more.
It's a versatile command for network interface and routing configurations.
ifconfig
¶
Deprecated in favor of ip
.¶
Used for configuring network interfaces.
route
¶
Deprecated in favor of ip route
.¶
Used to view and manipulate the IP routing table.
netstat
¶
Considered deprecated in favor of ss
.¶
Shows network status, listening ports, and routing tables.
ss
¶
Replaces netstat
.
Used to display various network socket statistics.
ping
¶
Sends ICMP ECHO_REQUEST
packets to network hosts.
traceroute
¶
Prints the route packets take to a network host.
mtr
¶
Combines the functionality of traceroute
and ping
. It provides continuous traceroute and ping statistics.
nc
(netcat
)¶
Swiss army knife for TCP/IP. Used for reading/writing across network connections using TCP or UDP.
curl
¶
Tool to transfer data from or to a server. Supports various protocols.
curl https://example.com
curl -o ~/downloaded_file.txt https://example.com/file.txt # Download file.txt
# Silently download file.txt
curl -s -o ~/downloaded_file.txt https://example.com/file.txt
# Silently download file.txt, and be silent if it fails
curl -sf -o ~/downloaded_file.txt https://example.com/file.txt
# Follow redirects (like if the file has moved), and be silent even when it fails
curl -sfL -o ~/downloaded_file.txt https://example.com/file.txt
# Follow redirects (like if the file has moved), fail silently but output error messages if it fails
curl -sfSL -o ~/downloaded_file.txt https://example.com/file.txt
wget
¶
Similar to curl
, used to download files from the web.
wget https://example.com/file.txt # Download file.txt
wget -O remote_file.txt https://example.com/file.txt # save the file to remote_file.txt
dig
¶
DNS lookup utility.
nslookup
¶
Query Internet domain name servers.
dig
is more powerful, but nslookup
is widely used for quick queries.
nsupdate
¶
Used for dynamically updating DNS records. Useful for DDNS (Dynamic DNS) services.
host
¶
DNS lookup utility, simpler than dig
.
iperf3
¶
Network speed testing tool.
One machine (server) needs to be ready to receive packets, while another machine (client) sends packets.
Default port used is 5201
.
Default packet type is TCP. Use -u
for UDP.
-P
: Allow parallel streams.
Choosing Which Tool to Use¶
-
For modern systems, prefer using
ip
overifconfig
androute
, andss
overnetstat
for updated functionalities.- Replacement for
ifconfig
isip addr
- Replacement for
netstat
isss
. - Replacement for
netstat -i
:ip -s link
. - Replacement for
netstat -g
isip maddr
.
- Replacement for
-
DNS Troubleshooting: Use
dig
,host
, andnslookup
for diagnosing DNS issues.dig
is more detailed, whilehost
andnslookup
are more straightforward for quick lookups.
-
Network Testing:
ping
,traceroute
, andmtr
are essential for testing network reachability and path.mtr
provides a more detailed path analysis over time.
-
Data Transfer and Testing:
curl
andwget
are indispensable for downloading files or exploring HTTP APIs.netcat
is invaluable for more low-level TCP/UDP network testing or transferring data.
Files¶
cat /etc/resolv.conf
¶
This file contains the DNS rules for the system.
This is symlinked to /run/systemd/resolve/stub-resolv.conf
on some systems.
You shouldn't edit this file directly.
Using resolvectl status
will display details about the uplink DNS servers that
are currently in use.
/etc/nsswitch.conf
¶
Contains the configuration for the Name Switch Service.
This file is responsible for determining the order in which sources are used to resolve names and look up information, such as:
- Hostname resolution
- User and group information (
passwd
,group
) - Authentication mechanisms
- Network service entries, etc.
The Name Switch Service specifies the order in which name service databases are queried for certain information, like user accounts and hostnames.
It allows the system to determine where to look for this information: local files, DNS (Domain Name System), or network services like NIS and LDAP.
- The first column in this file is the database name.
- The next columns specify service specifications.
files
,db
,systemd
,sss
,nis
- Optional actions to perform if a result is obtained from the previous service.
Example from a rocky linux box:
passwd: files sss systemd
group: files sss systemd
netgroup: sss files
automount: sss files
services: sss files
passwd: files sss systemd
files
Says to look in/etc/passwd
for user account info firstsss
: Then query the SSSD (System Security Services Daemon), typically used with LDAP or FreeIPAsystemd
: Finally check the systemd user database (for runtime or transient user accounts)