Skip to content

networking

Common networking commands for interfaces, ports, DNS, routing, firewalls and connectivity troubleshooting.

Most Used

Start here for local network details, open ports and connection failures.

Show network interfaces and local IP addresses

Identify network interfaces and the IP addresses assigned to them.

macOS
Terminal window
scutil --nwi
Pretty print
ifconfig | awk '/^[a-zA-Z0-9]/ { iface=$1; sub(/:$/, "", iface) } /inet / { print iface, $2 } /inet6 / && $2 !~ /^fe80:/ { print iface, $2 }'
Linux
Terminal window
ip -brief address show up
Pretty print
ip -brief address show up | awk '{ for (i=3; i<=NF; i++) if ($i ~ /^[0-9a-fA-F:.]+\/[0-9]+$/) print $1, $i }'
Windows
Terminal window
ipconfig
Pretty print
Get-NetIPAddress -AddressFamily IPv4,IPv6 -ErrorAction SilentlyContinue | Where-Object { $_.AddressState -eq 'Preferred' -and $_.IPAddress -notmatch '^127\.|^::1$|^fe80:' } | ForEach-Object { "$($_.InterfaceAlias) $($_.IPAddress)" }

Show listening ports

List TCP ports waiting for incoming connections and the processes using them.

macOS
Terminal window
lsof -nP -iTCP -sTCP:LISTEN
Linux
Terminal window
ss -lntup
Windows
Terminal window
Get-NetTCPConnection -State Listen
Pretty print
Get-NetTCPConnection -State Listen | ForEach-Object { $p = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue; [PSCustomObject]@{ Address = $_.LocalAddress; Port = $_.LocalPort; Process = $p.ProcessName; PID = $_.OwningProcess } } | Sort-Object Port | Format-Table -AutoSize

Check whether a local port is in use

Check a specific local port, replacing 3000 with the port you want to inspect.

macOS
Terminal window
lsof -nP -i :<port>
Example
lsof -nP -i :3000
Linux
Terminal window
ss -lntup 'sport = :<port>'
Example
ss -lntup 'sport = :3000'
Windows
Terminal window
Get-NetTCPConnection -LocalPort <port>
Example
Get-NetTCPConnection -LocalPort 3000

Look up a domain's IP address

Check whether DNS can resolve a hostname to an IP address.

macOSLinux
Terminal window
dig <domain>
Example
dig example.com
Windows
Terminal window
Resolve-DnsName <domain>
Example
Resolve-DnsName example.com

Trace the route to a host

Show the network hops toward a destination; missing hops may simply ignore probes.

macOS
Terminal window
traceroute <host>
Example
traceroute example.com
Linux
Terminal window
tracepath <host>
Example
tracepath example.com
Windows
Terminal window
Test-NetConnection <host> -TraceRoute
Example
Test-NetConnection example.com -TraceRoute

Test a remote port

Check whether a remote host accepts TCP connections on a specific port.

macOSLinux
Terminal window
nc -vz -w 3 <host> <port>
Example
nc -vz -w 3 example.com 443
Windows
Terminal window
Test-NetConnection <host> -Port <port>
Example
Test-NetConnection example.com -Port 443

Inspect an HTTP or HTTPS request

Show DNS, connection, TLS and HTTP details to help locate a URL failure.

macOSLinux
Terminal window
curl -v --connect-timeout 5 --max-time 15 <url>
Example
curl -v --connect-timeout 5 --max-time 15 https://example.com/
Windows
Terminal window
curl.exe -v --connect-timeout 5 --max-time 15 <url>
Example
curl.exe -v --connect-timeout 5 --max-time 15 https://example.com/

Network diagnostics

Troubleshoot and inspect system network configurations.

Show network status

List network interfaces and their current status.

macOS
Terminal window
networksetup -listallhardwareports
Linux
Terminal window
ip -brief link
Windows
Terminal window
Get-NetAdapter

Show interface details

Display detailed information for a specific network interface.

macOS
Terminal window
ifconfig <iface>
Example
ifconfig en0
Linux
Terminal window
ip address show dev <iface>
Example
ip address show dev eth0
Windows
Terminal window
Get-NetAdapter -Name <iface>
Example
Get-NetAdapter -Name Ethernet

Set interface state

Enable or disable a network interface.

macOS
Terminal window
sudo ifconfig <iface> down
Example
sudo ifconfig en0 down
Replace down with up to enable the interface.
Linux
Terminal window
sudo ip link set <iface> down
Example
sudo ip link set eth0 down
Replace down with up to enable the interface.
Windows
Terminal window
Disable-NetAdapter -Name <iface>
Example
Disable-NetAdapter -Name Ethernet
Use Enable-NetAdapter to enable the interface.

macOS
Terminal window
sudo wdutil info
Linux
Terminal window
iw dev
Windows
Terminal window
netsh wlan show interfaces

Show configured DNS servers

