Learn the Basics of SSH: A Beginner’s Guide for Linux Users

Introduction

Introduction

Secure Shell, commonly known as SSH, is a tool that allows you to securely connect to a remote machine and control it using the command line. If you’re new to Linux or Unix-like systems, SSH might sound a bit confusing, but it’s an essential tool for managing remote machines, and it’s not as difficult to use as it might seem.

Before SSH, there were other tools like Telnet and FTP that could be used to remotely control a machine. However, those tools were not secure, and data transmitted over the network could be intercepted by attackers. SSH was created to solve these security problems by encrypting all data transmitted over the network. This makes it theoretically impossible for attackers to eavesdrop on your communication or manipulate the data in transit.

Learning how to use SSH can be very helpful if you’re working on a project that involves remote servers or if you’re interested in learning more about Linux system administration. SSH is a powerful tool that can help you manage remote machines and automate tasks.

In this beginner-friendly article, I’ll introduce you to SSH and explain why it’s such an important tool for Linux users. We’ll cover the basics of SSH and provide examples of commonly used commands. We’ll also touch on how to use SSH on Windows and explore some more advanced features.

So if you’re new to Linux and feeling overwhelmed, don’t worry – I’ll guide you through everything you need to know to get started with SSH.


History of SSH

Before SSH, there were several remote control applications like Telnet and FTP that were used to remotely control a machine. However, these applications transmitted data in plain text, which made it easy for attackers to intercept the communication and read or manipulate the data. This posed a significant security risk, especially if sensitive information was being transmitted.

SSH was created to address these security concerns by encrypting all data transmitted over the network. It was designed to provide a secure and encrypted channel for data transmission, remote login capabilities, and secure execution of commands on a remote machine.

SSH was first developed by Tatu Ylönen, a Finnish programmer, in 1995. He created it as a replacement for Telnet and other insecure remote access tools. Ylönen released SSH as open source software, which allowed other programmers to contribute to the project and improve its security and functionality.

Over time, SSH has become the standard protocol for remote access and control of Linux and Unix-like machines. It has also been ported to other operating systems, including Windows and Mac, and is used by millions of users worldwide.

Today, there are multiple versions of the SSH protocol, including SSH1 and SSH2, with SSH2 being the more secure and widely used version. Additionally, there are many implementations of SSH, including OpenSSH, PuTTY, and SSH.NET, to name a few.

In the next section, we’ll explore the basics of SSH and how to use it to connect to a remote machine.


The Basics of using SSH

Next, we’ll cover the basics of using SSH to connect to remote machines securely. We’ll start by installing and configuring the necessary SSH software, and then show you how to establish a connection to a remote machine. We’ll also briefly cover some best practices for verifying the identity of the remote machine to ensure that you’re connecting to the right machine. Finally, we’ll explore some advanced features of SSH that can help you use it more effectively.


Installing ssh

To use SSH, you’ll need to install an SSH client on your local machine and an SSH server on the remote machine you want to connect to. Most Linux distributions come with an SSH client pre-installed, but you may need to install an SSH server separately.

To install SSH on Ubuntu, Linux Mint, Pop!_OS, or other Debian-based systems, you can use the following command in a terminal window:

Terminal
sudo apt-get update
sudo apt-get install openssh-server

This will update your package list and install the OpenSSH server on your machine.


Connecting to a Remote Machine

Once you have an SSH client and server set up, you can connect to a remote machine using the ssh command in a terminal window. Here’s the basic syntax for connecting to a remote machine:

Terminal
ssh username@remote_host

Here, [username] is the username you’ll use to log in to the remote machine, and [remote_host] is the IP address or domain name of the machine you want to connect to. You can use the same syntax to connect to a domain as you would an IP address, just replace the IP address with the domain name.

For example, to connect to a remote machine with the domain name example.com using the username bob, you would use the following command:

Terminal
ssh bob@example.com

