DEV Community

Cover image for Terminal Mastery-Networking from the Command Line
Hamed Farazi
Hamed Farazi

Posted on

Terminal Mastery-Networking from the Command Line

07/10 — Networking from the Command Line
Your application works perfectly on your computer.

But suddenly:

  • An API request fails.
  • A website refuses to load.
  • A development server is unreachable.
  • A database connection times out.
  • A domain name cannot be resolved.
  • A port is already in use.

Where should you start?

Before opening a graphical network diagnostic tool, you can investigate many of these problems directly from your terminal.

Command-line networking tools help you understand how your computer communicates with other devices, servers, and services.

In this article, we'll explore:

  • IP addresses and network interfaces
  • Testing network connectivity with ping
  • Understanding DNS and domain resolution
  • Inspecting network routes
  • Checking open ports
  • Testing TCP connections
  • Making HTTP requests with curl
  • Troubleshooting common network problems

We'll cover Windows, macOS, and Linux.

Let's get started.

1. Understanding Basic Networking Concepts

Before running networking commands, let's understand a few essential terms.

IP Address

An IP address identifies a device or network interface on an IP network.

For example:

192.168.1.100
Enter fullscreen mode Exit fullscreen mode

This is an example of a private IPv4 address.

There are two commonly used IP versions:

  • IPv4: 192.168.1.100
  • IPv6: 2001:db8::1

IPv4 uses 32-bit addresses, while IPv6 uses 128-bit addresses.

Public vs. Private IP Addresses

A private IP address is used inside private networks, such as your home or office network.

Common private IPv4 ranges include:

Range Typical use
10.0.0.0/8 Private networks
172.16.0.0/12 Private networks
192.168.0.0/16 Home and office networks

A public IP address is globally routable on the internet.

Your computer may have a private IP address while your router uses a public IP address to communicate with the internet.

DNS

DNS stands for Domain Name System.

It translates domain names into IP addresses.

For example:

example.com
Enter fullscreen mode Exit fullscreen mode

A DNS resolver can return an IP address associated with that domain.

Without DNS, users would need to remember IP addresses instead of convenient domain names.

Ports

An IP address identifies a network interface, while a port identifies a particular communication endpoint.

For example:

localhost:3000
Enter fullscreen mode Exit fullscreen mode

Here:

  • localhost refers to your own computer.
  • 3000 is the port number.

Web development tools commonly use ports such as:

  • 3000 — Commonly used by development servers
  • 5173 — Vite's default development port
  • 8080 — Often used by local web servers
  • 5432 — PostgreSQL's default port
  • 3306 — MySQL's default port

These are conventions, not guarantees. Applications can use different ports.

2. Inspecting Your Network Configuration

The first step in diagnosing a network issue is understanding your current network configuration.

Windows: ipconfig

Open PowerShell or CMD:

ipconfig
Enter fullscreen mode Exit fullscreen mode

This displays information about your network adapters.

For more detailed information:

ipconfig /all
Enter fullscreen mode Exit fullscreen mode

You may see:

IPv4 Address . . . . . . . . . . : 192.168.1.100
Subnet Mask . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . : 192.168.1.1
Enter fullscreen mode Exit fullscreen mode

Let's understand these values:

  • IPv4 Address: Your local IP address.
  • Subnet Mask: Defines the network portion of the IPv4 address.
  • Default Gateway: The router or next-hop device used to reach destinations outside your local network.

Linux: ip

Modern Linux distributions commonly use the ip command.

To display network interfaces and their addresses:

ip addr
Enter fullscreen mode Exit fullscreen mode

A shorter version:

ip a
Enter fullscreen mode Exit fullscreen mode

To display routing information:

ip route
Enter fullscreen mode Exit fullscreen mode

To inspect network interfaces:

ip link
Enter fullscreen mode Exit fullscreen mode

macOS: ifconfig

On macOS, use:

ifconfig
Enter fullscreen mode Exit fullscreen mode

This displays network interfaces and their configuration.

To inspect the default route:

route -n get default
Enter fullscreen mode Exit fullscreen mode

To find the IP address of a specific interface, you can use:

ipconfig getifaddr en0
Enter fullscreen mode Exit fullscreen mode

On many Macs, en0 is the Wi-Fi interface, but interface names can vary.

3. Testing Network Connectivity with ping

One of the most familiar networking commands is:

ping
Enter fullscreen mode Exit fullscreen mode

