linux server network

SSH (Secure Shell) and SFTP

SSH (Secure Shell) is an encrypted protocol that allows secure access to a remote system through the command line. It is commonly used to manage Linux servers, execute remote commands, and securely transfer files.

Screenshot about the usage of SSH in Linux.

This is a typical connection from Linux to another host, also running Linux, via SSH.

What is SSH and what is it used for

SSH replaces insecure protocols such as telnet and rlogin, providing authentication and encryption of exchanged data. It typically operates on port 22.

  • Secure remote access to servers
  • Execute commands remotely
  • Secure file transfer (scp or sftp)
  • Tunneling and port forwarding

Basic connection

The general syntax of the SSH command is:

ssh username@hostname

Example:

ssh [email protected]

On the first connection, SSH will ask you to confirm the remote serverโ€™s key and then request the password of the specified user.

Authentication with public key

To avoid typing the password each time and to improve security, SSH can use a pair of keys (public and private).

  1. Generate a key pair on your computer:

ssh-keygen -t ed25519 -C "[email protected]"

  1. Send the public key to the remote server:

ssh-copy-id username@hostname

After that, you can log in without a password, since authentication will occur through your locally stored private key.

SSH configuration file

To simplify usage, you can create or edit the ~/.ssh/config file and define aliases for frequently used servers:

Host my-server
    HostName 192.168.1.10
    User root
    IdentityFile ~/.ssh/id_ed25519

From that point, you can connect simply by typing:

ssh my-server

File transfer with SSH

You can copy files between your local system and a remote server using scp or sftp.

Examples:

scp file.txt [email protected]:/home/user/

scp [email protected]:/var/log/syslog ./

Or open an interactive SFTP session:

sftp [email protected]

Port forwarding (tunneling)

SSH allows the creation of encrypted tunnels between local and remote ports, useful for accessing internal services that are not publicly exposed.

Example: forward local port 8080 to port 80 on a remote server:

ssh -L 8080:localhost:80 user@remote_host

You can now open http://localhost:8080 in your browser and access the remote service as if it were local.

Useful commands

  • ssh -v โ€” Show detailed debug output during connection
  • ssh-add ~/.ssh/id_ed25519 โ€” Add a private key to the SSH agent
  • ssh-agent bash โ€” Start a new SSH agent session
  • exit โ€” Terminate the current SSH session

Common issues

  • Permission denied (publickey): Check the permissions of files inside ~/.ssh/ and verify that the correct public key is on the server.
  • Connection refused: Ensure the SSH service is running (sudo systemctl status ssh).
  • Timeout: Make sure port 22 is open and reachable through the firewall.

Conclusion

SSH is an essential tool for anyone working with remote servers or networks. Using it correctly and securely (through keys, configurations, and proper permissions) is crucial to maintaining the integrity of your system and data.