When you connect to a remote machine for the first time, SSH will check the machine’s identity to ensure that you’re connecting to the right machine. This is an important security measure to protect against man-in-the-middle attacks, which can intercept your communication and steal your data.

SSH will display the remote machine’s key fingerprint, which you can verify against the fingerprint provided by the remote machine’s administrator to confirm that you’re connecting to the correct machine.

Here’s an example of what the key fingerprint verification questions look like in a simulated terminal session:

Terminal
The authenticity of host 'example.com (192.168.1.100)' can't be established.
ECDSA key fingerprint is SHA256:8ywdGntHn+ieaqZJn1wA8K4fs4Lkqw+1yvJ9KjPN8Q0.
Are you sure you want to continue connecting (yes/no)? yes

In this example, SSH is warning you that the authenticity of the remote machine cannot be established. It then displays the machine’s ECDSA key fingerprint, which you can verify against the fingerprint provided by the remote machine’s administrator. Finally, it asks you to confirm that you want to connect to the machine by typing ‘yes’ or ‘no’.

If you choose to connect to the machine by typing ‘yes’, SSH will store the machine’s key fingerprint, on your machine, in a file named known_hosts in a hidden sub-directory of your home folder named .ssh, so that it can be verified automatically in the future. If you try to connect to the same machine again in the future and the key fingerprint does not match the one stored on your machine, SSH will warn you that the machine’s identity has changed and prompt you to verify the fingerprint again.


Terminating an ssh session

To terminate an SSH session, simply type the exit command in the terminal window that is running the SSH session. This will log you out of the remote machine and close the SSH connection.

Here is an example of an ssh session where we connect and then terminate the session:

Terminal
username@local:~$ ssh bob@remote
bob@remote's password: *********
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-58-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

3 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable

Last login: Fri Feb 19 08:55:22 2023 from 192.168.1.10
bob@remote:~$ exit
logout
Connection to remote closed.
username@local:~$

In this example, the user username on the local machine logs into the remote machine using the SSH client and specifies the bob account on the remote machine. After successfully entering the password, the user is logged in to the remote machine as bob, and the prompt changes to bob@remote:~$. To end the SSH session, the user types exit, and the connection is closed. The prompt changes back to the local machine’s prompt (username@local:~$).


Using SSH Keys

SSH keys provide a more secure and convenient way to authenticate to a remote machine than passwords. Instead of typing a password every time you connect, you can use an SSH key pair, which consists of a private key that you keep on your local machine and a public key that you upload to the remote machine. Using SSH keys is much more convenient than typing a password every time you connect to a remote machine.


Creating an SSH Key

To create an SSH key pair, use the ssh-keygen command in a terminal window. By default, ssh-keygen generates a 2048-bit RSA key pair, but I recommend using a more secure elliptic curve algorithm such as ed25519. To generate an ed25519 key pair with the most secure options, use the following command:

Terminal
ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519

This will generate a new ed25519 key pair with the following options:

  • -o: Use the new OpenSSH key format instead of the old PEM format.
  • -a 100: Use the maximum number of KDF (Key Derivation Function) rounds allowed by the OpenSSH key format. This makes the key more secure against brute-force attacks.
  • -t ed25519: Generate an ed25519 key pair.
  • -f ~/.ssh/id_ed25519: Save the private key in the file ~/.ssh/id_ed25519, and the public key in the file ~/.ssh/id_ed25519.pub.

Copying your public key to the remote server

Once you’ve generated an SSH key pair, you can copy the public key to the remote machine using the ssh-copy-id command. This will upload the public key to the remote machine and add it to the list of authorized keys for the specified user account. For example, if you want to add your public key to the authorized_keys file on a remote machine with the domain name example.com and the username bob, you can use the following command:

Terminal
ssh-copy-id -i ~/.ssh/id_ed25519 bob@example.com