It sends ICMP Echo Request packets and waits for Echo Replies.

For example:

ping example.com
Enter fullscreen mode Exit fullscreen mode

If the destination responds, you'll see information about the replies and round-trip time.

Example:

64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=24.5 ms
Enter fullscreen mode Exit fullscreen mode

This tells us that an ICMP response was received.

Windows

ping example.com
Enter fullscreen mode Exit fullscreen mode

By default, Windows sends four requests.

To continuously send requests:

ping -t example.com
Enter fullscreen mode Exit fullscreen mode

Stop it with:

Ctrl + C
Enter fullscreen mode Exit fullscreen mode

Linux and macOS

ping example.com
Enter fullscreen mode Exit fullscreen mode

On most Linux and macOS systems, ping continues until you stop it.

Use:

ping -c 4 example.com
Enter fullscreen mode Exit fullscreen mode

This sends four requests.

Testing Your Local Network

You can also ping your router:

ping 192.168.1.1
Enter fullscreen mode Exit fullscreen mode

Replace the address with your actual default gateway.

Important: A Failed Ping Doesn't Always Mean the Server Is Down

Some servers and network configurations block ICMP traffic.

A destination may refuse to respond to ping while still accepting HTTP or HTTPS connections.

Therefore, don't rely on ping as your only connectivity test.

4. Understanding DNS with nslookup

Imagine you try to access an API:

https://api.example.com
Enter fullscreen mode Exit fullscreen mode

But your application reports:

Could not resolve host
Enter fullscreen mode Exit fullscreen mode

This may indicate a DNS resolution problem.

The nslookup command helps investigate it.

Windows

nslookup example.com
Enter fullscreen mode Exit fullscreen mode

Linux and macOS

nslookup example.com
Enter fullscreen mode Exit fullscreen mode

Example output:

Name:    example.com
Address: 93.184.216.34
Enter fullscreen mode Exit fullscreen mode

This shows that the DNS resolver returned an address for the domain.

Query a Specific DNS Server

You can also specify which DNS server to query:

nslookup example.com 8.8.8.8
Enter fullscreen mode Exit fullscreen mode

Here, 8.8.8.8 is a public DNS resolver operated by Google.

Another commonly used public resolver is Cloudflare's:

nslookup example.com 1.1.1.1
Enter fullscreen mode Exit fullscreen mode

This is useful when comparing the results returned by different DNS resolvers.

PowerShell: Resolve-DnsName

PowerShell provides a more native DNS command:

Resolve-DnsName example.com
Enter fullscreen mode Exit fullscreen mode

To request an IPv4 record:

Resolve-DnsName example.com -Type A
Enter fullscreen mode Exit fullscreen mode

To request an IPv6 record:

Resolve-DnsName example.com -Type AAAA
Enter fullscreen mode Exit fullscreen mode

Understanding DNS Record Types

Record Purpose
A Maps a name to an IPv4 address
AAAA Maps a name to an IPv6 address
CNAME Points one name to another name
MX Identifies mail servers
NS Identifies authoritative name servers
TXT Stores text information, often used for verification and email security

If you want more advanced DNS diagnostics, you can also use dig on Linux and macOS:

dig example.com
Enter fullscreen mode Exit fullscreen mode

On some systems, dig may need to be installed separately.

5. Understanding Network Routes with traceroute

Sometimes a connection fails even though your local network appears to work.

The problem might be somewhere between your computer and the destination.

This is where route-tracing tools become useful.

They help reveal the network path that packets take toward a destination.

Windows: tracert

tracert example.com
Enter fullscreen mode Exit fullscreen mode

Linux: traceroute

traceroute example.com
Enter fullscreen mode Exit fullscreen mode

Depending on your Linux distribution, you may need to install traceroute.

macOS: traceroute

traceroute example.com
Enter fullscreen mode Exit fullscreen mode

Example output might look like:

1    192.168.1.1
2    10.20.0.1
3    203.0.113.10
4    example.com
Enter fullscreen mode Exit fullscreen mode

Each line represents a hop along the route.

A hop is typically a router or another network device involved in forwarding traffic.

What Does an Asterisk Mean?

You may see:

3    *    *    *
Enter fullscreen mode Exit fullscreen mode

This means the tool didn't receive a response for those probes within the timeout.

It doesn't necessarily mean the route is broken.

