How to get the hostname from /etc/hostname & DNS domain name?

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).


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}'