This will copy the public key from ~/.ssh/id_ed25519.pub on your local machine to the ~/.ssh/authorized_keys file on the remote machine. You’ll be prompted to enter the password for the remote user account one last time to complete the process.

Once you’ve added your public key to the authorized_keys file, you can connect to the remote machine without typing a password by using the ssh command with the appropriate arguments. For example, to connect to a remote machine with the domain name example.com using the username bob and your new ed25519 key, you can use the following command:

Terminal
ssh bob@example.com

Much Simpler!

Using SSH keys is a convenient and secure way to connect to remote machines. You won’t have to remember any passwords, and you’ll be protected against brute-force attacks. If you have multiple remote machines that you need to connect to, using SSH keys can help you keep track of which sessions are active and which keys are being used.

In the next section, we’ll explore some of the more advanced features of SSH, including port forwarding and tunneling.



Advanced features of ssh

Now that you know the basics of SSH and how to manage SSH keys, let’s dive into some more advanced features of SSH. SSH provides a wide range of tools for remote connectivity that go beyond the basic functionality of terminal login. These advanced features include port forwarding, tunneling, X11 forwarding, and more. While these features may be more complex than basic SSH use, they can be incredibly powerful for remote system administration and secure file transfer. In this section, we’ll explore some of these advanced features and show you how to use them.


Forward and Reverse Port Forwarding

SSH port forwarding is a powerful feature that allows you to securely connect to a remote server and forward network traffic from your local machine to the remote machine and vice versa. There are two types of port forwarding: forward port forwarding and reverse port forwarding.


Forward Port Forwarding

Forward port forwarding, also known as local port forwarding, is used to forward a port from your local machine to the remote machine. This is useful if you want to access a service that is running on the remote machine, but the service is not directly accessible from your local machine.

For example, suppose you have a remote server that is running a web server on port 80. However, the server is not directly accessible from your local machine because it is behind a firewall. You can use forward port forwarding to forward port 80 from the remote machine to a port on your local machine, and then access the web server using your web browser. Here’s how you would do this:

Terminal
ssh -L 8080:localhost:80 user@remote_host

This command establishes an SSH connection to the remote machine and forwards traffic from port 8080 on your local machine to port 80 on the remote machine. You can now access the web server by opening a web browser and navigating to http://localhost:8080.


Reverse Port Forwarding

Reverse port forwarding, also known as remote port forwarding, is used to forward a port from the remote machine to your local machine. This is useful if you have a service running on your local machine that is not directly accessible from the remote machine.

For example, suppose you have a service running on your local machine that is listening on port 9000. You want to allow a user on a remote machine to access this service. However, your local machine is behind a firewall and is not directly accessible from the remote machine. You can use reverse port forwarding to forward port 9000 from the remote machine to a port on your local machine. Here’s how you would do this:

Terminal
ssh -R 9000:localhost:9000 user@remote_host

This command establishes an SSH connection to the remote machine and forwards traffic from port 9000 on the remote machine to port 9000 on your local machine. The user on the remote machine can now access the service by connecting to port 9000 on the remote machine.

Both forward and reverse port forwarding can be used to securely access services that are not directly accessible from your local machine or from the remote machine. They are powerful features that can simplify your workflow and help you work more efficiently.


Proxying

SSH also provides proxying functionality, which allows you to use an SSH connection as a proxy for other network traffic. This can be useful for accessing resources on a remote network, bypassing firewalls and content filters, and improving the security of your web browsing. There are two types of proxying: local and remote.


Local Proxying

Local proxying, also known as dynamic forwarding, sets up a SOCKS proxy on your local machine and forwards traffic through the proxy to a remote machine. This is useful if you need to access resources on a remote network that are not directly accessible from your local machine.

To use local proxying, you can use the ssh command with the -D option. For example, to set up a local SOCKS proxy on port 1080, you can use the following command:

Terminal
ssh -D 1080 user@remote-machine

This will set up a SOCKS proxy on your local machine that forwards traffic to the remote machine. You can then configure your web browser to use the SOCKS proxy on your local machine.

