How to connect to Ubuntu via SSH

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Connecting to an Ubuntu server via Secure Shell (SSH) is an essential skill for anyone managing servers, developing applications, or working in a DevOps environment. SSH provides a secure channel over an unsecured network in a client-server architecture, allowing users to log into another machine over the network, execute commands in a remote machine, and move files from one machine to another.

SSH encrypts the session, making the connection very secure. In this tutorial, we’ll cover everything you need to know about connecting to an Ubuntu server via SSH, from the basics to more advanced topics.

Prerequisites

  • A server running Ubuntu
  • A local computer running any OS
  • An SSH client installed on the local computer (OpenSSH is most commonly used)
  • The IP address or hostname of the Ubuntu server
  • An account on the server with SSH access

Installing OpenSSH Server on Ubuntu

First, make sure that the SSH server is installed and running on your Ubuntu server:

sudo apt update
sudo apt install openssh-server

Once the installation is complete, you can check the status of the SSH server with:

sudo systemctl status ssh

The output should indicate that the SSH service is active and running.

Connecting to the SSH Server

To connect to your Ubuntu server from your local machine, you simply need to use the ssh command:

ssh username@server_ip_or_hostname

Replace username with your user on the server and server_ip_or_hostname with the server’s IP address or hostname. You’ll be prompted for your user’s password on the sever.

SSH Key Authentication

For a more secure and convenient way to connect to the server, you can set up SSH key-based authentication. On the local machine, generate a new SSH key pair:

ssh-keygen -t rsa -b 4096

Accept the default key file location and press Enter. You can also set a passphrase for more security. The output will look like this:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/your_username/.ssh/id_rsa):[Press enter]

Next, copy your public SSH key to the server by using the ssh-copy-id script:

ssh-copy-id username@server_ip_or_hostname

Now, when you log in, the server checks to see if you have a private key that corresponds to a public key on the server and allows login without a password.

Configuring SSH

For better security and customization, you can configure SSH by editing the sshd_config file:

sudo nano /etc/ssh/sshd_config

Some of the changes you can make include:

  • Changing the default port (Port 22)
  • Disabling root login (PermitRootLogin no)
  • Allowing only specific users (AllowUsers your_username)

After editing the file, restart the SSH service:

sudo systemctl restart ssh

Advanced Usage

Tunneling/Port Forwarding: SSH tunneling is a method to forward a port from one SSH server to a client or another server. You can achieve this with:

ssh -L local_port:remote_host:remote_port username@server_ip_or_hostname

Where local_port is the local port number to bind, remote_host is the machine that has the service you want to forward, and remote_port is the service port number.

X11 Forwarding: If you want to run graphical applications on the server and have them displayed on your local machine, use the -X option:

ssh -X username@server_ip_or_hostname

Conclusion

Through this guide, we’ve explored how to establish a basic SSH connection to your Ubuntu server, as well as configure advanced SSH features for security and versatility. Mastering SSH connectivity will ensure safe, efficient administration of your server, and provide flexibility in how you interact with it remotely.