Dump the code
Best ressources to improve your Python Skills

SSH : Basics and commands

Created 11 months ago
Posted By admin
8min read
SSH, or Secure Shell, is a cryptographic network protocol that ensures secure communication over an unsecured network. It provides a secure way to access and manage remote systems, execute commands, and transfer files, enhancing the overall security of networked computing.

I. Secure Remote Access

SSH allows users to log in to a remote system securely over an encrypted channel. This is particularly important when accessing servers or systems over the internet or any untrusted network.

Let's consider a practical example where you need to log in to a remote server securely over the internet using SSH. Assume you have a server with the IP address 203.0.113.1 and a username your_username. You would use the following command to log in:

ssh [email protected]
  • The ssh command is used to initiate a secure connection to the remote server (203.0.113.1) using the specified username (your_username)
  • The communication between your local machine and the remote server is encrypted, ensuring that any sensitive information, such as login credentials, is secure during transmission.
  • This example illustrates connecting to a server over the internet (203.0.113.1). Without SSH, your login credentials could be exposed to potential eavesdropping. SSH encrypts the communication, providing a secure method for accessing servers over untrusted networks.
  • Upon executing the command, you'll be prompted for your password (or key passphrase if using key-based authentication). This ensures that only authorized users with the correct credentials can access the remote system.

II. Encrypted Communication

All communication between the client and the server is encrypted, providing confidentiality and protecting against eavesdropping. This is essential for protecting sensitive information, such as login credentials and data transferred between systems.

To securely transfer a sensitive file from your local machine to a remote server over the internet using SSH, assume you have a file named important_data.txt containing confidential information. You want to upload this file to a server with the IP address 203.0.113.1, placing it in the directory /home/your_username

You would use the scp command, which uses SSH for secure file transfer. The following command uploads the file securely:

scp important_data.txt [email protected]:/home/your_username/
  • The scp command utilizes the SSH protocol to securely copy files between your local machine and the remote server (203.0.113.1). The entire communication, including file transfer and login credentials, is encrypted, providing confidentiality.
  • Since the file being transferred (important_data.txt) contains sensitive information, using SSH ensures that the data is protected against eavesdropping. Without encryption, the file contents could potentially be intercepted by malicious actors during the transfer process.
  • The use of scp (Secure Copy Protocol) ensures that the file is transferred securely from your local machine to the remote server. This is crucial for maintaining the confidentiality of the data being transferred.
  • Similar to the SSH login example, when you use scp, you'll be prompted for your password (or key passphrase if using key-based authentication). This ensures that only authorized users with the correct credentials can upload files to the remote system.

III. Authentication

SSH supports various authentication methods, including password-based authentication and public key-based authentication. Public key authentication is considered more secure and is often preferred for better protection against unauthorized access.

Let's consider an example where you use public key-based authentication to log in to a remote server. This method is more secure than password-based authentication and is commonly used for better protection against unauthorized access.

1. Generate an SSH key pair (if you haven't already):
ssh-keygen -t rsa -b 2048
  • ssh-keygen: This is the command-line utility for generating SSH keys.
  • -t rsa: This option specifies the type of key to create, in this case, RSA.
  • -b 2048: This option sets the number of bits in the key. In this example, it creates a key with a length of 2048 bits. The length of the key determines its strength, and longer keys are generally more secure but may take longer to generate and use more resources.
When you run this command, the system will prompt you to provide the file path where the key pair will be saved. The default is often set to ~/.ssh/id_rsa (private key) and ~/.ssh/id_rsa.pub (public key).

Additionally, you'll be prompted to enter an optional passphrase. A passphrase adds an extra layer of security. Even if someone gains access to your private key, they would still need the passphrase to use it. It's recommended to set a passphrase, but you can choose to leave it empty if you want.

2. Copy your public key to the remote server:
ssh-copy-id [email protected]
This command installs your public key on the remote server. You'll be prompted for your password on the remote server.

3. Log in using public key authentication:
ssh [email protected]
You should be able to log in without entering a password, as the public key is used for authentication.


IV. Tunneling and Port Forwarding

SSH can create secure tunnels, allowing the secure transmission of other network protocols. This is useful for accessing services securely, such as accessing a database or web server on a remote machine as if they were local.

This is often referred to as SSH tunneling or port forwarding. Assume you have a database server with the IP address '203.0.113.2' and a MySQL database running on port '3306'. You want to access the MySQL database securely from your local machine.

ssh -L 3306:localhost:3306 [email protected]
  • -L 3306:localhost:3306: This option specifies the port forwarding. It tells SSH to listen on port '3306' on your local machine and forward any traffic to the remote server's 'localhost:3306'. This effectively creates a secure tunnel between your local machine and the remote server.
  • [email protected]: This is the SSH login to the remote server.
After executing this command, your local machine's port '3306' is securely connected to the MySQL server on the remote machine. Now, you can use a MySQL client on your local machine to connect to the database as if it were running locally.


V. Remote Command Execution

SSH allows users to execute commands on a remote machine without physically being present at the machine. This is helpful for system administration, automation, and remote troubleshooting.

Assume you have a remote server with the IP address "203.0.113.4", and you want to check the disk usage on that server without physically being present at the machine.

ssh [email protected] 'df -h'
After executing this command, you will see the disk usage information for the remote server printed in your local terminal.


VI. X11 Forwarding

SSH supports X11 forwarding, allowing the secure transmission of graphical applications from a remote server to a local machine. This is useful for running graphical applications on a remote server while displaying them locally.

Assume you have a remote server with the IP address "203.0.113.5", and you want to run a graphical application, such as a text editor, and have its window displayed on your local machine.

ssh -X [email protected]
-X: This option enables X11 forwarding. It tells SSH to forward graphical application windows from the remote server to your local machine.

After executing this command, you are logged into the remote server with X11 forwarding enabled. Now, you can run a graphical application, such as the text editor 'gedit', and have its window displayed on your local machine:

gedit
The graphical window of the text editor ("gedit") will appear on your local machine, even though the application is running on the remote server.


In summary, SSH is a fundamental tool for secure remote communication, offering encryption, strong authentication, and various features for secure system administration and file transfer. Its versatility makes it an essential component of modern networked computing.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles