How to Fix the SSH “Connection Refused” Error on Linux
SSH or Secure Shell is a popular protocol used for securely connecting to remote systems. It’s a powerful tool for system administrators and developers who need to manage their servers or deploy applications from a remote location. However, sometimes you may encounter an SSH “Connection refused” error, indicating that the server is unreachable or the SSH service is down. This error can prevent you from accessing your server remotely, which can be frustrating. In this article, we’ll explore how you can fix the SSH Connection refused error on Linux.
Step 1: Verify that SSH is installed and running
The first step to fixing the SSH Connection refused error is to ensure that the SSH daemon is installed and running on your Linux system. To check if SSH is installed, run the following command in your terminal:
“`
sudo systemctl status ssh
“`
If the output shows that SSH is not installed, you can install it using the following command:
“`
sudo apt-get install openssh-server
“`
Once you have installed SSH, ensure that the SSH service is running by entering the following command:
“`
sudo systemctl start ssh
“`
You can also check the SSH status by running the following command:
“`
sudo systemctl status ssh
“`
Step 2: Check the SSH configuration file
If SSH is installed and running, the next step is to check the SSH configuration file. The file may contain incorrect settings that prevent the SSH service from starting. The SSH configuration file is located in the /etc/ssh/sshd_config directory. You can edit the file by running the following command:
“`
sudo nano /etc/ssh/sshd_config
“`
In the configuration file, look for the following settings:
“`
Port 22
PermitRootLogin yes
“`
Ensure that the Port number is set to the correct value (the default is 22), and PermitRootLogin is set to yes. Once you have made the changes, save the file by pressing CTRL+X, then Y, and finally ENTER.
Step 3: Check the firewall settings
Another common cause of the SSH Connection refused error is a firewall that blocks incoming SSH traffic. You can check if your firewall is blocking SSH traffic by running the following command:
“`
sudo ufw status
“`
If the firewall is enabled, you can allow SSH traffic by running the following command:
“`
sudo ufw allow ssh
“`
Step 4: Check the SSH key settings
SSH keys are used to authenticate remote connections. If the SSH keys are not set up correctly, you may have trouble connecting to the server. You can check if the SSH keys are set up by running the following command:
“`
cd ~/.ssh && ls
“`
If you don’t see any files in the directory, you can generate a new SSH key pair by running the following command:
“`
ssh-keygen -t rsa -b 4096 -C “[email protected]”
“`
Once you have generated the key pair, you can copy the public key to your remote server by running the following command:
“`
ssh-copy-id user@server_ip
“`
Replace “user” with your username and “server_ip” with your server’s IP address.