DEV Community

DevOps Man
DevOps Man

Posted on

🧠 Ultimate Guide to Linux Networking Commands

Master the Linux networking essentials with proper command usage, flag explanations, and common troubleshooting tips. Perfect for SysAdmins, DevOps engineers, and Linux learners.


πŸ–§ Getting Network Interface Information

  1. ifconfig – Interface Configuration (Deprecated but still useful)
ifconfig                   # Show info about active interfaces
ifconfig -a               # Show info about all interfaces (active/inactive)
ifconfig enp0s3           # Show info for specific interface

Enter fullscreen mode Exit fullscreen mode
  • -a: Show all interfaces

  • Displays IPv4, MAC address, TX/RX packets, etc.


  • ip – Modern replacement for ifconfig
ip address show                  # Show all addresses
ip addr show dev enp0s3          # Show specific device
ip -4 address                    # IPv4 only
ip -6 address                    # IPv6 only
ip link show                     # Show MAC and Layer 2 info
ip link show dev enp0s3

Enter fullscreen mode Exit fullscreen mode
  • address or addr: Shows IP info

  • link: Layer 2 (MAC level) info

  • -4: IPv4 only

  • -6: IPv6 only


🧭 Displaying and Setting Routing Information
route – Legacy command

route                    # Show routing table
route -n                 # Show numerical IPs only

Enter fullscreen mode Exit fullscreen mode
  • -n: Don't resolve IP to hostnames

ip route – Modern replacement

ip route show                   # Show routing table
ip route add default via 192.168.0.1
ip route del default

Enter fullscreen mode Exit fullscreen mode

🧰 Network Interface Control

# Bringing interfaces up/down
ifconfig enp0s3 down
ifconfig enp0s3 up

ip link set enp0s3 down
ip link set enp0s3 up

# Assigning IP addresses
ifconfig enp0s3 192.168.0.222/24 up
ip addr add 192.168.0.112/24 dev enp0s3
ip addr del 192.168.0.111/24 dev enp0s3

# Secondary IP using sub-interface
ifconfig enp0s3:1 10.0.0.1/24

# Gateway changes
route del default gw 192.168.0.1
route add default gw 192.168.0.2

ip route del default
ip route add default via 192.168.0.1

# MAC address change
ip link set dev enp0s3 address 08:00:27:51:05:a3

Enter fullscreen mode Exit fullscreen mode

βš™οΈ Netplan – Static Network Config (Ubuntu)

# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: false
      addresses:
        - 192.168.0.20/24
      gateway4: "192.168.0.1"
      nameservers:
        addresses:
          - "8.8.8.8"
          - "8.8.4.4"

Enter fullscreen mode Exit fullscreen mode
sudo systemctl stop NetworkManager
sudo systemctl disable NetworkManager
sudo netplan apply

Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ Network Troubleshooting
Using ping

ping 8.8.8.8                  # Ping Google DNS
ping -c 5 192.168.0.1         # Send 5 packets
ping -i 0.5 google.com        # Interval of 0.5s

Enter fullscreen mode Exit fullscreen mode
  • -c: Count

  • -i: Interval


πŸ› οΈ Troubleshooting Network Issues in Linux
When you're facing network issues, here's a step-by-step checklist to diagnose and fix them.
1️⃣ No Internet?

  • βœ… Check Internet connectivity
ping 8.8.8.8

Enter fullscreen mode Exit fullscreen mode
  • πŸ› οΈ If ping fails, check your gateway configuration.

2️⃣ DNS Not Resolving?

  • βœ… Test DNS resolution
ping google.com
Enter fullscreen mode Exit fullscreen mode
  • πŸ” If this fails but 8.8.8.8 works, check DNS settings:
resolvectl status
Enter fullscreen mode Exit fullscreen mode

3️⃣ Interface Down?

  • πŸ”Ž Check interface status
ip link show
Enter fullscreen mode Exit fullscreen mode
  • 🧰 Bring interface up (example: enp0s3)
ip link set enp0s3 up
Enter fullscreen mode Exit fullscreen mode

πŸ” SSH – Secure Shell

ssh -p 22 user@host                # Connect to host
ssh -l user -p 22 host
ssh -v -p 22 user@host             # Verbose mode

Enter fullscreen mode Exit fullscreen mode
  • -p, --port

  • -l, --login-name

  • -v, --verbose

SSH Daemon Control:

sudo systemctl status sshd
sudo systemctl restart sshd
sudo systemctl enable sshd

Enter fullscreen mode Exit fullscreen mode

πŸ“€ SCP – Secure Copy

scp a.txt user@host:~               # Upload file
scp -P 2222 file.txt user@host:~/   # With custom port
scp -r dir/ user@host:~/            # Recursive directory copy
scp -P 2222 user@host:~/a.txt .     # Download file

Enter fullscreen mode Exit fullscreen mode
  • -P: Port

  • -r: Recursive


πŸ” RSYNC – File Synchronization

rsync -av /src/ /dest/                       # Archive & verbose
rsync -av --delete /src/ /dest/              # Mirror
rsync -av -e ssh /src/ user@host:/dest/      # Over SSH
rsync -av --exclude '*.mp4' /src/ /dest/     # Exclude files

Enter fullscreen mode Exit fullscreen mode
  • -a, --archive

  • -v, --verbose

  • --delete: Mirror deletions

  • -e ssh: Use SSH


🌐 WGET – Web Download Tool

wget https://file.com/sample.iso
wget -c https://file.com/sample.iso             # Resume
wget -P ~/Downloads/ https://file.com/sample    # Set dir
wget --limit-rate=100k -P dir/ URL              # Limit rate
wget -i urls.txt                                # Batch download
wget -b URL && tail -f wget-log                 # Background
wget --mirror --convert-links http://site.com   # Full site

Enter fullscreen mode Exit fullscreen mode

  • -c, --continue

  • -P, --directory-prefix

  • --limit-rate=

  • -b, --background

  • -i, --input-file


πŸ“Š NETSTAT, SS, and LSOF

netstat -tupan          # TCP/UDP active ports
ss -tupan               # Faster alternative
lsof                    # List open files
lsof -u user            # By user
lsof -c sshd            # By command
lsof -iTCP -sTCP:LISTEN # TCP listening ports

Enter fullscreen mode Exit fullscreen mode
  • -t, --tcp

  • -u, --udp

  • -a, --all

  • -p, --program


πŸ” NMAP – Network Scanner

nmap -sS 192.168.0.1               # SYN scan
nmap -sT 192.168.0.1               # TCP connect
nmap -p- 192.168.0.1               # All ports
nmap -sV -p 22,80 192.168.0.1      # Version scan
nmap -O 192.168.0.1                # OS detection
nmap -A 192.168.0.1                # Full scan
nmap -iL hosts.txt -oN report.txt  # Input file + save

Enter fullscreen mode Exit fullscreen mode
  • -sS: SYN scan

  • -sT: TCP connect

  • -sV: Version detection

  • -O: OS detection

  • -A: Aggressive scan

  • -iL: Input from file

  • -oN: Normal output file


Top comments (0)