How to Securely Connect to an Ubuntu 22.04 Server with SSH?
Learn how to securely connect to your Ubuntu 22.04 server using SSH, even if you're new to SSH keys. We'll cover best practices for ensuring a safe remote access experience.
ubuntusecurityssh
Nano
VPS
1
vCPU
1 GB
Memory
10 GB
NVMe Disk
512 GB
Traffic
2.90€
/month
* Up to 3GB RAM, 30GB NVMe Disk Space and 1Gbit/s Network Speed
### **Getting Started**
**Requirements:**
* An Ubuntu 22.04 server (such as an EcoStack Cloud VPS) with a static IP address.
* An SSH client (like PuTTY on Windows or OpenSSH on macOS/Linux)
* Administrative access to your Ubuntu 22.04 server.
**Initial Steps:**
1. **Update Your System:** Ensure your Ubuntu 22.04 server is up-to-date:
```bash
sudo apt update
sudo apt upgrade
```
2. **Install OpenSSH Server (if needed):** If not already installed, install the OpenSSH server package:
```bash
sudo apt install openssh-server
```
### **Strengthening SSH Security**
1. **Change the Default SSH Port:** This makes it harder for automated attacks to find your SSH server. Edit the SSH configuration file:
```bash
sudo nano /etc/ssh/sshd_config
```
Find the line `#Port 22` and change it to `Port [your_chosen_port]` (e.g., `Port 2468`). Save and exit.
2. **Disable Root Login:** Never allow direct SSH login as the root user. In the same configuration file, find `PermitRootLogin` and change it to:
```
PermitRootLogin no
```
3. **Password Authentication vs. Key Authentication:**
* **Password Authentication (Less Secure):** This is the default method, but less secure. Choose a very strong password if you use this.
* **Key Authentication (More Secure):** SSH keys offer significantly better security.
* **If you can generate keys:** Follow online guides for creating SSH key pairs on your local machine. Then transfer the public key to your server using `ssh-copy-id`.
* **If you can't generate keys:** Don't worry! You can still improve security. Follow the next step.
4. **(Optional) Strengthen Password Authentication:** If you're using passwords, add these extra security measures:
* **Fail2Ban:** Install Fail2Ban to automatically ban IP addresses that make too many failed login attempts.
```bash
sudo apt install fail2ban
```
* **Limit Login Attempts:** In the SSH configuration file, uncomment or add the line `MaxAuthTries 6` to limit the number of tries.
### **Testing Your SSH Connection**
* **If you're using a custom port:** Remember to specify it when connecting:
```bash
ssh -p [your_chosen_port] your_username@your_server_ip
```
* **Troubleshooting:** If you have issues, check the SSH server logs for clues:
```bash
sudo journalctl -u sshd.service
```
### **Additional Security Tips**
* **Regular Updates:** Keep your Ubuntu 22.04 server and SSH client software updated.
* **Strong Passwords/Passphrases:** Use long, complex passwords or passphrases.
* **Firewall:** Consider using a firewall like UFW to further restrict access.
* **Two-Factor Authentication (2FA):** If possible, set up 2FA for even greater security.