Some routers filter or deprioritize diagnostic traffic while forwarding normal traffic successfully.

Alternative: mtr

On Linux, and on macOS if installed, you may encounter:

mtr example.com
Enter fullscreen mode Exit fullscreen mode

mtr combines aspects of ping and route tracing, providing repeated measurements along the route.

It's useful for investigating intermittent packet loss and latency.

6. Inspecting Network Connections and Open Ports

This is particularly important for developers.

Imagine your application runs on port 3000, but another process is already using it.

Or perhaps you want to know whether your application is listening for incoming connections.

Let's explore the available tools.

Windows: netstat

Run:

netstat -ano
Enter fullscreen mode Exit fullscreen mode

This displays network connections and listening ports.

The options mean:

  • -a: Display all connections and listening ports.
  • -n: Display addresses and port numbers numerically.
  • -o: Display the associated process ID.

To show listening ports:

netstat -ano | findstr LISTENING
Enter fullscreen mode Exit fullscreen mode

To investigate a specific port:

netstat -ano | findstr :3000
Enter fullscreen mode Exit fullscreen mode

Linux: ss

Modern Linux systems commonly use ss:

ss -tuln
Enter fullscreen mode Exit fullscreen mode

Options:

  • -t: TCP sockets
  • -u: UDP sockets
  • -l: Listening sockets
  • -n: Numeric addresses and ports

To include process information:

sudo ss -tulnp
Enter fullscreen mode Exit fullscreen mode

macOS: lsof

On macOS, use:

lsof -i
Enter fullscreen mode Exit fullscreen mode

To display listening TCP ports:

lsof -nP -iTCP -sTCP:LISTEN
Enter fullscreen mode Exit fullscreen mode

To inspect a specific port:

lsof -nP -iTCP:3000 -sTCP:LISTEN
Enter fullscreen mode Exit fullscreen mode

PowerShell: Get-NetTCPConnection

PowerShell provides a useful networking command:

Get-NetTCPConnection
Enter fullscreen mode Exit fullscreen mode

To display listening TCP connections:

Get-NetTCPConnection -State Listen
Enter fullscreen mode Exit fullscreen mode

To inspect a specific port:

Get-NetTCPConnection -LocalPort 3000
Enter fullscreen mode Exit fullscreen mode

To display the process ID:

Get-NetTCPConnection -LocalPort 3000 |
    Select-Object LocalAddress, LocalPort, State, OwningProcess
Enter fullscreen mode Exit fullscreen mode

You can then identify the process:

Get-Process -Id 4820
Enter fullscreen mode Exit fullscreen mode

Replace 4820 with the actual process ID.

7. Testing TCP Connections with PowerShell

Sometimes you don't need to inspect every network connection.

You just want to know whether a specific server and port are reachable.

PowerShell provides:

Test-NetConnection
Enter fullscreen mode Exit fullscreen mode

For example:

Test-NetConnection example.com -Port 443
Enter fullscreen mode Exit fullscreen mode

This tests TCP connectivity to port 443.

A successful result includes:

TcpTestSucceeded : True
Enter fullscreen mode Exit fullscreen mode

A failed result may show:

TcpTestSucceeded : False
Enter fullscreen mode Exit fullscreen mode

You can also test a local development server:

Test-NetConnection localhost -Port 3000
Enter fullscreen mode Exit fullscreen mode

This is particularly useful when debugging:

  • API connectivity
  • Database connections
  • Remote development servers
  • SSH access
  • Web server availability

Important: Test-NetConnection tests TCP connectivity. It doesn't prove that an HTTP application is working correctly or that its API returns valid responses.

8. Making HTTP Requests with curl

Now let's move to one of the most important networking tools for developers.

curl is a command-line tool for transferring data using URLs.

It supports many protocols, including HTTP and HTTPS.

It's extremely useful when working with APIs.

Make a Simple HTTP Request

curl https://example.com
Enter fullscreen mode Exit fullscreen mode

This sends an HTTP request and displays the response body.

Display Response Headers

curl -I https://example.com
Enter fullscreen mode Exit fullscreen mode

The -I option requests headers only using an HTTP HEAD request.

You may see something like:

HTTP/2 200
content-type: text/html
Enter fullscreen mode Exit fullscreen mode

The 200 status code indicates success.

Display Detailed Connection Information

curl -v https://example.com
Enter fullscreen mode Exit fullscreen mode