Configure your web browser to use the proxy server:

The exact steps for configuring your web browser will depend on the browser you’re using, but here’s an example for Firefox:

  1. Open Firefox and go to the Preferences menu.
  2. Click on the General tab and scroll down to the Network Settings section.
  3. Click on the Settings button.
  4. Select the Manual proxy configuration option.
  5. In the SOCKS Host field, enter “127.0.0.1” (or the IP address of your local machine) and the port number you used when setting up the SSH tunnel (in this example, “1080”).
  6. Check the box next to “SOCKS v5”.
  7. Click OK to save the changes.

Now, all of your web browsing traffic will be routed through the SSH tunnel to the remote machine, providing an extra layer of security.


Remote Proxying

To use remote proxying, you can use the ssh command with the -L option. For example, to forward traffic from port 8080 on the remote machine to port 80 on your local machine, you can use the following command:

Terminal
ssh -L 8080:localhost:80 user@remote-machine

This will forward traffic from port 8080 on the remote machine to port 80 on your local machine. You can then access the remote service by navigating to “localhost:8080” in your web browser.

Proxying is a powerful tool that can be used to add an extra layer of security to your web browsing, among other things. Whether you’re using local or remote proxying, it’s important to understand the potential security risks and take appropriate precautions to protect your data.


X11 Forwarding

X11 is a technology that allows programs to show their windows and graphics on your screen. X11 forwarding is a feature of SSH that allows you to run graphical applications on a remote Linux server and have them display on your local machine. This can be useful if you need to run a Linux application that has a graphical interface, but you don’t have access to a Linux desktop environment on your local machine. By forwarding X11 traffic over an SSH connection, you can run the application on the remote server and see its graphical interface on your local machine. However, keep in mind that X11 forwarding can be slow over a slow network connection, and some applications may not work correctly over X11 forwarding.

To use X11 forwarding, you must first enable it in your SSH configuration by adding the following line to your local SSH configuration file (~/.ssh/config):

~/.ssh/config
ForwardX11 yes

Then, connect to the remote Linux server using the -X option with the ssh command:

Terminal
ssh -X username@remote_server

Once you are connected, you can start graphical applications on the remote Linux server just as you would on your local machine. However, keep in mind that X11 forwarding can be slow over a slow network connection.

It’s important to note that the program you want to run must be installed on the remote machine for X11 forwarding to work. If the program is not installed, you’ll need to install it on the remote machine before you can use X11 forwarding to run it.



Examples of Commonly Used Basic SSH Commands

In this section, we’ll explore some examples of commonly used SSH commands. These commands are essential for connecting to a remote machine, copying files, running remote commands, and managing SSH keys.


Connecting to a Remote Machine

The ssh command is used to connect to a remote machine. To connect to a remote machine, you need to know the remote machine’s IP address or domain name, and you must have a user account on the remote machine.

Here’s the basic syntax for connecting to a remote machine using SSH:

Terminal
ssh remote_user@remote_machine

For example, to connect to a remote machine with the IP address 192.168.0.10 using the user account user1, you would enter the following command:

Terminal
ssh user1@192.168.0.10

Copying Files Using SCP

The scp command is used to securely copy files between two machines using SSH. To copy a file from a local machine to a remote machine, use the following syntax:

Terminal
scp local_file remote_user@remote_machine:remote_folder

For example, to copy a file named example.txt from the local machine to a remote machine with the IP address 192.168.0.10 and the user account user1, you would enter the following command:

Terminal
scp example.txt user1@192.168.0.10:/home/user1/

To copy a file from a remote machine to a local machine, use the following syntax:

Terminal
scp remote_user@remote_machine:remote_file local_folder

For example, to copy a file named example.txt from the remote machine with the IP address 192.168.0.10 and the user account user1 to the local machine’s current directory, you would enter the following command:

