22

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

Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38

8 Answers8

17

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

Elwillow
  • 178
  • 5
  • 8
    According to the manpage for `hostname(1)` on Solaris 10 (on sun4v) and every older version I've used, `--fqdn` isn't available. It just tries to set the hostname to `--fqdn`. If you run it as a non-superuser for safety, it says ‘uname: error in setting name: Not owner’. This is obviously the stock Solaris `hostname`. The poster doesn't indicate whether or not they have the GNU toolset installed. – Alexios May 11 '12 at 21:57
14

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`
Nicholas
  • 161
  • 3
5

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)
}
Alexios
  • 18,757
  • 3
  • 57
  • 74
  • 4
    `domainnname` actually reports your NIS domain if you're running NIS. This is not necessarily the same as your DNS domain name. – Magellan May 09 '12 at 17:45
  • No, it isn't. But since I've managed to evade/avoid NIS since it was Yellow Pages, I never knew that. :) So *is* there a reliable way to get the domain name if the NIS one is different? – Alexios May 09 '12 at 20:33
  • You should feel blessed that you've avoided NIS. It's annoying. And even the Mozilla Thunderbird team screwed this one up since they depended on 'domainname' output for the default reply-to in 2.0. For actual domainname, I use `hostname --fqdn | cut -d. -f2-4` – Magellan May 10 '12 at 16:28
  • 1
    This works well on Linux. On Solaris at least up to 10, `hostname --fqdn` *sets* the hostname to ‘--fqdn’, which is probably a bad idea (so make sure you're not root if you try it). :) Is `--fqdn` supported on Solaris 11? – Alexios May 11 '12 at 21:54
  • 1
    Oy! Inadvertently setting the hostname would be a pain, and the OP did say Solaris. Thanks! – Magellan May 11 '12 at 21:59
4

check-hostname | awk '{ print $NF }'

ealgumby
  • 51
  • 1
  • 2
    Please explain a bit for non-experts. – vonbrand Mar 20 '13 at 19:05
  • Synopsis /usr/sbin/check-hostname Description The check-hostname script is a migration aid for sendmail(1M). This script tries to determine the local host's fully-qualified host name (FQHN) in a manner similar to sendmail(1M). If check-hostname is able to determine the FQHN of the local host, it reports success. Otherwise, check-hostname reports how to reconfigure the system so that the FQHN can be properly determined. – ealgumby Mar 20 '13 at 19:53
  • /usr/sbin/check-hostname will report the FQDN, if it is defined, on Solaris systems. Piping the output to awk and using print $NF just reports the last field of output from check-hostname, which is the FQDN. This is the most direct method I am aware of to obtain this information from the command line, assuming you have a fully qualified domain name. If not, then this will not work, but I did not feel the need to show that, as in that case hostname is all you have. If you are not using Solaris, this will not work either, but the query was directed for Solaris. – ealgumby Mar 20 '13 at 20:04
  • Seriously, I do not understand how this does not answer the question, which was: "In unix (Solaris) is there any command that returns the hostname and domain name together?" The answer is "yes" there is, and that command is: check-hostname | awk '{ print $NF }' – ealgumby Mar 20 '13 at 20:25
  • 4
    The real answer is in your comments. Giving a one-line command with little (or in your case, none) explanation is not the kind of answer this website is about. – rahmu Mar 20 '13 at 23:08
  • None of the Solaris boxes I have access to have that command; is it standard? – Michael Mrozek Mar 21 '13 at 03:21
  • Yes, check-hostname is a standard tool for sendmail as shipped with Solaris, but if you are using an older version it may not be at the nominal /usr/sbin path (Solaris 10 & 11); try /usr/lib/mail/sh if not in /usr/sbin (Solaris 9 and earlier). If locked down, it is possible sendmail has been removed, in which case check-hostname may be gone as well. If sendmail replaced with postfix, check-hostname may or may not have been left. Finally, make sure your path includes where check-hostname resides, if not invoking with the full pathname. If not, update path or specify the full pathname. – ealgumby Mar 21 '13 at 13:17
2

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}
ASG
  • 41
  • 2
1

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)}'
user1314742
  • 111
  • 1
  • 2
  • on my ubuntu, `hostname -i` print IP adress corresponding to hostname. – Archemar Jul 06 '15 at 11:20
  • Yes that's right , so I pass this IP to `host` command . This will get you something like this: `YOUR_IP.in-addr.arpa domain name pointer hostname.domainname.` with awk I get the last column which is `hostname.domainname` – user1314742 Jul 06 '15 at 13:57
  • I see, unfortunatly using `host $(hostname -i)` give me a list of 8 hostname, all with same IP, and no hostname corresponding to my hosts – Archemar Jul 06 '15 at 14:01
  • are you sure you do not have any aliases for `host` command? it seems that your command `host` has an alias to `host -a` – user1314742 Mar 19 '16 at 13:06
0

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

Anthon
  • 78,313
  • 42
  • 165
  • 222
Blaise
  • 1
  • 1
-2

In a Unix bash script, in Sun Solaris 10, I just displayed my host name by:

echo "My hostname is $(hostname)"