SSH (Secure Shell) in Linux
SSH stands for Secure Shell. It is a network protocol that allows us to connect to another machine remotely and securely.
For example, suppose we have an AWS EC2 instance running Linux. We want to access that machine from our local terminal. We can use SSH to connect to it.
With SSH, we can:
- Connect to remote Linux servers.
- Execute commands on remote machines.
- Transfer files between machines.
- Manage servers remotely.
- Authenticate using passwords or SSH keys.
1. SSH Connection Basics
To connect to a remote machine, we generally need three things:
- Username: The user account on the remote machine.
- IP Address: The public or private IP address of the remote machine, depending on network connectivity.
- Port Number: The port on which the SSH service is running. By default, SSH uses port
22.
The basic syntax is:
bash
ssh username@ip_address
Example:
bash
ssh ubuntu@192.168.1.10
Here:
sshis the command.ubuntuis the username.192.168.1.10is the remote machine’s IP address.
We can also specify a port number using the -p option.
bash
ssh -p 2222 ubuntu@192.168.1.10
Here, SSH will connect using port 2222 instead of the default port 22.
2. Connecting Using a Password
The simplest way to connect to a remote machine is through password-based authentication.
bash
ssh ubuntu@192.168.1.10
When we execute this command, SSH will ask for the password of the remote user.
bash
ubuntu@192.168.1.10's password:
We enter the password, and if the credentials are correct, we get access to the remote machine.
Important points:
- The password belongs to the remote machine’s user account.
- Password authentication must be enabled on the SSH server.
- We don’t see the password characters while typing. This is normal.
3. Connecting Using SSH Keys
Now, instead of entering a password every time, we can use SSH keys.
SSH key-based authentication is commonly used in DevOps because it provides a secure and convenient way to connect to remote servers.
In SSH key authentication, we have two keys:
- Private Key
- Public Key
Private Key
- The private key stays on our local machine.
- We should never share it with anyone.
- It is used to prove our identity when connecting to a remote server.
Public Key
- The public key can be shared with the remote server.
- We place it inside the remote user’s
authorized_keysfile. - The server uses it to verify that we possess the matching private key.
Remember: The private key stays with us, and the public key goes to the server.
4. Generating SSH Keys
We can generate SSH keys using the ssh-keygen command.
bash
ssh-keygen
It will ask us a few questions:
text
Enter file in which to save the key:
Enter passphrase:
Enter same passphrase again:
By default, it creates two files inside the .ssh directory.
bash
~/.ssh/id_rsa
~/.ssh/id_rsa.pub
Here:
id_rsa→ Private key.id_rsa.pub→ Public key.
We can also generate keys using the Ed25519 algorithm.
bash
ssh-keygen -t ed25519
This is a commonly recommended modern key type.
To see the generated keys:
bash
ls -la ~/.ssh/
5. How to Configure SSH Key Authentication
Now we have generated our public and private keys.
The next step is to copy the public key to the remote server.
We can use the following command:
bash
ssh-copy-id ubuntu@192.168.1.10
This command copies our public key into the remote user’s authorized_keys file.
We can also manually copy the public key using:
bash
cat ~/.ssh/id_ed25519.pub
Then add its contents to the following file on the remote machine:
bash
~/.ssh/authorized_keys
Once the public key is added, we can connect using:
bash
ssh ubuntu@192.168.1.10
Now, instead of asking for the remote account password, SSH will authenticate using our SSH keys (unless a private-key passphrase is required).
6. Understanding the .ssh Directory
There is a special hidden directory called .ssh inside the user’s home directory.
bash
cd ~/.ssh
ls -la
This directory contains different files related to SSH configuration and authentication.
Some important files are:
| File | Purpose |
|---|---|
id_rsa | Private SSH key |
id_rsa.pub | Public SSH key |
id_ed25519 | Ed25519 private key |
id_ed25519.pub | Ed25519 public key |
authorized_keys | Stores public keys allowed to access this user’s account |
known_hosts | Stores identities of previously contacted SSH servers |
config | User-specific SSH client configuration |
Let’s understand some of these files in detail.
7. What is the known_hosts File?
Whenever we connect to a remote server for the first time, SSH checks the identity of that server.
The server provides its host key, and SSH asks us to verify and trust it.
We generally see a message like:
text
The authenticity of host '192.168.1.10' can't be established.
Are you sure you want to continue connecting (yes/no)?
When we type yes, the server’s host key fingerprint is accepted and its key is stored in our local known_hosts file.
Location:
bash
~/.ssh/known_hosts
Why do we need this file?
- It remembers previously contacted servers.
- It helps identify servers when we connect again.
- It protects us against certain man-in-the-middle attacks by detecting unexpected host key changes.
For example, if the server’s host key changes, SSH may show a warning.
We should not blindly ignore this warning. First, verify whether the server was rebuilt or its host keys were legitimately changed.
8. What is the authorized_keys File?
This file is available on the remote machine.
Location:
bash
~/.ssh/authorized_keys
It contains the public keys of users or machines that are allowed to authenticate to that particular user account.
For example, suppose we have two machines:
Local Machine
Client- Private key: id_ed25519
- Public key:
id_ed25519.pub
Remote Machine
Server- Stores the public key in authorized_keys.
- Verifies authentication using the corresponding key.
The important difference is:
| File | Stored Where? | Purpose |
|---|---|---|
known_hosts | Local machine | Remembers remote server identities |
authorized_keys | Remote machine | Stores public keys allowed to log in |
9. What is /etc/ssh/sshd_config?
This is one of the most important SSH configuration files.
Location:
bash
/etc/ssh/sshd_config
It is the configuration file for the SSH server daemon.
It controls how the SSH server behaves.
For example, we can configure:
- Port
bash
Port 22
Defines the port on which the SSH server listens.
- PermitRootLogin
bash
PermitRootLogin no
Controls whether the root user is allowed to log in through SSH.
- PasswordAuthentication
bash
PasswordAuthentication yes
Controls whether password-based authentication is allowed.
- PubkeyAuthentication
bash
PubkeyAuthentication yes
Controls whether public-key authentication is allowed.
- AllowUsers
bash
AllowUsers ubuntu admin
Allows only the specified users to connect through SSH.
After modifying the configuration, we generally need to validate it and restart or reload the SSH service.
bash
sudo sshd -t
On Ubuntu:
bash
sudo systemctl restart ssh
On some other Linux distributions, the service name is sshd.
bash
sudo systemctl restart sshd
Important: Be careful when changing SSH configurations on a remote server. Keep your existing session open until you have verified that the new configuration allows another successful connection.
10. What is /etc/ssh/ssh_config?
There is another important configuration file:
bash
/etc/ssh/ssh_config
This is the configuration file for the SSH client.
It controls how our local machine connects to remote servers.
Let’s understand the difference:
| File | Purpose |
|---|---|
/etc/ssh/sshd_config | SSH server configuration |
/etc/ssh/ssh_config | System-wide SSH client configuration |
~/.ssh/config | User-specific SSH client configuration |
Remember:
sshd_config→ Server side.ssh_config→ Client side.
11. Understanding the SSH Port Number
By default, SSH uses port 22.
We can specify a different port in the server configuration:
bash
sudo nano /etc/ssh/sshd_config
Find or add:
bash
Port 2222
After validating and restarting the SSH service, we can connect using:
bash
ssh -p 2222 ubuntu@192.168.1.10
Here:
-pspecifies the port.2222is our custom SSH port.ubuntuis the username.192.168.1.10is the server IP address.
Remember that changing the SSH port alone does not provide strong security. We must also ensure the firewall and cloud security groups allow the selected port.
12. Important SSH Commands – Quick Revision
| Command | Purpose |
|---|---|
ssh user@ip | Connect to a remote machine |
ssh -p 2222 user@ip | Connect using a custom port |
ssh-keygen | Generate SSH keys |
ssh-keygen -t ed25519 | Generate Ed25519 keys |
ssh-copy-id user@ip | Copy public key to remote server |
ssh -i key user@ip | Connect using a specific private key |
ssh -v user@ip | Connect with verbose debugging |
cat ~/.ssh/known_hosts | View known hosts |
cat ~/.ssh/authorized_keys | View authorized public keys |
13. SSH File Permissions
SSH is very particular about file permissions.
Some commonly used permissions are:
bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/authorized_keys
| Permission | Meaning |
|---|---|
700 | Only the owner can access the directory |
600 | Only the owner can read and write |
644 | Owner can read and write; others can read |
These permissions help protect private keys and SSH authentication files.
14. Practical Assignment
Try performing these tasks yourself:
Level 1 – Basic
- Connect to a remote Linux machine using a password.
- Generate SSH keys using
ssh-keygen. - Identify the public and private key files.
- Check the contents of the
.sshdirectory. - Connect to a remote server using a custom port.
Level 2 – Intermediate
- Configure passwordless SSH authentication.
- Understand the difference between
known_hostsandauthorized_keys. - Configure SSH to use a custom port.
- Disable password authentication after confirming key-based login works.
- Explore the
/etc/ssh/sshd_configfile.