8.2.12 OpenSSH Installation and Configuration

The Host Banner OpenSSH

OpenSSH allows secure connection to a server via the SSH protocol, executing commands, transferring files, and managing the server. Proper OpenSSH configuration is a critical step in ensuring your server’s security.

System Requirements

  • Virtual or Dedicated server
  • Minimum resources: 1 CPU, 512 MB RAM, 10 GB SSD
  • Ubuntu 24.04 LTS
  • Root access or sudo privileges

Important: if you are configuring OpenSSH on a public server, make sure to set it up securely – https://thehost.ua/en/wiki/root/install-openssh#dopolnitelnye-nastroyki-open-ssh

Installing OpenSSH Server

By default, OpenSSH Server is available in Ubuntu 24.04 repositories.

Update the package index and install OpenSSH Server:

sudo apt update
sudo apt install -y openssh-server

Check the SSH service status:

sudo systemctl status ssh

openssh_status

Expected output:

● ssh.service - OpenBSD Secure Shell server  
   Active: active (running)

If the service is not running — start it manually:

sudo systemctl start ssh --now

To test the connection from your local machine:

ssh user@SERVER_IP

For more connection methods, refer to our guide.

Note: replace user and SERVER_IP with your actual login and IP address.

If you use SSH keys — connect using the -i flag:

ssh -i ~/.ssh/id_rsa user@SERVER_IP

Configuring Logging

Let’s enable verbose logging. Open the configuration file:

sudo nano /etc/ssh/sshd_config

Add or modify the following parameter:

LogLevel VERBOSE

LogLevel

Save and restart the service:

sudo systemctl restart ssh

To view logs:

sudo journalctl -u ssh

journalctl

Or check system logs:

sudo tail -f /var/log/auth.log

Additional OpenSSH Settings

To improve server security, it is recommended to change default settings.

Disable Password Authentication

Password-based authentication is a common target for brute-force attacks. Disabling it significantly reduces the risk of compromise. After this, only SSH key-based connections will be allowed.

PermitRootLogin without-password
PasswordAuthentication no

password

Enable Key-Only Login

To further enhance security, enforce login using SSH keys only. This completely disables password login.

PubkeyAuthentication yes

pubkey

Change the Default Port (Default is 22)

Port 22 is frequently scanned by attackers. Changing it to a non-standard port helps reduce automated attacks.

Port 2222

port

Note: changing the port helps reduce the number of automated attacks.

Restrict Access by IP

To restrict access by IP address, use iptables or ufw.

UFW:

sudo ufw allow from <YOUR_IP> to any port 2222 proto tcp
sudo ufw enable

iptables:

# Allow new connections to port 2222 only from your IP
sudo iptables -I INPUT -p tcp -s <YOUR_IP> --dport 2222 -m conntrack --ctstate NEW -j ACCEPT

# Block all other connections to port 2222
sudo iptables -A INPUT -p tcp --dport 2222 -j DROP

Note: replace <YOUR_IP> with your actual IP address (e.g., 203.0.113.45). The -I rule (insert) places the allow rule at the top for priority processing.

Important: before testing, keep your current SSH session or open a VNC console to avoid locking yourself out.

Test connection:

ssh -p 2222 user@SERVER_IP

After making changes, be sure to restart SSH:

sudo systemctl restart ssh

Service Check

To check if the service is running:

sudo systemctl status ssh

openssh_status

To check login logs:

sudo grep sshd /var/log/auth.log

Expected output:

Accepted publickey for user from IP port ...
Common Errors
Error/Symptom Cause/Solution
Connection refused SSH server not running or blocked by firewall
Permission denied (publickey) Access denied (publickey)
Port 22 not open A non-standard port is used — specify -p or configure your SSH client
Cannot restart ssh: failed SSH restart failed — check /etc/ssh/sshd_config for issues

Diagnostic Commands

To check firewall status use next command.

When using UFW:

sudo ufw status verbose

ufw_status_verbose

If using iptables:

iptables -L -v -n

View recent login attempts:

sudo last -a | grep ssh

Another helpful command to show active logins and IPs:

w

who log in