hostname
Command LimitationThis is the first of a new monthly feature on the BRUDEN-OSSG "Think Link" that will focus on UNIX and/or Linux tips and tricks. Without further ado, here is UNIX/Linux Tip #1!
The hostname
(1) command on the HP-UX and Solaris operating systems is missing a command option that is often used by other UNIX/Linux operating systems.
This option is the "-s
" (also "--short
" on Linux) option which is used to return the shortened version of the system's hostname.
Veteran HP-UX and Solaris system administrators are probably now thinking, "I just pipe the output of the hostname
command into the cut
command which will accomplish the same thing." And, they are correct.
# hostname
gkar.bruden.com
# hostname | cut -d. -f1
gkar
Why is not having this option a big deal? Well, if your background includes a UNIX/Linux operating system that has this command option,
your fingers are likely to rattle off "hostname -s
" before your brain catches up (or perhaps you have many scripts that you would like to reuse that already contain this command).
And, if you were the superuser when you executed this command you would have changed the system's hostname to "-s
" which could be a very bad thing if not caught and corrected immediately.
# hostname -s
# hostname
-s
So, what can you do to prevent an accidental hostname change from occurring?
-s
" option on the hostname
command.-s
" option to the hostname
command.
This could take a long while before you would see this become a reality, though it certainly does not hurt to make the request anyway.
You could create a simple shell function to be able to use a "-s
" option. Below is one possible solution in the POSIX shell.
# function hostname
> {
> if [[ "${1:-0}" = "-s" ]]; then
> /bin/hostname | cut -d. -f1
> else
> /bin/hostname $1
> fi
> }
# hostname
gkar.bruden.com
# hostname -s
gkar
In order to make this solution work every time you log into your account, add the function
to your .profile
file (or place it in /etc/profile
to create the function
for all users).
You could write a script instead of creating a shell function
.
Given that the shell function
solution is short and simple, why would you bother to write a script?
Good question! Well, one reason would be so that you could enhance the command beyond adding a single option.
For example, we have written a Perl script as a wrapper to the hostname
command that in addition to having a "-s
" option will also prompt you prior to allowing you to change the system's hostname.
# cat /usr/local/scripts/hostname
#!/usr/bin/perl
#
# hostname
#
# This script is a wrapper for the /bin/hostname command on HP-UX
# to workaround its lack of a "-s" option.
#
# Additionally, this script performs a verification step so that there
# is not an accidental setting of the hostname on a system.
#
# ============================================================================
# Modification History
#
# Date Author Comment
# -------- ----------------------- -----------------------------------------
# 20080128 Scott Fafrak Created.
# 20081119 Scott Fafrak Added the force option.
#
# ============================================================================
#
use strict;
use Carp;
use English;
use File::Basename;
use Getopt::Long
$OUTPUT_AUTOFLUSH++;
#
# Operating System Commands
#
my $HOSTNAME = "/bin/hostname";
#
# Variables
#
use constant SUCCESS => 0;
use constant FAILURE => 1;
my $STATUS = FAILURE;
my $PROMPT = "no";
my $PROGNAME = basename $0;
my $USAGE = "
USAGE: $PROGNAME [-h|--help][-s|--short][-f|--force] [newHostname]\n"
#
# Parse the command line
#
my ($help, $short, $host);
GetOptions ("help" => \$help,
"short" => \$short
"force" => \$force)
or die $USAGE;
if ($help || @ARGV > 1) { print $USAGE; exit SUCCESS; }
#
# If we get here we have either a "-s" or we're setting
# the hostname.
#
if (@ARGV == 1)
{
die "ERROR: You must be root to set the hostname\n"
unless ( $UID == 0 );
if ($force)
{
$PROMPT = "yes";
}
else
{
print "Are you sure you want change the hostname? (yes|[no]) ";
chomp ($PROMPT = lc ());
}
if ($PROMPT =~ /^y/)
{
$STATUS = system($HOSTNAME, $ARGV[0]);
croak "ERROR: $HOSTNAME failed: $?" unless ($STATUS == SUCCESS);
}
else
{
print "No change will be made\n";
}
}
else
{
chomp ($host = qx("$HOSTNAME"));
if ($short)
{
$host = (split ('\.', $host))[0];
}
print "$host\n";
}
exit SUCCESS;
You can download the script here.
Here are a few examples of the script in use.
$ /usr/local/scripts/hostname -h
USAGE: hostname [-h|--help][-s|--short][-f|--force] [newHostname]
-s
" option.
$ /usr/local/scripts/hostname -s
gkar
$ /usr/local/scripts/hostname delenn.bruden.com
ERROR: You must be root to set the hostname
# /usr/local/scripts/hostname delenn.bruden.com
Are you sure you want change the hostname? (yes|[no]) yes
# /usr/local/scripts/hostname
delenn.bruden.com
--force
" option.
# /usr/local/scripts/hostname --force gkar.bruden.com
# /usr/local/scripts/hostname
gkar.bruden.com
This tip discusses how to work around the missing "-s
" option on the hostname
command on HP-UX and Solaris, it does not address how to permanently change the system's hostname.
Permanently changing a system's hostname is usually accomplished by modifying one or more configuration files and it seems that each operating system has its own implementation
(sounds like a topic of another time ).
We hope that you've found this tip useful. If you have any questions, comments, or suggestions for future tips, please contact us.