SSH connection works only with IP address and not domain name
I created a new Linode and set up SSH, but can only log in with the IP address and not the domain name.
This command logs me in: SSH example_user@<IP4>
This command generates an error: SSH example_user@domain.com
Error message:
ssh: Could not resolve hostname domain.com: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server.
STEPS TO SET UP THE SERVER:
Server setup:
Distribution: Debian 12
Region: Chicago, IL (us-ord)
Linode Plan: Shared CPU > Nanode 1GB $5
Linode Label: debian-us-ord-domain
Add Tags: <blank>
Root Password: <entered password>
SSH Keys: <laptop label>
Assign VPC: None
Assign Firewall: None
<Left everything else blank for the setup>Domain setup for Hostname, IP Address, and TTL:
domain.com <IP4 address> Default domain.com <IP6 address> Default mail <IP4 address> Default mail <IP6 address> Default www <IP6 address> Default www <IP4 address> Default
Connect to the server through SSH:
ssh root@<IP4>
Windows 11
known_hosts
file has legitimate IP addresses and no obsolete IP addressesUpdate and upgrade the packages
sudo apt update && sudo apt upgrade
Restart SSH on Debian 12 server
sudo systemctl restart ssh
Set the timezone
timedatectl set-timezone 'America/Chicago'
Open the hosts file with Nano.
nano /etc/hosts
Add the <ip4> and <ip6> lines to the /etc/hosts file after the 127.0.0.1 where <ip4> is the xxx.xxx.xxx.xxx numeric address and <ip6> is the xxxx.xxxx.xxxx.xxxx.xxxx.xxxx alphanumeric address:
127.0.0.1 localhost.localdomain localhost <ip4> www.boldpostprojects.com boldpostprojects.com <ip6> www.boldpostprojects.com boldpostprojects.com
Create the user name (I replaced example_user with a user name):
adduser example_user
Add example_user to the sudo group so it will have root user capabilities:
adduser example_user sudo
Log back into the server
ssh example_user@<IP4>
Create the authorized_keys file to store the SSH public key on the server side:
sudo mkdir -p ~/.ssh && cd .ssh && sudo touch authorized_keys && cd .. && sudo chmod -R 700 ~/.ssh/ && sudo chmod 600 ~/.ssh/authorized_keys
Exit from the server
exit
From the client side (the laptop), copy the public SSH key from the laptop to the server:
scp C:\Users\sbrew\.ssh\id_ed25519.pub example_user@172.234.18.254:~/.ssh/authorized_keys
Reboot the server
Log back into the server
ssh example_user@domain.com
I get the following error:
ssh: Could not resolve hostname domain.com: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server.
I am able to log in successfully with ssh example_user@<ip4>
It seems that the addition of the following lines in the /etc/hosts file are what is needed to log in with the following line:
ssh example_user@domain.com
What am I missing?
2 Replies
I followed our guide to Setting up and Securing a Compute Instance and was able to change my hostname to my domain and log in that way.
One command I'm not seeing in your output is set-hostname
, which would look like this using your example.
hostnamectl set-hostname domain
Then I updated the host file with a text editor and filled in my domain and public IP into the format below:
nano /etc/hosts
...
# /etc/hosts
$PUBLICIP www.domain.com domain
...
Next, I made sure there was an A record pointing my IP toward my domain and that the records fully propagated.
After that, if I log in using the IP address it works, but it also works with the domain as well. I didn't create a limited user here, but I have another server set up with a limited user that works, so I don't think that's an issue. Though that is Debian 11.
One thing I can't account for is your use of Windows. I don't have a Windows machine to test on, but again, I don't think this should be an issue.
Thanks, CassandraD. I do use the hostnamectl set-hostname domain
command. If you work on several servers, that is handy since it sets the name in the prompt. If you're not sure which server you're working in, then the prompt will quickly let you know since the hostnamectl set-hostname domain
command has set the unique name in the server prompt for you. But that doesn't do anything for my SSH login.
Typically, the /etc/hosts
file matches the ip address to the domain name for the SSH login to use. The line that matches the www.domain.com
and domain.com
names for me is:
172.245.26.216 www.domain.com domain.com
Once that line is added to the /etc/hosts
file, then I should be able to log into the server with SSH by using any one of the following three commands from my laptop (assuming root hasn't been disabled):
SSH root@172.245.26.216
SSH root@domain.com
SSH root@www.domain.com
But for some reason, after adding that line to the domain.com
file and saving it, I still can only log in with
SSH root@172.245.26.216
I still can't log in with either of the following:
SSH root@domain.com
SSH root@www.domain.com
So I'm puzzled as to what I haven't set up properly.