Network Basics
Overview
This chapter introduces Linux network configuration and management fundamentals, including network interfaces, IP address configuration, DNS settings, and more.
Network Configuration Viewing
ip Command
ip is the main modern Linux network configuration tool.
bash
# View all network interfaces
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 ...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ...
3: wlan0: <BROADCAST,MCAST,UP,LOWER_UP> mtu 1500 ...
# View IP addresses
$ ip addr
$ ip a
2: eth0: <BROADCAST,MCAST,UP,LOWER_UP>
inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0
inet6 fe80::1/64 scope link
# View specific interface
$ ip addr show eth0
# View routing table
$ ip route
$ ip r
default via 192.168.1.1 dev eth0 proto dhcp
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
# View ARP cache
$ ip neigh
192.168.1.1 dev eth0 lladdr aa:bb:cc:dd:ee:ff REACHABLEifconfig (Traditional Tool)
bash
# Install
$ sudo apt install net-tools
# View all interfaces
$ ifconfig
# View specific interface
$ ifconfig eth0Network Interface Information
bash
# View network interface statistics
$ ip -s link
# View interface details
$ ethtool eth0
# View wireless information
$ iwconfig wlan0Configuring IP Addresses
Temporary Configuration
bash
# Add IP address
$ sudo ip addr add 192.168.1.100/24 dev eth0
# Delete IP address
$ sudo ip addr del 192.168.1.100/24 dev eth0
# Enable interface
$ sudo ip link set eth0 up
# Disable interface
$ sudo ip link set eth0 down
# Add default gateway
$ sudo ip route add default via 192.168.1.1
# Delete default gateway
$ sudo ip route del defaultPermanent Configuration (Netplan - Ubuntu)
Ubuntu 18.04+ uses Netplan for network configuration.
yaml
# /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: trueyaml
# Static IP configuration
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
- 8.8.8.8
- 8.8.4.4bash
# Apply configuration
$ sudo netplan apply
# Test configuration
$ sudo netplan tryPermanent Configuration (NetworkManager)
bash
# View connections
$ nmcli connection show
# View devices
$ nmcli device status
# Configure static IP
$ sudo nmcli connection modify "Wired connection 1" \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8,8.8.4.4" \
ipv4.method manual
# Re-activate connection
$ sudo nmcli connection up "Wired connection 1"
# Configure DHCP
$ sudo nmcli connection modify "Wired connection 1" ipv4.method autoPermanent Configuration (Traditional Method - Debian)
bash
# /etc/network/interfaces
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
# Or DHCP
auto eth0
iface eth0 inet dhcpbash
# Restart network
$ sudo systemctl restart networkingDNS Configuration
/etc/resolv.conf
bash
$ cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.comsystemd-resolved
bash
# View DNS status
$ resolvectl status
# View current DNS
$ resolvectl dns/etc/hosts
Local hostname resolution:
bash
$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 mycomputer
192.168.1.10 server.local server
# Add custom resolution
$ sudo echo "192.168.1.20 myserver" >> /etc/hostsHostname Configuration
bash
# View hostname
$ hostname
$ hostnamectl
# Set hostname
$ sudo hostnamectl set-hostname newhostname
# Or edit file
$ sudo vim /etc/hostnameNetwork Diagnostic Tools
ping - Test Connectivity
bash
# Basic usage
$ ping google.com
PING google.com (142.250.185.206) 56(84) bytes of data.
64 bytes from 142.250.185.206: icmp_seq=1 ttl=115 time=10.5 ms
# Specify count
$ ping -c 4 google.com
# Specify interval
$ ping -i 2 google.com
# Specify packet size
$ ping -s 1000 google.comtraceroute - Route Tracing
bash
# Install
$ sudo apt install traceroute
# Trace route
$ traceroute google.com
# Use ICMP
$ sudo traceroute -I google.com
# Use TCP
$ sudo traceroute -T google.commtr - Comprehensive Diagnosis
bash
# Install
$ sudo apt install mtr
# Run (interactive mode)
$ mtr google.com
# Report mode
$ mtr -r -c 10 google.comdig - DNS Query
bash
# Install
$ sudo apt install dnsutils
# Query A record
$ dig google.com
# Query specific record type
$ dig google.com MX
$ dig google.com NS
$ dig google.com TXT
# Short output
$ dig +short google.com
# Specify DNS server
$ dig @8.8.8.8 google.com
# Reverse query
$ dig -x 8.8.8.8nslookup - DNS Query
bash
$ nslookup google.com
$ nslookup google.com 8.8.8.8host - Simple DNS Query
bash
$ host google.com
$ host -t MX google.comPorts and Connections
ss - Socket Statistics
bash
# View all connections
$ ss
# View listening ports
$ ss -l
# View TCP connections
$ ss -t
# View UDP connections
$ ss -u
# Display process information
$ ss -p
# Display numeric format
$ ss -n
# Common combinations
$ ss -tlnp # TCP listening ports, show process
$ ss -tunap # All TCP/UDP, show processnetstat (Traditional Tool)
bash
# Install
$ sudo apt install net-tools
# View listening ports
$ netstat -tlnp
# View all connections
$ netstat -anp
# View routing table
$ netstat -rlsof - View Port Usage
bash
# View port usage
$ sudo lsof -i :80
$ sudo lsof -i :22
# View specific process's network connections
$ sudo lsof -i -p 1234
# View port usage by user
$ sudo lsof -i -u maxwellNetwork Test Tools
curl - HTTP Request
bash
# GET request
$ curl https://example.com
# Show response headers
$ curl -I https://example.com
# Show detailed information
$ curl -v https://example.com
# POST request
$ curl -X POST -d "key=value" https://example.com
# JSON data
$ curl -X POST -H "Content-Type: application/json" \
-d '{"key":"value"}' https://example.com
# Download file
$ curl -O https://example.com/file.zip
$ curl -o newname.zip https://example.com/file.zip
# Follow redirects
$ curl -L https://example.com
# Save cookies
$ curl -c cookies.txt https://example.com
# Use cookies
$ curl -b cookies.txt https://example.comwget - Download Tool
bash
# Download file
$ wget https://example.com/file.zip
# Specify filename
$ wget -O newname.zip https://example.com/file.zip
# Background download
$ wget -b https://example.com/file.zip
# Resume broken download
$ wget -c https://example.com/file.zip
# Limit download speed
$ wget --limit-rate=1m https://example.com/file.zip
# Recursive download
$ wget -r https://example.com/
# Directory download
$ wget -r https://example.com/ -P /path/to/dirnc (netcat) - Network Swiss Army Knife
bash
# Port scan
$ nc -zv 192.168.1.1 1-1000
# Test port
$ nc -zv google.com 80
# As server
$ nc -l 8080
# As client
$ nc 192.168.1.1 8080
# Transfer file
# Receiver
$ nc -l 8080 > received_file
# Sender
$ nc 192.168.1.1 8080 < file_to_sendFirewall
UFW (Ubuntu)
bash
# Enable firewall
$ sudo ufw enable
# Disable firewall
$ sudo ufw disable
# View status
$ sudo ufw status
$ sudo ufw status verbose
# Allow ports
$ sudo ufw allow 22
$ sudo ufw allow 80/tcp
$ sudo ufw allow 443
$ sudo ufw allow 80
# Allow services
$ sudo ufw allow ssh
$ sudo ufw allow http
$ sudo ufw allow https
# Deny ports
$ sudo ufw deny 23
# Delete rules
$ sudo ufw delete allow 80
# Allow specific IP
$ sudo ufw allow from 192.168.1.100
# Allow subnet
$ sudo ufw allow from 192.168.1.0/24iptables
bash
# View rules
$ sudo iptables -L
$ sudo iptables -L -n -v
# Allow port
$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Deny port
$ sudo iptables -A INPUT -p tcp --dport 23 -j DROP
# Save rules
$ sudo iptables-save > /etc/iptables.rules
# Restore rules
$ sudo iptables-restore < /etc/iptables.rulesNetwork Configuration Files
| File | Description |
|---|---|
/etc/hosts | Local hostname resolution |
/etc/hostname | Hostname |
/etc/resolv.conf | DNS configuration |
/etc/network/interfaces | Network interface configuration (Debian) |
/etc/netplan/*.yaml | Netplan configuration (Ubuntu) |
/etc/sysconfig/network-scripts/ | Network configuration (RHEL) |
Summary
This chapter introduced Linux network basics:
- Viewing configuration:
ip,ifconfig - Configuring network: Netplan, NetworkManager
- DNS configuration: resolv.conf, hosts
- Diagnostic tools:
ping,traceroute,dig - Port viewing:
ss,netstat,lsof - Network testing:
curl,wget,nc - Firewall:
ufw,iptables
Network configuration is an important skill for server administration. Mastering these tools will help you diagnose and solve network problems.
Previous chapter: Service Management
Next chapter: SSH Remote Connection