Linux networking commands cheat sheet
Modern Linux tools for checking and changing network settings. Commands that change addresses, routes, connections or firewall rules need root, so prefix them with sudo. Changes made with ip are lost on reboot; use nmcli or your distribution's network config to make them permanent.
- Covers
- Current iproute2, NetworkManager 1.4x, nftables 1.x, BIND 9 dig and tcpdump 4.99 (Debian/Ubuntu/RHEL)
- Commands
- 57
- Last verified
Interfaces and addresses
| Command | What it does |
|---|---|
ip -br address | List every interface with its state and IP addresses in a short table. Example ip -br aip objects can be abbreviated: a for address, r for route, n for neigh. |
ip -br link | List interfaces with their link state and MAC address. |
ip -s link show dev <interface> | Show link details plus RX and TX packet, error and drop counters. Example ip -s link show dev eth0 |
ip link set dev <interface> up | Bring an interface up. Example ip link set dev eth0 up |
ip link set dev <interface> down | Take an interface down. Example ip link set dev eth1 downWarning: Drops all traffic on that interface; running it on the interface you are connected through cuts off your SSH session. |
ip link set dev <interface> mtu <bytes> | Change the MTU of an interface. Example ip link set dev eth0 mtu 1400 |
ip address add <ip>/<prefix> dev <interface> | Add an IP address to an interface. Example ip address add 192.0.2.10/24 dev eth0 |
ip address del <ip>/<prefix> dev <interface> | Remove one IP address from an interface. Example ip address del 192.0.2.10/24 dev eth0Warning: Removing the address you are connected to drops your session. |
ip address flush dev <interface> | Remove all IP addresses from an interface. Example ip address flush dev eth4 scope globalWarning: Removes every matching address at once with no undo. Without scope global it also removes IPv6 link-local addresses. |
Routes and neighbours
| Command | What it does |
|---|---|
ip route | Show the main routing table. Example ip -6 route-6 shows IPv6 routes. |
ip route add default via <gateway> dev <interface> | Add a default route through a gateway. Example ip route add default via 192.0.2.1 dev eth0 |
ip route add <prefix> via <gateway> | Add a static route to a network. Example ip route add 198.51.100.0/24 via 192.0.2.254Use ip route replace to add or change a route in one step. Add metric <n> to set the preference; lower wins. |
ip route del <prefix> | Remove a route. Example ip route del 198.51.100.0/24Deleting the default route on a remote server cuts off access from other networks. |
ip route get <ip> | Show the route, source address and interface the kernel would use for a destination. Example ip route get 203.0.113.10No packet is sent. |
ip neigh | Show the ARP (IPv4) and NDP (IPv6) neighbour table with entry states. States include REACHABLE, STALE, FAILED and PERMANENT. |
ip neigh flush dev <interface> | Clear neighbour (ARP) entries on an interface so they are learned again. Example ip neigh flush dev eth0 |
Sockets and ports
| Command | What it does |
|---|---|
ss -tulpn | List listening TCP and UDP sockets with port numbers and the owning process. -p needs root to show processes owned by other users. |
ss -s | Print a summary of socket counts by type and state. |
ss -tn state established | List established TCP connections with numeric addresses and ports. |
ss -tn '( dport = :<port> or sport = :<port> )' | List TCP sockets to or from a given port. Example ss -tn '( dport = :443 or sport = :443 )' |
NetworkManager (nmcli)
| Command | What it does |
|---|---|
nmcli general status | Show the overall status of NetworkManager and connectivity. |
nmcli device status | List devices with their type, state and active connection profile. |
nmcli device show <interface> | Show details of a device, including addresses, gateway and DNS. Example nmcli device show eth0 |
nmcli connection show | List connection profiles stored on the system. Example nmcli connection show --active--active lists only profiles in use. Add a profile name to see all its settings. |
nmcli connection up <profile> | Activate a connection profile, applying its saved settings. Example nmcli connection up office-lan |
nmcli connection down <profile> | Deactivate a connection profile. Example nmcli connection down office-lanWarning: Takes the interface offline; over SSH this disconnects you if it is the profile you are using. |
nmcli connection modify <profile> ipv4.addresses <ip>/<prefix> ipv4.gateway <gateway> ipv4.dns <dns> ipv4.method manual | Give a profile a static IPv4 address, gateway and DNS server. Example nmcli connection modify office-lan ipv4.addresses 192.168.10.20/24 ipv4.gateway 192.168.10.1 ipv4.dns 192.168.10.1 ipv4.method manualSaved to the profile only; run nmcli connection up <profile> to apply. Set ipv4.method auto to go back to DHCP. |
nmcli connection modify <profile> +ipv4.dns <dns> | Append a DNS server to a profile without replacing the existing ones. Example nmcli connection modify office-lan +ipv4.dns 192.168.10.2Use -ipv4.dns to remove one. |
nmcli connection add type ethernet con-name <profile> ifname <interface> | Create a new Ethernet connection profile (DHCP unless you set ipv4.method). Example nmcli connection add type ethernet con-name office-lan ifname eth0 |
DNS lookups
| Command | What it does |
|---|---|
dig <name> [<type>] | Query DNS for a name; with no type it looks up the A record. Example dig example.com MX |
dig +short <name> | Print only the answer values. Example dig +short example.com AAAA |
dig @<server> <name> | Send the query to a specific DNS server instead of the system resolver. Example dig @192.0.2.53 example.com |
dig -x <ip> | Do a reverse (PTR) lookup for an IPv4 or IPv6 address. Example dig -x 192.0.2.10 |
dig +trace <name> | Follow the delegation from the root servers down and show the answer from each server. Example dig +trace example.com |
resolvectl status | Show the global and per-link DNS servers and search domains used by systemd-resolved. |
resolvectl query <name-or-ip> | Resolve a name or address through systemd-resolved, as applications see it. Example resolvectl query example.com |
resolvectl flush-caches | Flush the systemd-resolved DNS cache. |
Path and reachability
| Command | What it does |
|---|---|
ping -c <count> <host> | Send a set number of ICMP echo requests and stop. Example ping -c 4 192.0.2.1-4 or -6 forces the IP version; -I <interface> sets the source interface or address. |
ping -M do -s <size> <host> | Test path MTU by sending packets with Don't Fragment set. Example ping -M do -s 1472 192.0.2.11472 bytes of data plus 28 bytes of headers is a 1500-byte IPv4 packet. If it fails, lower the size until it passes. |
traceroute -n <host> | Show each hop to a destination without resolving names. Example traceroute -n 203.0.113.10Uses UDP probes by default. -I uses ICMP echo; -T -p 443 uses TCP SYN to port 443 and needs root. |
tracepath -n <host> | Trace the path and discover the path MTU, without root. Example tracepath -n 203.0.113.10 |
mtr --report --report-cycles <n> <host> | Run several traceroute cycles and print per-hop loss and latency statistics. Example mtr --report --report-cycles 10 --no-dns 203.0.113.10Install the mtr package (mtr-tiny on minimal Debian/Ubuntu). --tcp --port 443 probes with TCP instead of ICMP. |
Packet capture (tcpdump)
| Command | What it does |
|---|---|
tcpdump -D | List the interfaces tcpdump can capture on. |
tcpdump -i <interface> -n | Capture on an interface without converting addresses and port numbers to names. Example tcpdump -i any -nany captures on all interfaces (not in promiscuous mode). Add -c <count> to stop after that many packets. |
tcpdump -i <interface> -n host <ip> | Capture only traffic to or from one host. Example tcpdump -i eth0 -n host 192.0.2.10 |
tcpdump -i <interface> -n '<filter>' | Capture with a combined filter on host, port or protocol. Example tcpdump -i eth0 -n 'src host 192.0.2.10 and port 53'Quote filters that contain spaces or parentheses. |
tcpdump -i <interface> -w <file>.pcap | Write raw packets to a file for Wireshark instead of printing them. Example tcpdump -i eth0 -w capture.pcap 'port 443' |
tcpdump -r <file>.pcap | Read and print packets from a saved capture file. Example tcpdump -n -r capture.pcap |
nftables firewall basics
| Command | What it does |
|---|---|
nft list ruleset | Print the whole firewall ruleset. Example nft list ruleset > /etc/nftables.rulesThe output can be loaded again with nft -f. |
nft -a list table <family> <table> | List one table with each rule's handle number. Example nft -a list table inet filter |
nft add table <family> <table> | Create a table. Example nft add table inet filterThe inet family covers IPv4 and IPv6 together. |
nft add chain <family> <table> <chain> '{ type filter hook input priority 0; policy drop; }' | Create a base chain that filters incoming packets and drops anything not accepted. Example nft add chain inet filter input '{ type filter hook input priority 0; policy drop; }'Warning: With policy drop, incoming traffic including SSH is blocked at once until accept rules exist. On a remote server load the chain and its accept rules together from a file with nft -f. |
nft add rule <family> <table> <chain> ct state established,related accept | Allow return traffic for connections that are already established. Example nft add rule inet filter input ct state established,related accept |
nft add rule <family> <table> <chain> tcp dport 22 accept | Allow incoming SSH. Example nft add rule inet filter input tcp dport 22 acceptUse a set for several ports: tcp dport { 22, 443 } accept. |
nft delete rule <family> <table> <chain> handle <n> | Remove one rule by its handle number. Example nft delete rule inet filter input handle 5Warning: The rule is removed immediately. Find the handle with nft -a list table. |
nft -f <file> | Load a ruleset from a file. Example nft -f /etc/nftables.rulesnft -c -f <file> checks the file without applying it. |
nft flush ruleset | Remove all tables, chains and rules. Warning: Leaves no packet filtering at all, and anything not saved is lost. Save first with nft list ruleset > <file>. |
Official references
Last verified against these pages. Commands marked Warning restart, erase or remove something: run them only in a maintenance window and with a backup.
- ip-address(8) manual page man7.org
- ip-route(8) manual page man7.org
- ss(8) manual page man7.org
- nmcli(1) NetworkManager reference manual networkmanager.dev
- BIND 9 manual pages: dig bind9.readthedocs.io
- tcpdump(1) manual page tcpdump.org
- nft(8) manual page netfilter.org
- nftables wiki: Quick reference wiki.nftables.org
Questions people ask
What replaced ifconfig and netstat on Linux?
The iproute2 tools. Use ip address (or ip -br a) instead of ifconfig, ip route instead of route, ip neigh instead of arp, and ss instead of netstat, for example ss -tulpn to list listening ports and the processes that own them.
How do I see which process is listening on a port in Linux?
Run sudo ss -tulpn. It lists listening TCP and UDP sockets with numeric ports and the process name and PID. Without root, -p only shows processes you own. Filter by port with an expression such as ss -tln '( sport = :443 )'.
How do I set a static IP address on Linux with nmcli?
Run nmcli connection modify <profile> ipv4.addresses 192.168.10.20/24 ipv4.gateway 192.168.10.1 ipv4.dns 192.168.10.1 ipv4.method manual, then nmcli connection up <profile> to apply it. Find the profile name with nmcli connection show. The setting survives reboots because it is saved in the profile.
How do I check which DNS server Linux is using?
On systems with systemd-resolved (Ubuntu and many others) run resolvectl status to see the DNS servers per interface. On NetworkManager systems nmcli device show <interface> also lists them. To test a specific server directly, use dig @<server> example.com.