-2

I tried changing my hostname from localhost.localdomain of fedora machine by giving the below command

hostnamectl set-hostname example.com --static

After that I did a network restart . But when I tried to ping the example.com it doesn't map to the local ipaddress of the machine. My PC is behind a router with private address 192.168.1.2.

I checked the /etc/hostname which is updated correctly but the /etc/hosts still has the below entry

127.0.0.1       localhost.localdomain   localhost
::1             localhost6.localdomain6 localhost6

How to change hostname so that it maps to local IP ?

P.S : I tried rebooting the machine but it didn't help.

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47
Knight71
  • 103
  • 3

1 Answers1

1

ping uses DNS, not your local hostname, to resolve the IP address for example.com. You need to set the IP address in DNS.

You can do that just locally to your machine, by adding example.com to the lines for 127.0.0.1 and ::1 in /etc/hosts. That will make DNS resolve example.com to 127.0.0.1 (IPv4) or ::1 (IPv6). You'll also need to be sure that in /etc/nsswitch.conf, the word files appears before dns, so that IP resolution on your host looks at /etc/hosts first, before going to DNS.

If you want the change to take effect globally, you'll need to add a DNS A record at your DNS provider, associating your public IP address with example.com.

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47