SSH is the secure transmission network protocol that transfers the data files from one device to another (between local or remote servers) securely. This requires the correct hostname and IP address on both devices (client and server side) to create a secure SSH connection. The SSH hostname error occurs if the SSH credentials are not authenticated for the client and local server.
This post will provide all the reasons and possible solutions to fix the error. The content addressed in this post is:
- Reason 1: Hostname is not Configured Correctly
- Solution: Use the Correct SSH Hostname
- Reason 2: Hosts File is not Configured
- Solution: Configure /etc/hosts File
Let’s describe the causes and solutions for the SSH error.
Reason 1: Hostname is not Configured Correctly
SSH could not resolve hostname occurs when the hostname or ip address is incorrect like the hostname has extra space or some typing error while writing the hostname or ip address. For instance, when you write the incorrect hostname, the below error shows up:
This error can be removed using the two solutions described below.
Solution: Use the Correct SSH Hostname
The error for using the SSH protocol that the hostname can not be resolved can be removed using the correct hostname and ip address. The correct syntax of connecting an SSH connection is provided below:
$ ssh <host-name>@<ip-address>
OR
$ ssh <ip-address> -l <host-name>
The IP-address can be found using “ip addr” command and hostname using the “hostname” command. For instance, to create an SSH connection with a server having username “itslinuxfoss” and ip address “192.168.18.18”, use the below command:
$ ssh [email protected]
The SSH connection is successfully established.
Reason 2: Hosts File is not Configured
This error can occur if the hosts file is not configured or the hosts file is pointing toward the wrong SSH address. We can fix the SSH hostname could not be resolved error by manually adding the correct hostname and ip address in the “/etc/hosts” file.
Solution: Configure /etc/hosts File
Follow these steps to configure the host’s configuration file:
Open the system hosts configuration /etc/hosts in nano editor by using:
$ sudo nano /etc/hosts
The above interface for the /etc/hosts file will open up, showing the hosts already exist in the system.
Add an entry for your desired IP address, and hostname in the /etc/hosts opened file, whose syntax is provided below:
$ <ip-addr> <host-name>
- <ip-addr>: Replace it with the remote server IP address.
- <host-name>: Replace with system remote user name.
For instance, to create an SSH connection with the remote server having IP address “192.168.227.132” and username “itslinuxfoss”, add the below information in the /etc/hosts file:
$ 192.168.227.132 itslinuxfoss
After adding the remote user’s IP address and username, press the “Ctrl + O” to save the file changes. Keeping the same file name, press “Enter” and “Ctrl + X” to quit.
Create the SSH connection with the added remote server to verify the SSH connection is created using this command (Replace itslinuxfoss with your desired username):
Note: Enter “yes” to confirm creating the connection and enter the remote server password when asked.
$ ssh itslinuxfoss
The output shows the local server is successfully connected to the remote server (itslinuxfoss) via SSH.
Conclusion
The SSH could not resolve the hostname error that occurs if the hostname and ip address of local and client servers are incorrect. To fix the SSH hostname error, enter the correct server credentials or add the correct SSH entry in the “/etc/hosts” file as described in this guide.