In Unix (Solaris) is there any command that returns the hostname and domain name together?
For instance:
hostname -> servername
domainname -> us.xyz.com
I need : servername.us.xyz.com
In Unix (Solaris) is there any command that returns the hostname and domain name together?
For instance:
hostname -> servername
domainname -> us.xyz.com
I need : servername.us.xyz.com
The command
hostname --fqdn (or -f)
might also do what you want or not since on my system I get (none) when I run domainname
This will work if your domain is set correctly in resolv.conf. You can also use the domainname command the others have mentioned if your NIS domainname is the same as your DNS domain.
echo `uname -n`.`awk '/^domain/ {print $2}' /etc/resolv.conf`
This one has been bugging me for years, too. I just work around it by saying
$(hostname).$(domainname)
You could define a shell function or alias:
fqdn () {
echo $(hostname).$(domainname)
}
check-hostname | awk '{ print $NF }'
I know this is an older thread but I had a need for pulling the hostname and domain name separately in a script.
ealgumby's response to use check-hostname was something I had never seen before so I gave it a try to great success for my needs. I would mark it as helpful but apparently I don't have the rep to do so.
I set the domain as follows looping through the output from check-hostname.
domain=`check-hostname | nawk -F\. '{for(i=2; i<NF;i++){printf $i"."}printf $NF"\n"}'`
fqdn=`hostname`'.'${domain}
I ve done a little workaround for hostname and host:
$ host $(hostname -i) | awk '{print $NF }'
(I m using Centos but it should work elsewhere)
Getting the domain without trailing dot:
$ host $(hostname -i) | awk '{print substr($NF, 1, length($NF)-1)}'
On Solaris this worked well for me: sorry for the backtick, it is the reversed quote next to number 1 on a qwerty keyboard or you can use $( command) in KSH
getent hosts (backtick) /usr/bin/hostname (backtick)
or
getent hosts $(/usr/bin/hostname)
example:
root@melauto:[/]# getent hosts $(/usr/bin/hostname)
10.4.19.241 melauto.sro.vic.gov.au melauto loghost
root@melauto:[/]#
getent queries the current name search mechanism as specified in /etc/nsswitch.conf
and returns the information that matches the search, here it returns the info as found in /etc/hosts. if you lookup for the host info for another host that is not in /etc/hosts, it will look in DNS provided that is what is defined in /etc/nsswitch.conf
In a Unix bash script, in Sun Solaris 10, I just displayed my host name by:
echo "My hostname is $(hostname)"