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
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
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
Here:
-
localhostrefers to your own computer. -
3000is 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
This displays information about your network adapters.
For more detailed information:
ipconfig /all
You may see:
IPv4 Address . . . . . . . . . . : 192.168.1.100
Subnet Mask . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . : 192.168.1.1
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
A shorter version:
ip a
To display routing information:
ip route
To inspect network interfaces:
ip link
macOS: ifconfig
On macOS, use:
ifconfig
This displays network interfaces and their configuration.
To inspect the default route:
route -n get default
To find the IP address of a specific interface, you can use:
ipconfig getifaddr en0
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
It sends ICMP Echo Request packets and waits for Echo Replies.
For example:
ping example.com
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
This tells us that an ICMP response was received.
Windows
ping example.com
By default, Windows sends four requests.
To continuously send requests:
ping -t example.com
Stop it with:
Ctrl + C
Linux and macOS
ping example.com
On most Linux and macOS systems, ping continues until you stop it.
Use:
ping -c 4 example.com
This sends four requests.
Testing Your Local Network
You can also ping your router:
ping 192.168.1.1
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
But your application reports:
Could not resolve host
This may indicate a DNS resolution problem.
The nslookup command helps investigate it.
Windows
nslookup example.com
Linux and macOS
nslookup example.com
Example output:
Name: example.com
Address: 93.184.216.34
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
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
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
To request an IPv4 record:
Resolve-DnsName example.com -Type A
To request an IPv6 record:
Resolve-DnsName example.com -Type AAAA
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
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
Linux: traceroute
traceroute example.com
Depending on your Linux distribution, you may need to install traceroute.
macOS: traceroute
traceroute example.com
Example output might look like:
1 192.168.1.1
2 10.20.0.1
3 203.0.113.10
4 example.com
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 * * *
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
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
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
To investigate a specific port:
netstat -ano | findstr :3000
Linux: ss
Modern Linux systems commonly use ss:
ss -tuln
Options:
-
-t: TCP sockets -
-u: UDP sockets -
-l: Listening sockets -
-n: Numeric addresses and ports
To include process information:
sudo ss -tulnp
macOS: lsof
On macOS, use:
lsof -i
To display listening TCP ports:
lsof -nP -iTCP -sTCP:LISTEN
To inspect a specific port:
lsof -nP -iTCP:3000 -sTCP:LISTEN
PowerShell: Get-NetTCPConnection
PowerShell provides a useful networking command:
Get-NetTCPConnection
To display listening TCP connections:
Get-NetTCPConnection -State Listen
To inspect a specific port:
Get-NetTCPConnection -LocalPort 3000
To display the process ID:
Get-NetTCPConnection -LocalPort 3000 |
Select-Object LocalAddress, LocalPort, State, OwningProcess
You can then identify the process:
Get-Process -Id 4820
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
For example:
Test-NetConnection example.com -Port 443
This tests TCP connectivity to port 443.
A successful result includes:
TcpTestSucceeded : True
A failed result may show:
TcpTestSucceeded : False
You can also test a local development server:
Test-NetConnection localhost -Port 3000
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
This sends an HTTP request and displays the response body.
Display Response Headers
curl -I https://example.com
The -I option requests headers only using an HTTP HEAD request.
You may see something like:
HTTP/2 200
content-type: text/html
The 200 status code indicates success.
Display Detailed Connection Information
curl -v https://example.com
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
The response body is saved as page.html.
Follow Redirects
Some websites redirect requests to another URL.
Use:
curl -L https://example.com
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"}'
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
You can also use PowerShell's native HTTP commands:
Invoke-WebRequest -Uri "https://example.com"
For API requests:
Invoke-RestMethod -Uri "https://api.example.com/users"
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
If you receive:
HTTP/2 404
The server responded, but the requested resource wasn't found.
If you receive:
HTTP/2 500
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
Clear the DNS cache:
ipconfig /flushdns
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
You can also restart the system DNS responder:
sudo killall -HUP mDNSResponder
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
To flush its cache:
sudo resolvectl flush-caches
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
But the browser cannot connect.
Start by checking DNS:
nslookup example.com
Then test basic connectivity:
ping example.com
Remember that ICMP may be blocked.
Next, test HTTPS connectivity from PowerShell:
Test-NetConnection example.com -Port 443
Finally, inspect the HTTP response:
curl -I https://example.com
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
But another device on your network cannot access it.
First, identify your local IP address.
Windows:
ipconfig
Linux:
ip addr
macOS:
ifconfig
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
Then, from another device on the same network, try:
http://YOUR_LOCAL_IP:5173
For example:
http://192.168.1.100:5173
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")
But it fails.
You can investigate the endpoint independently:
curl -v https://api.example.com/users
Then inspect DNS:
nslookup api.example.com
And test the HTTPS port:
Test-NetConnection api.example.com -Port 443
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)