Unix / Linux - Communication
Unix / Linux - Network Communication Utilities
Section titled “Unix / Linux - Network Communication Utilities”In this chapter, we will explore essential network communication utilities in Unix-like systems. Working in distributed environments necessitates communicating with remote users and accessing remote systems securely and efficiently.
Modern Unix-like systems offer a suite of tools for network interaction. We’ll cover fundamental utilities for diagnostics, remote access, and file transfer, focusing on current best practices.
The ping Utility
Section titled “The ping Utility”The ping command is a fundamental tool used to test network connectivity. It sends ICMP ECHO_REQUEST packets to a target host and waits for ECHO_RESPONSE replies. This helps verify if a remote host is reachable and responsive.
ping is useful for:
- Diagnosing basic network connectivity issues.
- Determining if a remote host is online.
- Getting a rough estimate of network latency (round-trip time).
- Identifying packet loss.
Syntax
Section titled “Syntax”The basic syntax for ping is:
$ ping [options] hostname_or_ip-addressBy default, ping sends packets continuously until interrupted. You can stop it by pressing Ctrl + C. You can often limit the number of pings using an option like -c count (e.g., ping -c 4 google.com).
Note: Some network firewalls may block ICMP requests, so a lack of response doesn’t always mean the host is down, just that it’s not responding to pings.
Example
Section titled “Example”Checking the availability of google.com:
$ ping -c 4 google.comPING google.com (142.250.190.78) 56(84) bytes of data.64 bytes from lhr48s15-in-f14.1e100.net (142.250.190.78): icmp_seq=1 ttl=118 time=15.4 ms64 bytes from lhr48s15-in-f14.1e100.net (142.250.190.78): icmp_seq=2 ttl=118 time=15.9 ms64 bytes from lhr48s15-in-f14.1e100.net (142.250.190.78): icmp_seq=3 ttl=118 time=15.5 ms64 bytes from lhr48s15-in-f14.1e100.net (142.250.190.78): icmp_seq=4 ttl=118 time=15.2 ms
--- google.com ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3005msrtt min/avg/max/mdev = 15.198/15.508/15.889/0.242 ms$If a host cannot be resolved or reached (and isn’t blocking pings):
$ ping nonexistanthostname12345.comping: nonexistanthostname12345.com: Name or service not known$Secure File Transfer: scp and sftp
Section titled “Secure File Transfer: scp and sftp”Transferring files between systems is a common task. While the older ftp (File Transfer Protocol) utility exists, it is highly discouraged for general use because it transmits data, including usernames and passwords, in plaintext, making it insecure.
Modern best practice dictates using secure alternatives that leverage SSH (Secure Shell):
scp(Secure Copy): Simple command-line utility for copying files/directories between hosts over SSH. Ideal for non-interactive transfers.sftp(SSH File Transfer Protocol): An interactive file transfer program, similar in usage toftp, but operating over a secure SSH connection. Allows browsing remote directories, uploading, downloading, etc.
Both scp and sftp encrypt the entire session, protecting your credentials and data.
Using scp
Section titled “Using scp”The basic syntax resembles the cp command:
Copy a local file to a remote host:
$ scp local_file.txt username@remotehost:/path/to/destination/Copy a file from a remote host to the local machine:
$ scp username@remotehost:/path/to/remote_file.txt /local/destination/path/Copy a directory recursively (use the -r option):
$ scp -r local_directory username@remotehost:/path/to/destination/You will typically be prompted for the remote user’s password unless you have SSH key-based authentication configured (which is recommended for frequent use).
Using sftp
Section titled “Using sftp”To start an interactive sftp session:
$ sftp username@remotehostOnce connected, you’ll get an sftp> prompt. Common commands include:
ls: List files on the remote system.lls: List files on the local system.cd path: Change remote directory.lcd path: Change local directory.pwd: Show current remote directory.lpwd: Show current local directory.get remote_file [local_file]: Download a file.put local_file [remote_file]: Upload a file.mkdir directory: Create a remote directory.rm file: Delete a remote file.helpor?: Show available commands.quitorexit: Close the session.
Example sftp session snippet:
$ sftp user@example.comConnected to example.com.sftp> pwdRemote working directory: /home/usersftp> lsDocuments Downloads myfile.txtsftp> get myfile.txtFetching /home/user/myfile.txt to myfile.txt/home/user/myfile.txt 100% 12KB 3.1MB/s 00:00sftp> lcd /tmpLocal working directory: /tmpsftp> put important_report.pdfUploading important_report.pdf to /home/user/important_report.pdfimportant_report.pdf 100% 512KB 10.2MB/s 00:00sftp> quit$For more advanced file synchronization tasks, consider the rsync utility, which is very efficient for transferring only the differences between files.
Secure Remote Login: ssh
Section titled “Secure Remote Login: ssh”To connect to a remote Unix/Linux machine and use its command line, the standard and secure method is ssh (Secure Shell). It replaces older, insecure protocols like telnet and rlogin.
telnet is highly insecure as it transmits all data, including passwords, in plaintext. You should never use telnet over untrusted networks.
ssh provides a secure, encrypted channel for remote login sessions and command execution.
Syntax
Section titled “Syntax”$ ssh [options] username@hostname_or_ip-addressIf your local username is the same as the remote username, you can omit username@:
$ ssh hostname_or_ip-addressExample
Section titled “Example”Connecting to a remote server server.example.com as user admin:
$ ssh admin@server.example.comThe authenticity of host 'server.example.com (192.0.2.10)' can't be established.ECDSA key fingerprint is SHA256:AbCdEfGhIjKlMnOpQrStUvWxYzabcdefghijklmnop.Are you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added 'server.example.com' (ECDSA) to the list of known hosts.admin@server.example.com's password:Last login: Mon Jul 25 10:00:00 2023 from client.example.org[admin@server ~]$ # You are now logged into the remote server[admin@server ~]$ pwd/home/admin[admin@server ~]$ lsDocuments Public Templates[admin@server ~]$ logoutConnection to server.example.com closed.$The first time you connect to a host, ssh will ask you to verify its host key. Subsequent connections will use the stored key to prevent man-in-the-middle attacks. As with scp/sftp, passwordless login using SSH keys is recommended for better security and convenience.
Obsolete Utilities: finger
Section titled “Obsolete Utilities: finger”The finger command was historically used to display information about users on local or remote systems (login name, real name, terminal, login time, etc.).
However, due to privacy and security concerns (revealing potentially sensitive user information), the finger service (fingerd) is almost universally disabled on modern systems. While the client command might still exist, it’s unlikely to work against remote hosts and is considered obsolete for practical use.