CodeGym /Courses /Docker SELF /Introduction to SSH: Setting Up Remote Access, Using Keys...

Introduction to SSH: Setting Up Remote Access, Using Keys

Docker SELF
Level 4 , Lesson 3
Available

Introduction to SSH: Setting Up Remote Access, Using Keys

1. What is SSH?

Imagine you're on one side of the planet, and the server you need to set up is on the other side. Running around like a school IT specialist fixing problems in different rooms? Not what we're aiming for. Fortunately, there's SSH (Secure SHell) — a tool for remote server management.

SSH is a network protocol that allows you to securely connect to remote computers. The connection is encrypted, which means the data transferred is protected from prying eyes. SSH replaces outdated methods like Telnet, which used to send data in plain text (like a postcard in the mail).

Advantages of SSH

  1. Security. SSH uses encryption, protecting data from interception.
  2. Flexibility. Remote server management, file transfer, setting up tunnels — all of this is available through SSH.
  3. Cross-platform. Works on Linux, Windows (including WSL), and MacOS.

Now that you understand why SSH is important, let's move on to practice.


2. Installing the SSH client

Let's start with the client that allows you to connect to the server. If you’re using a modern Linux distribution, the SSH client is probably already installed. Let’s check it:

ssh -V

You should see information about the SSH client version. If the command is not found, install it (on Ubuntu and other Debian-based distributions):

sudo apt update
sudo apt install openssh-client

On MacOS, the SSH client is pre-installed, and if you're working in WSL, the installation is similar to Linux.


3. Connecting to a Remote Server via SSH

Connect to the server using this command:

ssh username@hostname
  • username — this is the username on the remote server.
  • hostname — this is the server's IP address or domain name.

Example connection:

ssh student@192.168.1.10

If everything's set up right, the system will ask for a password. After entering the password, you'll find yourself in the server's remote console. Congrats, you're halfway to becoming "the supervillain operator in a hacker movie."


4. Installing and Configuring the SSH Server

Installing the SSH Server

If you're working on a server or local machine you want to connect to, you need to install the SSH server. On Ubuntu, do it like this:

sudo apt update
sudo apt install openssh-server

Check the status of the SSH service:

sudo systemctl status ssh

You should see a message like active (running). If the server isn't running, use:

sudo systemctl start ssh
sudo systemctl enable ssh

Now the SSH server is ready to accept connections.


Configuring the SSH Server

The main SSH server configuration file is /etc/ssh/sshd_config. To edit it, use any text editor, for example, nano:

sudo nano /etc/ssh/sshd_config

Pay attention to the following parameters:

  • Port 22 — the port SSH runs on.
  • PermitRootLogin no — prohibits root login (it's recommended to leave it as no for security reasons).
  • PasswordAuthentication yes — allows password-based login.

After changing the parameters, restart the SSH server:

sudo systemctl restart ssh

5. Using Key Authentication

Password-based connections are convenient, but not secure. The best option is to use SSH keys.

Generating SSH Keys

On the client machine, run the command:

ssh-keygen

The script will ask for a path to save the key (default is ~/.ssh/id_rsa). You can press Enter to accept it. Then it will prompt for a passphrase. It’s better to leave it empty for simplicity (though it’s not super secure).

After running the command, the key files will be created:

  • id_rsa — the private key (don’t make it public, like your project’s source code before release, especially if it’s full of tech debt).
  • id_rsa.pub — the public key (this one can be used for connecting).

Transferring the Key to the Server

Use the ssh-copy-id command to transfer the public key to the server:

ssh-copy-id username@hostname

This will add your public key to the ~/.ssh/authorized_keys file on the server. Now you can connect without entering your password.

Try it out:

ssh username@hostname

If it works, you won’t be asked for a password. Your life just got a little easier.

SSH and WSL

If you’re using Windows with WSL, you can use SSH there too. Open the WSL terminal and install the SSH client:

sudo apt update
sudo apt install openssh-client

The rest of the steps are the same as for Linux.


6. Practical part: setting up SSH on localhost

For practice, you can set up an SSH connection between two machines on the same network or even on your own computer (using localhost as the address).

  1. Install an SSH server and client on one machine.
  2. Connect via the local address:
ssh your_user@localhost
  1. Set up key-based authentication as shown above.

As a result, you should achieve a connection without password input.


7. Working with SSH Configurations and Aliases

If you often connect to different servers, you can simplify your life by adding SSH configurations in the file ~/.ssh/config:

Host myserver
    HostName 192.168.1.10
    User student
    IdentityFile ~/.ssh/id_rsa

Now you can connect with just this command:

ssh myserver

Common Errors and Fixes

  1. Error "Connection refused". This means the SSH server is not running on the remote machine. Make sure it's installed and started.

  2. Error "Permission denied". Either you entered the wrong password, or the keys are not set up properly. Check the contents of the ~/.ssh/authorized_keys file on the server.

  3. Error "No route to host". Check if the server is reachable over the network (use ping).


Now you're equipped with knowledge on how to set up SSH, use keys, and deal with common issues. It's a powerful tool that's used everywhere—whether it's setting up servers at work or managing your home Raspberry Pi.

1
Task
Docker SELF, level 4, lesson 3
Locked
Installing an SSH client
Installing an SSH client
1
Task
Docker SELF, level 4, lesson 3
Locked
Generating SSH keys and setting up authentication
Generating SSH keys and setting up authentication
1
Task
Docker SELF, level 4, lesson 3
Locked
Configuring the SSH configuration file
Configuring the SSH configuration file
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION