C
Linux

hostnameコマンドは何気に多機能かも

More than 1 year has passed since last update.

多機能と言うほどではないけど、オプションは何気に結構あったり。

aliasとかfqdnで結果返せるのね。

$ hostname --help

Usage: hostname [-b] {hostname|-F file} set host name (from file)
hostname [-a|-A|-d|-f|-i|-I|-s|-y] display formatted name
hostname display host name

{yp,nis,}domainname {nisdomain|-F file} set NIS domain name (from file)
{yp,nis,}domainname display NIS domain name

dnsdomainname display dns domain name

hostname -V|--version|-h|--help print info and exit

Program name:
{yp,nis,}domainname=hostname -y
dnsdomainname=hostname -d

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

Description:
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.

yp も dns も シンボリックリンク。

$ rpm -ql hostname-3.13-3.el7.x86_64 | grep bin |  xargs ls -li

33663457 lrwxrwxrwx. 1 root root 8 Jan 20 2015 /bin/dnsdomainname -> hostname
33663500 lrwxrwxrwx. 1 root root 8 Jan 20 2015 /bin/domainname -> hostname
33663501 -rwxr-xr-x. 1 root root 15768 Jun 10 2014 /bin/hostname
33663502 lrwxrwxrwx. 1 root root 8 Jan 20 2015 /bin/nisdomainname -> hostname
33663503 lrwxrwxrwx. 1 root root 8 Jan 20 2015 /bin/ypdomainname -> hostname

息抜きにソースみてみっか。

yum-utilsでソースダウンロード。

$ yum install yum-utils

$ yum --downloadonly --source hostname
$ rpm -ivh hostname-3.13-3.el7.src.rpm
$ rpm2cpio hostname-3.13-3.el7.src.rpm| cpio -id
$ tar xvzf hostname_3.13.tar.gz

argv[0]みて動き変えるはUNIXだとよくやるよね。こういうのって昔はハードリンクだったのが多かったけど今は違うのかな。


hostname.c

    /* If called as 'dnsdomainname', by default show the DNS domain name. */

progname = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
if (!strcmp(progname, "dnsdomainname"))
type = DNS;
else if (!strcmp(progname, "domainname"))
type = NIS;
else if (!strcmp(progname, "ypdomainname"))
type = NIS_DEF;
else if (!strcmp(progname, "nisdomainname"))
type = NIS_DEF;