Welcome back to our Unix command series! This installment is all about network management tools in Unix. Whether you’re an administrator or a regular user, understanding these commands can help you troubleshoot network issues, monitor connections, and secure your network.
1. Checking Network Connections with netstat
What It Does: netstat (network statistics) is a versatile command that shows network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
How to Use It:
- netstat -a lists all ports and connections.
- netstat -r shows the routing table.
- netstat -tuln lists active Internet connections to the server.
Scenario and Solution: If you’re experiencing issues with your server not responding on certain ports, netstat -tuln can help you verify whether the ports are listening and open.
Pro Tip: Regularly check your system’s connections with netstat to ensure no unexpected services are running or ports open.
2. Managing IP Addresses with ip
What It Does: The ip command is used to show / manipulate routing, devices, policy routing, and tunnels. It’s more powerful and intended to replace older tools like ifconfig.
How to Use It:
- ip addr show displays all IP addresses associated with all network interfaces.
- ip route list shows the route entries of the system.
Scenario and Solution: When setting up a new interface or troubleshooting network issues, ip addr add 192.168.1.2/24 dev eth0 can assign an IP address to a specific interface directly.
Pro Tip: ip is more preferred in modern Unix systems over ifconfig, as it provides more detailed and extensive network interface info.
3. Secure Shell for Remote Connections with ssh
What It Does: ssh (Secure Shell) is an essential command for securely accessing remote machines over a network. It encrypts all traffic to eliminate eavesdropping, connection hijacking, and other attacks.
How to Use It:
- ssh username@host connects you to host as username.
- Use ssh -p port username@host if the remote server uses a non-standard port.
Scenario and Solution: If you need to manage a server or perform tasks remotely, ssh provides a secure way to log in and operate the remote machine as if you were right there.
Pro Tip: Always use key-based authentication over passwords with ssh for added security, especially when connecting to critical production environments.
4. Transferring Files Securely with scp
What It Does: scp (Secure Copy) allows secure file transfer between local and remote hosts under the SSH protocol.
How to Use It:
- scp localfile username@remote:/path copies a local file to a remote directory.
- scp username@remote:/path/remotefile localdir to download a file from a remote machine.
Scenario and Solution: Need to quickly and securely send configuration files to your remote server? Use scp to ensure they’re transferred safely without exposure to network threats.
Pro Tip: Combine scp with compression options (like -C) for faster transfer speeds, especially useful when dealing with large files.