Display DNS resolver configuration for the system.

macOS
Terminal window
scutil --dns
Linux
Terminal window
resolvectl status
Requires systemd-resolved; inspect /etc/resolv.conf if it is unavailable.
Windows
Terminal window
Get-DnsClientServerAddress

Capture network packets

Capture packets on a network interface; stop the capture with Ctrl+C.

macOSLinux
Terminal window
sudo tcpdump -i <iface>
Example
sudo tcpdump -i en0
Windows
Terminal window
pktmon start --capture
Run pktmon stop to stop the capture. Use pktmon help for additional capture options.

Firewall

Inspect firewall status and manage inbound traffic rules.

Show UFW status

Display the status and rules of the UFW firewall.

Linux
Terminal window
sudo ufw status verbose

List nftables rules

Display the active nftables ruleset.

Linux
Terminal window
sudo nft list ruleset

Show application firewall status

Check whether the macOS application firewall is enabled.

macOS
Terminal window
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate

Show Windows firewall profiles

Display enabled state and default inbound and outbound actions for each profile.

Windows
Terminal window
Get-NetFirewallProfile | Select-Object Name, Enabled, DefaultInboundAction, DefaultOutboundAction

Allow incoming traffic

Add a rule allowing incoming TCP traffic on a specific port.

Linux
Terminal window
sudo iptables -A INPUT -p tcp --dport <port> -j ACCEPT
Example
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
This changes the active iptables rules and may not persist after reboot.
Windows
Terminal window
New-NetFirewallRule -DisplayName "Allow Port <port>" -Direction Inbound -Protocol TCP -LocalPort <port> -Action Allow
Example
New-NetFirewallRule -DisplayName "Allow Port 22" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow

Block incoming traffic

Add a rule blocking incoming TCP traffic on a specific port.

Linux
Terminal window
sudo iptables -A INPUT -p tcp --dport <port> -j DROP
Example
sudo iptables -A INPUT -p tcp --dport 80 -j DROP
This changes the active iptables rules and may not persist after reboot.
Windows
Terminal window
New-NetFirewallRule -DisplayName "Block Port <port>" -Direction Inbound -Protocol TCP -LocalPort <port> -Action Block
Example
New-NetFirewallRule -DisplayName "Block Port 80" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Block

Routing

Inspect the routing table and add or remove static routes.

Display routing table

Display the system routing table.

macOSLinux
Terminal window
netstat -rn
Windows
Terminal window
route print

Add static route

Add a route to a destination subnet through a gateway.

macOS
Terminal window
sudo route -n add -net <subnet> <gateway>
Example
sudo route -n add -net {{SUBNET_VAR}} {{GATEWAY_IP_ADDRESS_VAR}}
Linux
Terminal window
sudo ip route add <subnet> via <gateway>
Example
sudo ip route add {{SUBNET_VAR}} via {{GATEWAY_IP_ADDRESS_VAR}}
Windows
Terminal window
New-NetRoute -DestinationPrefix <subnet> -NextHop <gateway> -InterfaceAlias <iface>
Example
New-NetRoute -DestinationPrefix {{SUBNET_VAR}} -NextHop {{GATEWAY_IP_ADDRESS_VAR}} -InterfaceAlias Ethernet

nmap

Network discovery, port scanning and service identification.

Scan network for open port

Find devices on a network with a specific port open.

macOSLinuxWindows
Terminal window
nmap -p <port> --open <subnet>
Example
nmap -p 22 --open {{SUBNET_VAR}}

Discover active devices

Discover active devices on a network without scanning ports.

macOSLinuxWindows
Terminal window
nmap -sn <subnet>
Example
nmap -sn {{SUBNET_VAR}}

Detect service versions

Detect services and versions running on a host.

macOSLinuxWindows
Terminal window
nmap -sV <host>
Example
nmap -sV {{CLIENT_IP_VAR}}

Scan specific ports

Scan specific ports on a host.

macOSLinuxWindows
Terminal window
nmap -p <ports> <host>
Example
nmap -p 22,80,443 {{CLIENT_IP_VAR}}

Scan all TCP ports

Scan all TCP ports on a host.

macOSLinuxWindows
Terminal window
nmap -p- <host>
Example
nmap -p- {{CLIENT_IP_VAR}}

Identify operating system

Attempt to identify the operating system of a host.

macOSLinuxWindows
Terminal window
nmap -O <host>
Example
nmap -O {{CLIENT_IP_VAR}}

Comprehensive host scan

Perform OS detection, version detection, script scanning and traceroute.

macOSLinuxWindows
Terminal window
nmap -A <host>
Example
nmap -A {{CLIENT_IP_VAR}}

Scan for vulnerabilities

Check a host for known vulnerabilities using Nmap scripts.

macOSLinuxWindows
Terminal window
nmap --script vuln <host>
Example
nmap --script vuln {{CLIENT_IP_VAR}}