Linux networking is a key area that every user or system administrator should be familiar with. Linux provides you with powerful tools for network management, troubleshooting, and communicating with other systems. This article is written step by step for beginners.
ls and cd is sufficient. Network concepts are explained from scratch.
1. Basic Networking Concepts
Before diving into commands, it's helpful to understand a few key concepts:
- IP Address: A unique identifier for each device on a network. There are two types: IPv4 (e.g., 192.168.1.1) and IPv6.
- Subnet Mask: Determines which part of an IP address belongs to the network and which part belongs to the device.
- Default Gateway: The address of the device that routes network traffic to outside the local network (usually a router).
- DNS (Domain Name System): A service that converts domain names (like google.com) into IP addresses.
In Linux, you can view and change all of this information with simple commands.
2. Viewing Network Information
ip – Display and Manage Network Interfaces
The ip command is the modern replacement for ifconfig and provides comprehensive information about network interfaces:
ip addr show
# or in short
ip a
The output includes the interface name (e.g., eth0 or wlan0), IP address, subnet mask, and status (UP/DOWN).
Finding Your Public IP Address
To see your public IP address (the address visible from the internet), you can use tools like curl:
curl ifconfig.me
# or
curl icanhazip.com
3. Network Connection Troubleshooting
ping – Check Connectivity to a Host
The ping command sends ICMP packets to an address and measures response time. This is the simplest way to check connectivity.
ping -c 4 google.com
# Send 4 packets to Google's server
If you receive a response, the connection is working. If you get an error, there may be an issue with the local network, DNS, or firewall.
traceroute – Path to Destination
This command shows the route packets take from source to destination and is useful for identifying problematic nodes.
traceroute google.com
nslookup and dig – DNS Lookup
These commands are used to check the conversion of domain names to IP addresses:
nslookup google.com
dig google.com
4. Managing Connections and Ports
ss – Display Socket Connections
The ss command is the replacement for netstat and shows network connections, open ports, and their status.
ss -tuln
# Show all TCP and UDP ports that are listening
-t: TCP connections-u: UDP connections-l: Only listening ports-n: Show ports as numbers (without converting to service names)
netstat (Legacy)
If you prefer netstat, it is still available on most distributions:
netstat -tulpn
# Show connections with associated process names
5. File Transfer Over Network
scp – Secure Copy via SSH
scp (Secure Copy) allows you to securely transfer files between different systems.
scp file.txt user@192.168.1.10:/home/user/
# Copy file from local system to the destination server
scp user@192.168.1.10:/home/user/file.txt ./
# Copy file from server to local system
rsync – Advanced Synchronization
rsync is very efficient for synchronizing folders and incremental transfers (only changes).
rsync -avz /local/folder/ user@server:/remote/folder/
# Synchronize a folder with compression
6. Secure Connection to Server with SSH
SSH (Secure Shell) is a protocol for secure connection to remote systems. This tool is essential for system administrators.
ssh user@192.168.1.10
# Connect to server with a specific username
To connect without entering a password, you can use SSH keys:
ssh-keygen -t rsa -b 4096
# Generate public/private key
ssh-copy-id user@192.168.1.10
# Copy public key to the server
7. Downloading and Sending HTTP Requests
curl – Powerful Data Transfer Tool
curl is used for sending HTTP requests, downloading files, and interacting with APIs.
curl https://api.github.com
# Get information from GitHub API
curl -O https://example.com/file.zip
# Download file with original name
wget – Simple Downloader
wget is a simple and lightweight tool for downloading files from the internet.
wget https://example.com/file.zip
# Download a file
8. Network Configuration in Linux
Network settings in Linux are typically stored in configuration files. In Debian/Ubuntu-based distributions, the main file is /etc/network/interfaces. In RedHat-based distributions, files in /etc/sysconfig/network-scripts/ifcfg-* are used.
To temporarily set an IP address (until reboot), use the ip command:
ip addr add 192.168.1.100/24 dev eth0
# Add IP address to eth0 interface
ip link set eth0 up
# Activate the interface
ip are lost after a system reboot. For permanent changes, edit the configuration files.
9. Advanced Networking Tools
- tcpdump: For capturing and analyzing network packets (useful for deep troubleshooting).
- netcat (nc): A tool for reading and writing data through network connections, often used for port scanning and debugging.
- iftop: Shows real-time bandwidth usage.
- nmap: A powerful scanner for discovering hosts and network services.
tcpdump -i eth0
# Capture packets on eth0 interface
nc -zv google.com 80
# Check if port 80 is open on google.com
10. Practical Examples
Example 1: Check Internet Connection
ping -c 4 8.8.8.8
if [ $? -eq 0 ]; then
echo "Internet connection is working."
else
echo "No internet connection."
fi
Example 2: Find Local Network Devices
nmap -sn 192.168.1.0/24
# Ping scan to find active devices on the local network
Example 3: Automatic Log File Backup to Backup Server
rsync -avz /var/log/ user@backup-server:/backup/logs/
# Synchronize logs with a backup server
Conclusion and Next Steps
In this article, you learned the basic concepts and most important networking commands in Linux. To become a professional user, continue with these steps:
- Practice with one networking command every day and analyze its output.
- Study TCP/IP, routing, and DNS concepts more deeply.
- Use
tcpdumpandwiresharkfor network traffic analysis. - Read «The Linux Command Line» (available at Linux Library) for more learning.