🌐 Linux networking cheatsheet
ifconfig, route and netstat are old; on current Linux systems you use ip, ss and nmcli.
Addresses, links and routes: ip
ip -br a # all interfaces and addresses, one line each
ip a show dev eth0 # one interface, in detail
ip -br link # link state (UP/DOWN) and MAC addresses
ip r # routing table (IPv4)
ip -6 r # routing table (IPv6)
ip route get 1.1.1.1 # which interface and gateway are used for this address?
ip neigh # ARP / neighbour table: who is on the local network
ip -s link show dev eth0 # packet counters and errors
Temporary changes (gone after a reboot):
sudo ip link set dev eth0 up
sudo ip addr add 192.0.2.10/24 dev eth0
sudo ip route add default via 192.0.2.1
Permanent settings: nmcli (NetworkManager)
Fedora and Ubuntu Desktop use NetworkManager; nmcli is its command line.
nmcli device status # all devices and their connections
nmcli connection show # all saved connections
nmcli connection show "Wired connection 1" # every setting of one connection
# a fixed IP address
sudo nmcli connection modify "Wired connection 1" \
ipv4.method manual ipv4.addresses 192.0.2.10/24 \
ipv4.gateway 192.0.2.1 ipv4.dns "1.1.1.1 9.9.9.9"
sudo nmcli connection up "Wired connection 1"
# back to DHCP
sudo nmcli connection modify "Wired connection 1" ipv4.method auto ipv4.addresses "" ipv4.gateway ""
# Wi-Fi
nmcli device wifi list
nmcli device wifi connect "MyNetwork" --ask
Ports and connections: ss
sudo ss -tulpn # which programs listen on which ports (TCP + UDP)
ss -tn state established # open TCP connections
More in list outgoing connections.
DNS
resolvectl status # which DNS servers are used, per interface
resolvectl query example.com
dig example.com +short # from bind-utils (Fedora) / dnsutils (Debian/Ubuntu)
dig MX example.com +short
dig @9.9.9.9 example.com # ask one specific DNS server
Reachability
ping -c 4 192.0.2.1
tracepath example.com # the route, no root needed
mtr example.com # ping + traceroute live (package: mtr)
curl -sI https://example.com # does the web server answer? (headers only)
sudo ethtool eth0 # link speed and duplex of a network card
WireGuard VPN
WireGuard is a small, fast VPN built into the Linux kernel. Every side has a key pair.
sudo apt install wireguard-tools # or: sudo dnf install wireguard-tools
wg genkey | tee private.key | wg pubkey > public.key
chmod 600 private.key
A client configuration, /etc/wireguard/wg0.conf:
[Interface]
PrivateKey = <contents of private.key>
Address = 10.8.0.2/24
DNS = 10.8.0.1
[Peer]
PublicKey = <public key of the server>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24, 192.168.1.0/24 # only these networks go through the VPN
PersistentKeepalive = 25
sudo wg-quick up wg0 # connect
sudo wg show # status, last handshake, traffic
sudo wg-quick down wg0 # disconnect
sudo systemctl enable --now wg-quick@wg0 # connect at every boot
Prefer clicking? Import it into NetworkManager: sudo nmcli connection import type wireguard file /etc/wireguard/wg0.conf.
💡
AllowedIPs = 0.0.0.0/0, ::/0sends all traffic through the VPN; a list of networks (as above) only sends the traffic for those networks.