Linux networking cheat sheet
The chapter on one page: what to remember, the commands to have at your fingertips, and flashcards for quick revision.
The chapter in eight sentences
- A host is identified on the network by an address with a prefix, reaches other networks through a gateway, and asks a DNS server for names.
ip -br addr,ip routeandip neighshow addresses, routes and neighbours;ss -tulpnshows what listens, and on which address.pingproves an address answers; it does not prove that a service works.- NetworkManager separates devices (hardware) from connection profiles (saved settings), stored in
/etc/NetworkManager/system-connections/. nmcli connection add|modify|up|down|deletebuilds persistent configuration;modifyedits the profile andupapplies it;ip addr addis lost at reboot.hostnamectl set-hostnamesets the host's own name; names of other hosts are resolved in the order ofnsswitch.conf:/etc/hosts, then DNS.getent hostsshows what programs will see;digasks DNS only. A stale/etc/hostsline can override DNS.- Troubleshoot upward: link, address, gateway, target IP, name, port. "No route to host" with a working ping suggests a firewall, "Connection refused" means nothing listens there.
Cheat sheet
Inspect
| Command | Does |
|---|---|
ip -br addr, ip -br link | Addresses, links and MACs |
ip route | Routes and default gateway |
ip neigh | Neighbours |
ping -c 2 HOST | Reachability |
tracepath -n ADDR | Path hop by hop |
ss -tulpn | Listening ports and programs |
curl -sS -m 5 URL | Test a service, show the error |
Configure
| Command | Does |
|---|---|
nmcli device status | Devices |
nmcli connection show [NAME] | Profiles / one profile |
nmcli con add type dummy ifname I con-name N ipv4.method manual ipv4.addresses A/24 | New static profile |
nmcli con modify N +ipv4.addresses A/24 ipv4.dns D ipv4.gateway G | Edit the profile |
nmcli con up N / down N / delete N | Apply / remove settings / delete |
nmcli -g PROP con show N | One value |
Names
| Command / file | Does |
|---|---|
hostnamectl, hostnamectl set-hostname N | Show / set the name |
/etc/hosts | Local names, searched first |
/etc/resolv.conf | DNS servers and search domain (managed by NetworkManager) |
/etc/nsswitch.conf | hosts: files dns myhostname |
getent hosts N | What programs resolve |
dig +short N, dig -x ADDR, host N | DNS only |
Flashcards
- What do address, prefix and gateway mean?
- Address: this interface. Prefix: how many bits are the network (/24 = 255.255.255.0). Gateway: the router for other networks.
- Show addresses, routes, listeners?
- ip -br addr · ip route · ss -tulpn
- List devices and connection profiles?
- nmcli device status · nmcli connection show
- Create a static profile on a dummy interface?
- nmcli con add type dummy ifname dummy0 con-name dummy0 ipv4.method manual ipv4.addresses 10.20.30.1/24
- Change a profile and apply it?
- nmcli con modify NAME +ipv4.addresses A/24 · nmcli con up NAME
- Where are NetworkManager profiles stored?
- /etc/NetworkManager/system-connections/*.nmconnection
- Show or set the host name?
- hostnamectl · hostnamectl set-hostname NAME
- Order of name sources?
- nsswitch.conf: files (/etc/hosts), then dns (resolv.conf), then myhostname.
- See what programs resolve a name to, and ask DNS directly?
- getent hosts NAME · dig +short NAME
- "No route to host" with a working ping?
- A firewall rejects the port.
- "Connection refused"?
- Nothing listens on that port or address: check ss -tlnp on the server.
- Troubleshooting order?
- Link, address, gateway, target IP, name, port.