Can't ping to another linux machine by hostname
TL;DR
If you can't control the both the DHCP server and the name lookup service for your network, you're limited in what you can do. Typical options are /etc/hosts and a dynamic DNS service, but if you don't have root on either box then you'll just have to use the IP address directly.
Resolving Names
You can't look up a host by name unless you are able to resolve it. The Debian manual explains:
8.3.1. Name Resolution The mechanism for name resolution in Linux is modular and can use various sources of information declared in the /etc/nsswitch.conf file. The entry that involves host name resolution is hosts. By default, it contains
files dns
, which means that the system consults the /etc/hosts file first, then DNS servers. NIS/NIS+ or LDAP servers are other possible sources.
Since your comment says the target host has a dynamic IP address, if Raspberry2 doesn't update DNS (or one of the other data sources) when it gets its IP address, then you have to resolve it through a static entry in your /etc/hosts file or simply pass an IP address as you already do.
If your Raspberry Pi device is inside a private network, and if you can install the necessary software on it, a dynamic DNS service may still help if the client supports looking up the IP assigned to the host's network interface, rather than just a publicly-resolvable IP address. Your mileage with dynamic DNS will most definitely vary.
Related videos on Youtube
sanderfcb95
Updated on September 18, 2022Comments
-
sanderfcb95 over 1 year
When i try to ping to another linux machine in my local network i didn't work when i insert the host-name in the ping command.
sudo ping Raspberry2
It only works when i insert the IP address in the command
sudo ping 192.168.1.15
Does anyone have a solution?
-
schaiba over 7 yearsDo you have an entry for Raspberry2 in
/etc/hosts
? -
sanderfcb95 over 7 yearsThat is a possibility but because we do this in school we can't use a fixed IP address it changes every week
-
-
schaiba over 7 yearsThis is a question, or two, not an answer.
-
Brett Levene over 7 yearsFair comment, I've edited the answer to be precise. I would have put it in a comment but I do not have the right yet. Apologies.