The verbose option displays additional information about the request and connection.

This is useful when debugging TLS, HTTP, and connection problems.

Be careful when sharing verbose output because it may contain sensitive request information.

Save a Response to a File

curl -o page.html https://example.com
Enter fullscreen mode Exit fullscreen mode

The response body is saved as page.html.

Follow Redirects

Some websites redirect requests to another URL.

Use:

curl -L https://example.com
Enter fullscreen mode Exit fullscreen mode

The -L option tells curl to follow HTTP redirects.

Make a POST Request

When testing APIs, you often need to send data.

For example:

curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Hamed","role":"developer"}'
Enter fullscreen mode Exit fullscreen mode

This sends a JSON request body to the API.

Using curl on Windows

Modern Windows versions include curl.

However, Windows PowerShell has historically defined curl as an alias for Invoke-WebRequest.

To explicitly run the native curl executable, use:

curl.exe https://example.com
Enter fullscreen mode Exit fullscreen mode

You can also use PowerShell's native HTTP commands:

Invoke-WebRequest -Uri "https://example.com"
Enter fullscreen mode Exit fullscreen mode

For API requests:

Invoke-RestMethod -Uri "https://api.example.com/users"
Enter fullscreen mode Exit fullscreen mode

Invoke-RestMethod is particularly useful when working with JSON APIs because it can convert JSON responses into PowerShell objects.

9. Understanding HTTP Status Codes

When you work with APIs, you'll frequently encounter HTTP status codes.

These codes describe the result of an HTTP request.

Here are some important examples:

Status Meaning
200 OK
201 Created
204 No Content
301 Moved Permanently
302 Found
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
405 Method Not Allowed
429 Too Many Requests
500 Internal Server Error
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout

For example:

curl -I https://example.com
Enter fullscreen mode Exit fullscreen mode

If you receive:

HTTP/2 404
Enter fullscreen mode Exit fullscreen mode

The server responded, but the requested resource wasn't found.

If you receive:

HTTP/2 500
Enter fullscreen mode Exit fullscreen mode

The server encountered an internal error.

This distinction matters:

A successful network connection doesn't necessarily mean the application request succeeded.

You need to inspect both the network connection and the HTTP response.

10. Checking Your Network Configuration and DNS Cache

Sometimes your computer has outdated DNS information.

Operating systems maintain DNS caches to reduce repeated lookups.

You can inspect or clear these caches when troubleshooting.

Windows

Display DNS cache entries:

ipconfig /displaydns
Enter fullscreen mode Exit fullscreen mode

Clear the DNS cache:

ipconfig /flushdns
Enter fullscreen mode Exit fullscreen mode

You should see a confirmation that the DNS Resolver Cache was flushed.

macOS

On modern macOS versions, you can request a DNS cache flush with:

sudo dscacheutil -flushcache
Enter fullscreen mode Exit fullscreen mode

You can also restart the system DNS responder:

sudo killall -HUP mDNSResponder
Enter fullscreen mode Exit fullscreen mode

These commands may vary in relevance depending on the macOS version and DNS configuration.

Linux

DNS caching depends on the services installed and enabled on your distribution.

For systems using systemd-resolved:

resolvectl statistics
Enter fullscreen mode Exit fullscreen mode

To flush its cache:

sudo resolvectl flush-caches
Enter fullscreen mode Exit fullscreen mode

Not every Linux system uses systemd-resolved, so these commands may not be available everywhere.

11. Practical Troubleshooting Scenarios

Let's combine what we've learned.

Scenario 1: A Website Doesn't Load

You try to open:

https://example.com
Enter fullscreen mode Exit fullscreen mode

But the browser cannot connect.

Start by checking DNS:

nslookup example.com
Enter fullscreen mode Exit fullscreen mode

Then test basic connectivity:

ping example.com
Enter fullscreen mode Exit fullscreen mode

Remember that ICMP may be blocked.

Next, test HTTPS connectivity from PowerShell:

Test-NetConnection example.com -Port 443
Enter fullscreen mode Exit fullscreen mode

Finally, inspect the HTTP response:

curl -I https://example.com
Enter fullscreen mode Exit fullscreen mode

Each command investigates a different part of the connection.

Scenario 2: Your React Development Server Is Unreachable

Your Vite server starts successfully:

npm run dev
Enter fullscreen mode Exit fullscreen mode

But another device on your network cannot access it.

