November 19, 2024
Tutorials

How to Use SSH to Connect to a Remote Server on Ubuntu 22.04

How to Use SSH to Connect to a Remote Server on Ubuntu 22

SSH, also known as Secure Shell, provides secure communication between systems across networks. It is an encrypted and secure feature that enables users to connect to a remote server. It also facilitates its users to transfer files between systems.

This guide will assist you in establishing a secure connection between your local Ubuntu 22.04 machine and a remote server using SSH.

How to Use SSH to Connect to a Remote Server on Ubuntu 22.04?

Perform the sequential steps in the following to setup, configure, and connect a remote server using SSH.

How to Setup OpenSSH Server on Ubuntu 22.04?

In this section, you will see the detailed process of installing OpenSSH on your local Ubuntu 22.04 machine.

Step 1: Update System Files

Before installing any package, ensure that your Ubuntu machine is up to date with the newest version of the installed packages:

sudo apt update

If you see the message, “All packages are up to date” means your machine is ready to install new packages.

Step 2: Install OpenSSH Server

OpenSSH enables secure communication and remote management of your machine over a network. Therefore, to enable remote access, install the OpenSSH via terminal:

sudo apt install openssh-server -y
 

The OpenSSH is now installed on your local machine without encountering any error.

Step 3: Start SSH Service

Run the command to start the SSH services and accept the incoming remote connections on your Ubuntu 22.04 machine:

sudo service ssh start

If your terminal displays no output, it indicates that the SSH service has been successfully initiated.

Step 4: Check SSH Status

You can confirm the current status of the SSH service using the terminal:

sudo systemctl status ssh

If you have performed the above steps successfully, you will see that the status of the SSH is active and running on your local Ubuntu machine.

How to Setup Firewall on Ubuntu 22.04?

Configure the Firewall to allow SSH using the following steps.

Step 1: Allow SSH Connections Through UFW

Execute the command from your Ubuntu terminal to allow SSH connections through the system Firewall:

sudo ufw allow ssh

The command updated the firewall rules to allow secure connections.

Step 2: Enable the Firewall

Use the ufw command followed by the enable option to enable the UFW firewall on your system:

sudo ufw enable

The firewall is now active and enabled on your local Ubuntu 22.04 machine.

Step 3: Check Firewall Status

You can check the status of UFW to ensure that the firewall is active and accepts remote connections:

sudo ufw status

How to Modify the SSH Server Configuration File?

Follow these steps to edit the SSH configuration file according to your needs.

Step 1: Set the SSH Server Port

To modify and edit the SSH server configuration file, you need to open the text editor such as Nano:

sudo nano /etc/ssh/sshd_config

In the configuration file, locate #Port 22. Edit it according to your requirements. For example, if you want to use Port 2222, remove the # and add Port 2222. Save the changes and then close the file:

Port 2222

Remember to remove the comment (#) from the port line. Additionally, you can specify the port as required.

Step 2: Restart the SSH Service

Execute the command via the terminal to restart and update the SSH service:

sudo service ssh restart

If you see no output in your terminal, it means the SSH service has been restarted successfully.

How to Configure Virtual Machine Network Settings?

The steps below will guide you through configuring virtual machine network settings.

Step 1: Get the IP of the Virtual Machine

To establish an SSH connection to a remote server, you will need the IP address of a specific virtual machine. For example, to remotely connect to a VirtualBox machine, execute the ifconfig command to retrieve its IP address:

ifconfig

Locate the localhost IP address. For example, 127.0.0.1 is displayed on the terminal window.

Step 2: Configure Virtual Machine

To configure network settings, open the VirtualBox Manager, select a particular machine such as TechPub, and locate Settings:

Step 3: Modify Network Settings

In the Settings, locate to Network > Advance > Port Forwarding button:

Step 4: Set Port Forwarding Rules

After opening the Port Forwarding Rules:

  • Click the Add (+) button.
  • Enter your remote server ID 127.0.0.1 in the Host IP field.
  • Specify port 2222 in the Host Port.
  • Add a guest port in the Guest Port field.
  • Save the settings by clicking the OK button:

How to Connect to a Remote Server Using SSH on Ubuntu 22.04?

To establish a secure connection between systems remotely, perform the steps below.

Step 1: Connect to a Remote Server via SSH

After completing the aforementioned steps, you can now execute the SSH command as follows to connect to a remote server:

ssh -p 2222 techpub@127.0.0.1

The SSH command is executed to connect to the remote server using the Ubuntu terminal.

Remember to replace techpub with your remote server username and 127.0.0.1 with the IP address or domain name of your remote server.

Conclusion

You can use SSH on the Ubuntu 22.04 machine to connect to a remote server. To enable secure remote access to your system, install the OpenSSH server using the command sudo apt install openssh. Ensure that the Firewall is allowed and enabled to accept remote connections.

Furthermore, set the SSH Server Port and Port Forwarding Rules and finally use the ssh -p 2222 techpub@127.0.0.1 to connect to a remote server. This article demonstrated a detailed process of using SSH to connect to a remote server on Ubuntu 22.04.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video