How to Enable SSH in Linux: A Step-by-Step Guide

Introduction

Imagine you need to access a remote Linux server to troubleshoot an issue, manage files, or deploy an application. Secure Shell (SSH) is the go-to method for remote login and system administration, allowing you to securely connect to your Linux servers from anywhere. However, if SSH is not enabled, you won’t be able to establish a remote connection.

In this guide, we’ll walk you through how to enable SSH in Linux, covering installation, configuration, and security best practices. By the end, you’ll have a fully functional and secure SSH setup. Whether you’re a network administrator, system administrator, or DevOps engineer, mastering SSH is essential for efficient Linux server management.

Quick Answer Box

How to Enable SSH in Linux?

  • Install OpenSSH: Ensure SSH is installed using sudo apt install openssh-server (Debian-based) or sudo yum install openssh-server (RHEL-based).
  • Start and Enable SSH Service: Use sudo systemctl enable --now ssh.
  • Check SSH Status: Confirm SSH is running with sudo systemctl status ssh.
  • Allow SSH Through Firewall: Run sudo ufw allow ssh or sudo firewall-cmd --add-service=ssh --permanent.
  • Test SSH Connection: Connect using ssh user@hostname.

Let’s dive into the details.

Step 1: Check if SSH is Installed

Most Linux distributions come with OpenSSH pre-installed, but you can verify its presence with:

If SSH is not installed, follow the next step.

Step 2: Install OpenSSH Server

For Debian-based distributions (Ubuntu, Debian):

sudo apt update && sudo apt install openssh-server -y

For RHEL-based distributions (CentOS, Fedora, Rocky Linux):

sudo yum install -y openssh-server

Step 3: Start and Enable the SSH Service

Once installed, start and enable SSH to run on boot:

sudo systemctl enable --now ssh

To check the status of the SSH service:

sudo systemctl status ssh

Step 4: Configure Firewall Rules

If you are using UFW (Ubuntu/Debian):

sudo ufw allow ssh
sudo ufw reload

For firewalld (CentOS/RHEL):

sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --reload

Step 5: Secure Your SSH Server

Change the Default SSH Port

By default, SSH runs on port 22, making it an easy target for attackers. Change the port in /etc/ssh/sshd_config:

sudo nano /etc/ssh/sshd_config

Find and modify:

Port 2222

Restart SSH:

sudo systemctl restart ssh

Use SSH Key-Based Authentication

Instead of relying on passwords, use SSH keys for authentication:

ssh-keygen -t rsa -b 4096
ssh-copy-id user@remote-server

Enable Fail2Ban for SSH Protection

Fail2Ban helps protect against brute-force attacks:

sudo apt install fail2ban -y  # Debian-based
sudo yum install fail2ban -y  # RHEL-based

Start and enable Fail2Ban:

sudo systemctl enable --now fail2ban

Step 6: Test SSH Connection

To test your SSH setup:

ssh user@hostname_or_ip

Did You Know?

SSH was designed as a secure replacement for Telnet, which transmitted passwords in plaintext!

Important Note

Always back up your SSH configuration files before making changes:

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

Conclusion

Enabling SSH in Linux is straightforward and essential for remote system management. By securing your SSH setup, you can protect your system from unauthorized access while maintaining convenience.

Next Steps: Implement SSH best practices such as key-based authentication and firewall rules to enhance security.

Got questions? Drop them in the comments!

FAQs: How to Enable SSH in Linux

1. What is SSH, and why is it important?

SSH (Secure Shell) is a network protocol that allows secure remote login and command execution on Linux servers. It encrypts data, ensuring secure communication over an unsecured network.

2. How do I check if SSH is installed on my Linux system?

You can check if SSH is installed by running:

bashCopyEditssh -V

or

bashCopyEditwhich ssh

If SSH is not installed, you may need to install it using your package manager.

3. How do I install SSH on Linux?

For Debian-based systems (Ubuntu, Debian):

bashCopyEditsudo apt update && sudo apt install openssh-server

For RHEL-based systems (CentOS, Fedora, Rocky Linux):

bashCopyEditsudo yum install openssh-server

For Arch-based systems:

bashCopyEditsudo pacman -S openssh

4. How do I start and enable the SSH service?

Use the following commands to start and enable SSH at boot:

bashCopyEditsudo systemctl start ssh
sudo systemctl enable ssh

5. Which port does SSH use by default?

SSH uses port 22 by default. You can change it by modifying the /etc/ssh/sshd_config file and restarting the SSH service.

6. How can I check if SSH is running?

Run the following command:

bashCopyEditsudo systemctl status ssh

If SSH is active, you’ll see an “active (running)” status.

7. How do I allow SSH through the firewall?

For UFW (Ubuntu/Debian):

bashCopyEditsudo ufw allow ssh

For Firewalld (RHEL/CentOS/Fedora):

bashCopyEditsudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

8. How do I test SSH connectivity?

Use this command from another system:

bashCopyEditssh user@server-ip

Replace user with your username and server-ip with the Linux server’s IP address.

9. How do I enable SSH key-based authentication?

  1. Generate SSH keys on your local machine: bashCopyEditssh-keygen -t rsa -b 4096
  2. Copy the public key to the server: bashCopyEditssh-copy-id user@server-ip
  3. Try logging in without a password: bashCopyEditssh user@server-ip

10. How do I disable SSH password authentication for better security?

Edit the SSH configuration file:

bashCopyEditsudo nano /etc/ssh/sshd_config

Find and modify the following lines:

plaintextCopyEditPasswordAuthentication no
PermitRootLogin no

Save the file and restart SSH:

bashCopyEditsudo systemctl restart ssh

11. How do I change the default SSH port?

Edit the SSH configuration file:

bashCopyEditsudo nano /etc/ssh/sshd_config

Find #Port 22, uncomment it, and change the port number. Then restart SSH:

bashCopyEditsudo systemctl restart ssh

12. How can I prevent SSH brute-force attacks?

Use Fail2Ban to block repeated failed login attempts:

bashCopyEditsudo apt install fail2ban  # Ubuntu/Debian
sudo yum install fail2ban  # CentOS/RHEL

Then start and enable Fail2Ban:

bashCopyEditsudo systemctl start fail2ban
sudo systemctl enable fail2ban

13. How do I disable root login via SSH?

Edit the SSH configuration file:

bashCopyEditsudo nano /etc/ssh/sshd_config

Find the line:

plaintextCopyEditPermitRootLogin yes

Change it to:

plaintextCopyEditPermitRootLogin no

Then restart SSH:

bashCopyEditsudo systemctl restart ssh

14. Why can’t I connect to my SSH server?

Possible reasons include:

  • SSH is not installed or running (sudo systemctl status ssh).
  • Firewall is blocking SSH (sudo ufw allow ssh).
  • Incorrect username or IP address.
  • The SSH port has been changed (use -p <port> to specify).

15. How do I log into SSH with a different port?

If SSH is running on a different port (e.g., 2222), use:

bashCopyEditssh -p 2222 user@server-ip

Would you like any additional FAQs? 🚀

Leave a Comment

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

Scroll to Top