Skip to content

9. Networking Basics

9.1 Viewing Network Configuration

bash
# View network interface information (recommended)
ip addr show
ip a       # Short form

# View routing table
ip route show
ip r       # Short form

# Legacy commands (gradually being phased out, but still in use)
ifconfig
route -n
bash
$ ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:a1:b2:c3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
       valid_lft 86300sec preferred_lft 86300sec

$ ip r
default via 192.168.1.1 dev eth0 proto dhcp metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 metric 100

9.2 Network Diagnostic Tools

bash
# ping — test connectivity
ping -c 4 google.com

# ss — view socket information (replaces netstat)
ss -tulnp       # t=TCP, u=UDP, l=listening, n=numeric, p=process

# netstat — legacy socket viewer
netstat -tulnp

# curl — HTTP request tool
curl https://httpbin.org/ip
curl -I https://example.com         # Get response headers only
curl -o file.tar.gz https://example.com/file.tar.gz  # Download file

# wget — download tool
wget https://example.com/file.tar.gz
wget -c https://example.com/large.iso  # Resume partial download

# dig / nslookup — DNS lookup
dig example.com
nslookup example.com

# traceroute — trace route
traceroute google.com

# mtr — real-time route tracing
mtr google.com
bash
$ ping -c 4 google.com
PING google.com (142.250.80.46) 56(84) bytes of data.
64 bytes from 142.250.80.46: icmp_seq=1 ttl=116 time=5.23 ms
64 bytes from 142.250.80.46: icmp_seq=2 ttl=116 time=5.45 ms
64 bytes from 142.250.80.46: icmp_seq=3 ttl=116 time=5.12 ms
64 bytes from 142.250.80.46: icmp_seq=4 ttl=116 time=5.34 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 5.120/5.285/5.450/0.123 ms

$ ss -tulnp
Netid State  Recv-Q Send-Q Local Address:Port  Peer Address:Port Process
tcp   LISTEN 0      128          0.0.0.0:22         0.0.0.0:*    users:(("sshd",pid=1234,fd=3))
tcp   LISTEN 0      511          0.0.0.0:80         0.0.0.0:*    users:(("nginx",pid=5678,fd=6))
tcp   LISTEN 0      128             [::]:22            [::]:*    users:(("sshd",pid=1234,fd=4))

$ curl -s https://httpbin.org/ip
{
  "origin": "203.0.113.45"
}

$ dig example.com +short
93.184.216.34

9.3 Firewall Basics (ufw / firewalld)

bash
# Ubuntu — ufw (Uncomplicated Firewall)
sudo ufw status
sudo ufw enable
sudo ufw allow 22/tcp            # Allow SSH
sudo ufw allow 80,443/tcp        # Allow HTTP/HTTPS
sudo ufw allow from 192.168.1.0/24  # Allow LAN access
sudo ufw deny 3306               # Deny MySQL port
sudo ufw delete allow 80/tcp     # Delete rule

# CentOS/RHEL — firewalld
sudo firewall-cmd --state
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

# iptables (low-level tool, foundation of all firewalls)
sudo iptables -L -n -v            # View rules
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -A INPUT -s 10.0.0.0/8 -j DROP
bash
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
80,443/tcp                 ALLOW IN    Anywhere
192.168.1.0/24             ALLOW IN    Anywhere

⚠️ Note: ⚠️ Important: When configuring a firewall on a remote server, always allow the SSH port first, or you'll lock yourself out!