First, identify your local IP address.

Windows:

ipconfig
Enter fullscreen mode Exit fullscreen mode

Linux:

ip addr
Enter fullscreen mode Exit fullscreen mode

macOS:

ifconfig
Enter fullscreen mode Exit fullscreen mode

Next, verify that your development server is listening on the correct interface.

A server bound only to 127.0.0.1 or localhost generally accepts connections only from the same computer.

For Vite, you can expose the development server to your local network with:

npm run dev -- --host 0.0.0.0
Enter fullscreen mode Exit fullscreen mode

Then, from another device on the same network, try:

http://YOUR_LOCAL_IP:5173
Enter fullscreen mode Exit fullscreen mode

For example:

http://192.168.1.100:5173
Enter fullscreen mode Exit fullscreen mode

You may also need to allow the connection through your firewall.

Security note: Binding to 0.0.0.0 makes the server listen on all available IPv4 interfaces. Only do this on networks you trust, and avoid exposing development servers directly to the public internet.

Scenario 3: An API Request Fails

Your frontend sends a request:

fetch("https://api.example.com/users")
Enter fullscreen mode Exit fullscreen mode

But it fails.

You can investigate the endpoint independently:

curl -v https://api.example.com/users
Enter fullscreen mode Exit fullscreen mode

Then inspect DNS:

nslookup api.example.com
Enter fullscreen mode Exit fullscreen mode

And test the HTTPS port:

Test-NetConnection api.example.com -Port 443
Enter fullscreen mode Exit fullscreen mode

This helps distinguish DNS, TCP, TLS, and HTTP-level problems.

If curl succeeds but the browser request fails, investigate browser-specific issues such as CORS, cookies, authentication, and browser security policies.

12. Common Networking Mistakes

1. Assuming ping proves a website is available

Ping tests ICMP connectivity, not HTTP availability.

2. Confusing an IP address with a port

An IP address identifies a network interface; a port identifies a communication endpoint.

3. Assuming DNS problems are always caused by the website

Your local DNS configuration, resolver, VPN, or network may be responsible.

4. Ignoring firewall rules

A service may be running and listening while a firewall blocks incoming connections.

5. Assuming an HTTP 404 means the network is broken

A 404 is an HTTP response. The server was reachable enough to return that response.

6. Exposing local development servers unnecessarily

Listening on 0.0.0.0 can make an application accessible to other devices on the network.

Understand the security implications before doing it.

13. Practice Exercises

Try these exercises in your own environment.

Exercise 1: Inspect Your Network

  • Display your network interfaces.
  • Find your local IPv4 address.
  • Identify your default gateway.
  • Find your DNS configuration.

Exercise 2: Test Connectivity

  • Ping your default gateway.
  • Ping a public domain.
  • Compare the response times.
  • Try the same test against another destination.

Exercise 3: Investigate DNS

  • Resolve example.com.
  • Query an A record.
  • Query an AAAA record.
  • Compare DNS results using two different resolvers.

Exercise 4: Inspect Open Ports

  • Start a local development server.
  • Find the port it uses.
  • Identify the process listening on that port.
  • Verify whether the port is reachable.

Exercise 5: Test an API

  • Send an HTTP GET request with curl.
  • Display response headers.
  • Inspect the HTTP status code.
  • Send a JSON POST request to a test API.

Quick Reference

Task Linux macOS Windows
Network configuration ip addr ifconfig ipconfig
Test connectivity ping ping ping
DNS lookup nslookup / dig nslookup / dig nslookup / Resolve-DnsName
Trace route traceroute traceroute tracert
Inspect connections ss lsof netstat
Find listening ports ss -tuln lsof -iTCP -sTCP:LISTEN netstat -ano
Test TCP port nc nc Test-NetConnection
HTTP requests curl curl curl.exe
Flush DNS cache resolvectl (if available) dscacheutil ipconfig /flushdns

What's Next?

You've now learned how to inspect network interfaces, test connectivity, resolve domain names, investigate open ports, and troubleshoot HTTP requests.

These skills are incredibly useful when building web applications, debugging APIs, connecting to remote servers, and managing development environments.

In the next article, we'll focus on tools that make everyday development work faster and more efficient.

The Developer's Terminal Toolkit.

We'll explore utilities such as grep, ripgrep, jq, fzf, bat, and other tools that can transform the way you work in the terminal.

See you in Article 08!

Top comments (0)