15

How do I get the host name from /etc/hosts? by writing hostname? And what about the dns domain name, how do I get that? How do I get these names through the commandline?

16

When you type

hostname

it will show you the value that is stored in

/etc/hostname

See hostname --help for a lot of options. From the help ...

-s, --short            short host name
-a, --alias            alias names
-i, --ip-address       addresses for the host name
-I, --all-ip-addresses all addresses for the host
-f, --fqdn, --long     long host name (FQDN)
-A, --all-fqdns        all long host names (FQDNs)
-d, --domain           DNS domain name
-y, --yp, --nis        NIS/YP domain name
-b, --boot             set default hostname if none available
-F, --file             read host name or NIS domain name from given file

This command can get or set the host name or the NIS domain name. You can also get the DNS domain or the FQDN (fully qualified domain name). Unless you are using bind or NIS for host lookups you can change the FQDN (Fully Qualified Domain Name) and the DNS domain name (which is part of the FQDN) in the /etc/hosts file.


So

hostname -f

for the long host name (FQDN).

  • I get: "hostname: Name or service not known" – dada dudu Oct 13 '16 at 12:57
  • Do you have something in /etc/hostname? That one we here typically edit for apache. example topic: askubuntu.com/a/218499/15811 – Rinzwind Oct 13 '16 at 13:02
  • Have tried to cat /etc/hosts I get a localhost which I shouldn't as I run it on a VM – dada dudu Oct 13 '16 at 13:07
  • 1
    @dadadudu localhost is just a nickname for “myself” on a network. All computers (including VM’s) can connect to themselves using localhost address. – Melebius Oct 13 '16 at 13:15
  • @dadadudu the VM I have here has its own name (it is called "test1"). – Rinzwind Oct 13 '16 at 13:17
0

Presuming you want your local (LAN) IPv4 address....

To avoid your server returning a long string that combines your IPv4 and IPv6 addresses, use this programmatically in a bash script:

LOCALIP=$(hostname -I | awk '{print $1}')

Or type this on the CLI:

hostname -I | awk '{print $1}'
  • This only works well if the host has only a single IPv4 address. IPv6 hosts typically have multiple IPv6 addresses (one permanent and one or more temporary). – David Foerster Jan 1 '18 at 23:21
  • Right. That's why the 1st line of my suggestion is, "Presuming you want your local (LAN) IPv4 address...." But to your point, 'hostname' is not always the best tool for IP info. Depends on the goal. The OP was not detailed in his/her question about what they wanted (presumably the FQDN, but that is speculation on my part). – MrPotatoHead Jan 2 '18 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.