Terminal
scp user1@192.168.0.10:/home/user1/example.txt .

The . in this context refers to the current working directory on your local machine, and it tells scp to copy the file to the current working directory.


Running Remote Commands Using SSH

The ssh command can also be used to run remote commands on a remote machine. To run a remote command, use the following syntax:

Terminal
ssh remote_user@remote_machine 'remote_command'

For example, to list the contents of the remote machine’s home directory, you would enter the following command:

Terminal
ssh user1@192.168.0.10 'ls ~'


Using SSH in Microsoft Windows

SSH is a popular tool for secure remote access and management of Linux and Unix-based servers. While Linux and Unix systems have native support for SSH, Microsoft Windows does not. However, there are several ways to use SSH on Windows, including:


Windows Subsystem for Linux

The Windows Subsystem for Linux (WSL) is a compatibility layer for running Linux binaries natively on Windows. WSL provides a Linux-compatible kernel interface developed by Microsoft, which allows users to run Linux commands on Windows. This includes the ability to use SSH to connect to remote Linux servers. To use SSH in WSL, simply open a terminal window and run the ssh command followed by the hostname or IP address of the remote machine.


PuTTY

PuTTY is a popular SSH and telnet client for Windows. It provides a graphical user interface (GUI) for connecting to remote machines using SSH. To use PuTTY, download and install the software from the official website. Once installed, open PuTTY and enter the hostname or IP address of the remote machine, along with the port number, if necessary. Then, select SSH as the connection type and click “Open” to start the connection. You can also save the connection settings for future use.


Powershell

PowerShell is a command-line shell and scripting language developed by Microsoft. It provides a powerful and flexible way to manage and automate Windows systems. PowerShell also includes built-in support for SSH, allowing you to connect to remote Linux servers. To use SSH in PowerShell, first ensure that the OpenSSH client is installed on your system. Then, open a PowerShell window and run the ssh command followed by the hostname or IP address of the remote machine.


Using Third-Party Software

There are several third-party software options for using SSH on Windows, such as MobaXterm, Bitvise SSH Client, and SecureCRT. These applications provide a range of features and capabilities beyond what is offered by the built-in Windows options. However, they may require a purchase or subscription to use.

In conclusion, using SSH on Microsoft Windows is possible and relatively easy with the available options. Depending on your needs and preferences, you can choose from a variety of methods to securely connect to remote Linux servers.



Conclusion

A summary of the benefits of ssh

SSH is a powerful tool that provides secure remote access to other machines over the network. By using encryption to protect communication between computers, it is a much safer alternative to traditional methods such as Telnet and FTP. Some of the key benefits of using SSH include:

  • Improved security: SSH encrypts data being transmitted, providing a secure and private communication channel that can be used to transfer sensitive information.
  • Remote access: With SSH, you can remotely log in to another machine and control it just as if you were sitting in front of it, making it a great tool for system administrators and remote workers.
  • File transfer: SSH can also be used to securely transfer files between machines, which is particularly useful for backing up data and transferring files securely.

Continued learning

As a Linux beginner, learning about SSH can be overwhelming at first, but it is an incredibly valuable tool that can make your life much easier. We encourage you to continue learning and exploring the capabilities of SSH, as it will likely become an essential part of your daily workflow.

If you would like to continue your learning, there are many resources available to help you. Here are a few recommendations:

  • The OpenSSH homepage: This is the official website for OpenSSH, the most popular implementation of SSH, and it provides a wealth of information on how to use and configure SSH.
  • SSH Mastery: OpenSSH, PuTTY, Tunnels and Keys: This book by Michael W. Lucas is an excellent resource for learning about SSH, and it covers many advanced topics in addition to the basics.
  • SSH.com: This is another popular implementation of SSH, and their website provides a wide range of resources for learning how to use SSH, as well as commercial products and services that utilize SSH.

With these resources and a little practice, you’ll soon become proficient in using SSH to connect to and manage remote machines.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *