TExcelTAD
Home › Terminal & Command Line › Network Commands

Network Commands

"The internet is broken" usually means one specific link in a chain is broken: your network adapter, your router, the DNS lookup, the remote server or one port on it. A handful of terminal commands let you test each link in turn, and they exist on every OS with small differences in name and output.

text
your machine ──► router/gateway ──► internet ──► DNS server ──► remote server:port
  ipconfig / ip     ping gateway      ping 1.1.1.1   nslookup/dig     curl, Test-NetConnection

The playgrounds have no network access, so every example here is a recorded session. Addresses, times and names are sample output — yours will differ.

Your own address: ipconfig, ip, ifconfig

Windows (CMD and PowerShell)

ipconfig shows each adapter's IPv4/IPv6 address, subnet mask and default gateway (your router). ipconfig /all adds the MAC address and DNS servers.

cmd
C:\Users\ada>ipconfig

Windows IP Configuration


Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : home
   IPv6 Address. . . . . . . . . . . : fd12:3456:789a::1c
   IPv4 Address. . . . . . . . . . . : 192.168.1.42
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

PowerShell has object versions: Get-NetIPAddress and the summary Get-NetIPConfiguration. Other useful ones: ipconfig /flushdns (forget cached DNS answers) and ipconfig /release + /renew (ask the router for a new address).

Linux (bash)

Modern Linux uses ip from the iproute2 package; the old ifconfig is often not installed any more.

bash
ada@ubuntu:~$ ip -brief address
lo               UNKNOWN        127.0.0.1/8 ::1/128
wlp2s0           UP             192.168.1.57/24 fd12:3456:789a::57/64
ada@ubuntu:~$ ip route
default via 192.168.1.1 dev wlp2s0 proto dhcp metric 600
192.168.1.0/24 dev wlp2s0 proto kernel scope link src 192.168.1.57

macOS (zsh)

macOS still uses BSD ifconfig; Wi-Fi is usually en0. ipconfig getifaddr en0 prints just the address.

zsh
ada@macbook ~ % ipconfig getifaddr en0
192.168.1.63
ada@macbook ~ % route -n get default | grep gateway
    gateway: 192.168.1.1

Is it reachable? ping

ping sends small "echo request" packets and measures the round trip. Windows sends 4 and stops; Linux and macOS keep going until Ctrl+C, unless you give a count with -c.

cmd
C:\Users\ada>ping -n 2 example.com

Pinging example.com [93.184.215.14] with 32 bytes of data:
Reply from 93.184.215.14: bytes=32 time=89ms TTL=55
Reply from 93.184.215.14: bytes=32 time=88ms TTL=55

Ping statistics for 93.184.215.14:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
bash
ada@ubuntu:~$ ping -c 2 example.com
PING example.com (93.184.215.14) 56(84) bytes of data.
64 bytes from 93.184.215.14: icmp_seq=1 ttl=55 time=88.4 ms
64 bytes from 93.184.215.14: icmp_seq=2 ttl=55 time=88.1 ms

--- example.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms

A good order for troubleshooting: ping your gateway (local network OK?), then a public IP such as 1.1.1.1 (internet OK?), then a name such as example.com (DNS OK?). Note that many servers and firewalls block ping, so "no reply" doesn't always mean "down". tracert (Windows) and traceroute (Linux/macOS) list every router along the way.

Names to addresses: nslookup, dig, Resolve-DnsName

DNS turns names like example.com into IP addresses. nslookup exists everywhere; Linux and macOS also have the more detailed dig, and PowerShell has Resolve-DnsName.

cmd
C:\Users\ada>nslookup example.com
Server:  router.home
Address:  192.168.1.1

Non-authoritative answer:
Name:    example.com
Addresses:  2606:2800:21f:cb07:6820:80da:af6b:8b2c
          93.184.215.14
