Networking commands are essential for diagnosing, troubleshooting, and managing network-related issues on both Windows and Linux systems. Here, we’ll explore some commonly used networking commands and their usage on both platforms, as well as troubleshooting steps to resolve common network problems.
Additional Steps:
IPCONFIG is used to display the current TCP/IP network configuration values assigned to network interfaces on Windows systems.
ipconfig
PING is used to test the reachability of a host on a network and measure the round-trip time for packets sent from the local system to the target host.
ping <hostname or IP address>
NETSTAT displays active network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
netstat
netstat
netstat -ano
commandsnetstat
NETSTAT displays active network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
When you run netstat
without any options, it provides a list of active connections along with the corresponding protocols (TCP/UDP), local and foreign addresses, and their status.
Example output of netstat
:
Protocol | Local Address | Foreign Address | State | PID |
---|---|---|---|---|
TCP | 0.0.0.0:135 | 0.0.0.0:0 | LISTENING | 1234 |
TCP | 0.0.0.0:445 | 0.0.0.0:0 | LISTENING | 5678 |
TCP | 0.0.0.0:3389 | 0.0.0.0:0 | LISTENING | 9012 |
… | … | … | … | … |
In this table:
Protocol
: Protocol used by the connection (e.g., TCP).Local Address
: Local IP address and port.Foreign Address
: Remote IP address and port.State
: Connection state (e.g., LISTENING, ESTABLISHED).PID
: Process ID of the application or service associated with the connection.netstat -ano
Now, let’s look at the netstat -ano
command and explain its components:
a
: This option displays all connections and listening ports.n
: This option displays addresses and port numbers in numerical form, rather than resolving hostnames and service names.o
: This option displays the owning process ID associated with each connection.Example output of netstat -ano
:
Protocol | Local Address | Foreign Address | State | PID |
---|---|---|---|---|
TCP | 0.0.0.0:135 | 0.0.0.0:0 | LISTENING | 1234 |
TCP | 0.0.0.0:445 | 0.0.0.0:0 | LISTENING | 5678 |
TCP | 0.0.0.0:3389 | 0.0.0.0:0 | LISTENING | 9012 |
… | … | … | … | … |
In this table:
Protocol
: Protocol used by the connection (e.g., TCP).Local Address
: Local IP address and port.Foreign Address
: Remote IP address and port.State
: Connection state (e.g., LISTENING, ESTABLISHED).PID
: Process ID of the application or service associated with the connection.This information can be useful for identifying which processes are using network resources, troubleshooting network issues, or identifying potentially unwanted or malicious connections.
Remember, on Windows systems, the PID can be cross-referenced with the Task Manager to identify the specific process that’s using a particular network connection.
netstat -ano | findstr "LISTENING"
Another useful way to filter the netstat -ano
output is by using the findstr
command to specifically look for listening ports. This can help you quickly identify which processes are listening for incoming connections.
The findstr "LISTENING"
command filters the output to show only the connections that are in the LISTENING
state.
Example output after filtering:
Protocol | Local Address | Foreign Address | State | PID |
---|---|---|---|---|
TCP | 0.0.0.0:135 | 0.0.0.0:0 | LISTENING | 1234 |
TCP | 0.0.0.0:445 | 0.0.0.0:0 | LISTENING | 5678 |
TCP | 0.0.0.0:3389 | 0.0.0.0:0 | LISTENING | 9012 |
… | … | … | … | … |
In this filtered output:
Protocol
: Protocol used by the connection (e.g., TCP).Local Address
: Local IP address and port.Foreign Address
: Remote IP address and port.State
: Connection state (e.g., LISTENING).PID
: Process ID of the application or service associated with the connection.TRACERT is used to trace the route taken by packets across an IP network to a specific destination.
tracert <hostname or IP address>
NSLOOKUP is a command-line tool used for querying the Domain Name System (DNS) to obtain domain name or IP address mapping.
nslookup <hostname>
To retrieve the MAC address of a device on a Windows system, you typically use the getmac command. Here’s how you can use it:
getmac
To retrieve the ARP (Address Resolution Protocol) table on a Windows system, you can use the arp -a
command. This command displays the current ARP cache, which contains mappings of IP addresses to MAC addresses. Here’s how you can use it:
arp -a
When you run this command in the Command Prompt, it will display the ARP table with the IP addresses and corresponding MAC addresses of devices on the local network.
IP ADDR is used to display or manipulate routing, network devices, interfaces, and tunnels.
ip addr
IFCONFIG is used to configure and display network interface parameters on Linux systems.
ifconfig
PING in Linux functions similarly to Windows. It is used to test network connectivity.
ping <hostname or IP address>
NETSTAT in Linux provides information about active network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
netstat
netstat
lsof
commandsudo lsof -i -P -n | grep LISTEN
In Linux, the lsof
command (short for “list open files”) is used to list information about files that are open by processes. When combined with specific options, it can also provide information about network connections.
The command: sudo lsof -i -P -n | grep LISTEN
lists all open files (-i) that are network-related, including sockets and ports (-i), in a numeric format (-P), and with host names turned off (-n). The output is then filtered using grep LISTEN
to only show the entries where the connection state is “LISTENING”.
Example output after filtering:
COMMAND | PID | USER | FD | TYPE | DEVICE | SIZE/OFF | NODE | NAME |
---|---|---|---|---|---|---|---|---|
sshd | 1234 | root | 3u | IPv4 | 1234 | 0t0 | TCP | *:22 (LISTEN) |
nginx | 5678 | www-data | 6u | IPv4 | 5678 | 0t0 | TCP | *:80 (LISTEN) |
apache | 9012 | apache | 4u | IPv6 | 9012 | 0t0 | TCP | *:443 (LISTEN) |
… | … | … | … | … | … | … | … | … |
In this table:
COMMAND
: Process name or command associated with the open file.PID
: Process ID of the application or service.USER
: User associated with the process.FD
: File descriptor associated with the file.TYPE
: Type of file (e.g., IPv4, IPv6).DEVICE
: Device number associated with the file.SIZE/OFF
: Size or offset of the file.NODE
: Node associated with the file.NAME
: Name of the file.This command is useful for identifying which processes are listening on network ports, such as SSH, web servers, or other network services.
TRACEROUTE is used to display the route packets take to network host.
traceroute <hostname or IP address>
DIG (Domain Information Groper) is a flexible tool for interrogating DNS name servers.
dig <hostname>
SS is used to investigate sockets.
ss
SS -TUNLP is used to display TCP, UDP, UNIX, and RAW sockets, along with the process listening or owning them.
ss -tunlp
There are many more commands and ways to troubleshoot networks. Understanding and mastering these networking commands can greatly enhance your ability to troubleshoot and manage network-related issues on both Windows and Linux systems. Whether you’re a system administrator, network engineer, or simply a curious user, familiarity with these commands is invaluable in maintaining a healthy and efficient network environment.