In Unix, networking is a fundamental aspect of connecting and managing systems, whether it’s configuring network interfaces, diagnosing connectivity issues, or managing active network connections. Unix provides a powerful set of tools for networking tasks.
This blog will guide you through the key commands for managing network connections in Unix, including configuring network interfaces, monitoring active connections, and troubleshooting network issues.
1. How to View Network Configuration with ifconfig
The ifconfig
(interface configuration) command is used to view or configure network interfaces in Unix. It allows you to see details like the IP address, netmask, and status of your network interfaces.
Basic Syntax
ifconfig [interface]
interface
: The name of the network interface (e.g.,eth0
,wlan0
,lo
for loopback).
Examples:
- View all network interfaces:
$ ifconfig
This will display information about all active network interfaces.
- View a specific network interface:
$ ifconfig eth0
- Assign a static IP address to a network interface (for temporary changes):
$ sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
- Enable or disable an interface:
$ sudo ifconfig eth0 up # Enable
$ sudo ifconfig eth0 down # Disable
2. How to Use ip
for Advanced Networking Tasks
While ifconfig
is commonly used for basic tasks, the ip
command provides a more modern and feature-rich way to configure networking in Unix.
Basic Syntax
ip [options] [object] [command]
- [object]: The type of object to manipulate (e.g.,
link
,addr
,route
). - [command]: The operation to perform (e.g.,
show
,add
,del
).
Examples:
- View all network interfaces:
$ ip addr show
- Show the routing table:
$ ip route show
- Assign a static IP address:
$ sudo ip addr add 192.168.1.100/24 dev eth0
- Delete an IP address:
$ sudo ip addr del 192.168.1.100/24 dev eth0
- Enable or disable an interface:
$ sudo ip link set eth0 up # Enable
$ sudo ip link set eth0 down # Disable
3. How to Check Network Connectivity with ping
The ping
command is used to test the network connectivity between your system and another device. It sends ICMP Echo Request packets to the target and waits for a reply. This is useful for diagnosing connectivity issues.
Basic Syntax
ping [options] destination
- destination: The IP address or domain name of the device you want to test connectivity with.
Examples:
- Ping a remote host by IP address:
$ ping 192.168.1.1
- Ping a domain name:
$ ping google.com
- Ping continuously:
$ ping -t google.com # (on Linux, `-t` is for infinite pings)
- Limit the number of pings:
$ ping -c 4 google.com # Ping 4 times
- Ping a host until stopped manually:
$ ping -i 2 google.com # Interval of 2 seconds between pings
4. How to View Active Network Connections with netstat
The netstat
(network statistics) command provides information about active network connections, routing tables, and network interface statistics.
Basic Syntax
netstat [options]
Commonly Used Options:
-t
: Show TCP connections.-u
: Show UDP connections.-l
: Show listening ports.-a
: Show all connections and listening ports.-p
: Show the process using each connection.
Examples:
- List all active TCP connections:
$ netstat -t
- List all active UDP connections:
$ netstat -u
- Show all connections and listening ports:
$ netstat -a
- Display process information with network connections:
$ netstat -p
- Show network interface statistics:
$ netstat -i
5. How to Monitor Real-Time Network Activity with iftop
iftop
is a command-line tool that provides a real-time view of network traffic. It shows bandwidth usage by host and displays the connections currently using the most bandwidth.
Basic Syntax
sudo iftop
Once running, iftop
will display a list of active connections, their data rates, and other relevant information. You can interact with iftop
using the following keys:
P
: Pause the display.S
: Sort by source address.D
: Sort by destination address.q
: Quit the program.
Example:
$ sudo iftop
6. How to Trace Network Path with traceroute
traceroute
is a tool used to trace the route packets take from your system to a remote host. It helps diagnose routing issues and identify where delays are occurring.
Basic Syntax
traceroute destination
Examples:
- Trace the route to a host:
$ traceroute google.com
- Set the maximum number of hops:
$ traceroute -m 20 google.com
7. How to Test DNS Resolution with nslookup
and dig
To troubleshoot DNS issues, you can use nslookup
and dig
to query DNS servers and get detailed information about domain names and IP addresses.
Using nslookup
:
$ nslookup google.com
This command will return the IP address of google.com
.
Using dig
:
$ dig google.com
dig
provides more detailed information, including the DNS query time and response headers.
Practice Time!
- View Network Interfaces:
- Use
ifconfig
andip
to view and configure network interfaces.
- Test Connectivity:
- Use
ping
to test connectivity to different hosts, including IP addresses and domain names.
- Monitor Active Connections:
- Use
netstat
andiftop
to monitor network connections and traffic.
- Trace Network Path:
- Use
traceroute
to identify where delays are happening on your network.
- Test DNS Resolution:
- Use
nslookup
anddig
to troubleshoot DNS issues.
Summary
- How to use
ifconfig
andip
to configure and view network interfaces. - How to use
ping
to test network connectivity. - How to use
netstat
andiftop
to monitor active connections and bandwidth. - How to use
traceroute
to trace the route of network packets. - How to use
nslookup
anddig
to troubleshoot DNS resolution.