bash
ada@ubuntu:~$ dig +short example.com
93.184.215.14
ada@ubuntu:~$ dig +short MX gmail.com
5 gmail-smtp-in.l.google.com.
powershell
PS C:\Users\ada> Resolve-DnsName example.com -Type A | Select-Object Name, IPAddress

Name        IPAddress
----        ---------
example.com 93.184.215.14

If ping 1.1.1.1 works but ping example.com says could not find host / Name or service not known, your DNS is the problem, not the connection.

Talking HTTP: curl

curl sends web requests from the terminal: download files, call APIs, check headers. It ships with Windows 10/11, macOS and nearly every Linux.

bash
ada@ubuntu:~$ curl -sI https://example.com | head -n 3
HTTP/2 200
content-type: text/html
etag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134"
ada@ubuntu:~$ curl -s https://api.github.com/repos/git/git | jq .stargazers_count
53870
ada@ubuntu:~$ curl -LO https://example.com/files/data.zip

-s silent, -I headers only, -L follow redirects, -o name / -O save to a file.

On Windows there is a catch: in Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest, which takes completely different options. Type curl.exe to get the real curl in any Windows shell:

powershell
PS C:\Users\ada> curl.exe -sI https://example.com
HTTP/1.1 200 OK
Content-Type: text/html
...
PS C:\Users\ada> (Invoke-RestMethod https://api.github.com/repos/git/git).stargazers_count
53870

Invoke-RestMethod is PowerShell's own tool: it parses JSON into objects for you. (PowerShell 7 removed the curl alias, but curl.exe works in both.)

Open ports and connections: netstat, ss, Test-NetConnection

Which programs are listening, and which connections are open?

cmd
C:\Users\ada>netstat -ano | findstr LISTENING
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1180
  TCP    127.0.0.1:5173         0.0.0.0:0              LISTENING       15220
bash
ada@ubuntu:~$ ss -tlnp
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process
LISTEN  0       511          127.0.0.1:5173        0.0.0.0:*      users:(("node",pid=6120,fd=23))
LISTEN  0       4096           0.0.0.0:22          0.0.0.0:*

ss replaces netstat on Linux (-t TCP, -l listening, -n numeric, -p process). macOS uses netstat -an or lsof -i -P. The last column (PID) tells you which program owns the port, which is exactly what you need when port 5173 is already in use.

To test whether a remote port is open, PowerShell has a built-in tool; on Linux/macOS nc -zv does the same:

powershell
PS C:\Users\ada> Test-NetConnection github.com -Port 443 | Select-Object ComputerName, RemotePort, TcpTestSucceeded

ComputerName RemotePort TcpTestSucceeded
------------ ---------- ----------------
github.com          443             True

Cheat sheet

Task CMD PowerShell Linux macOS
My IP address ipconfig Get-NetIPAddress ip addr ifconfig, ipconfig getifaddr en0
Default gateway ipconfig Get-NetRoute 0.0.0.0/0 ip route route -n get default
Reachability ping -n 4 host Test-Connection host ping -c 4 host ping -c 4 host
Route tracert host Test-NetConnection host -TraceRoute traceroute host traceroute host
DNS lookup nslookup host Resolve-DnsName host dig host dig host
HTTP request curl.exe url Invoke-RestMethod url curl url curl url
Listening ports netstat -ano Get-NetTCPConnection -State Listen ss -tlnp lsof -i -P
Test a port — Test-NetConnection host -Port 443 nc -zv host 443 nc -zv host 443

Learn more: CMD Network Commands, curl & wget, The Network Stack.

Test yourself

ping 1.1.1.1 works, but ping example.com cannot find the host. What is most likely broken?

  1. The network cable
  2. DNS name resolution
  3. The web server
  4. The firewall on example.com

In Windows PowerShell 5.1, how do you run the real curl program?

  1. curl
  2. curl.exe
  3. Invoke-Curl
  4. wget

Which modern Linux command shows listening TCP ports with their processes?

  1. ipconfig /all
  2. ss -tlnp
  3. dig -p
  4. ip route