When system not applying hostname [hostname: the specified hostname is invalid]
A few days ago, I've found a strange problem on one Ubuntu server (12.04) instance. It was a Xen vps from one of the Finnish providers, the problem is that the hostname does not read by system as expected (from /etc/hostname).
Sure, /etc/hostname contain absolutely correct hostname, but when I tried
root@(none): hostname -F /root/hostname
I get just
hostname: the specified hostname is invalid
Also comands like
start/restart hostname or
/etc/init.d/hostname reload/stop/start always fails with very informative:
: Job failed to start
strace hostname -F /etc/hostname also not showing any interesting information. So the answer will in the hostname (/bin/hostname). When I look in the end of strace output I sow this:
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb8e529c000
read(3, "example.org\r\n", 4096)= 9
What the hell? How can windows-like new line character be in that file.
/root/hostname: ASCII text, with CRLF line terminators
file tell me what was the reason of hostname command fails.
And one very useful thing is od.
root@(none): cat /etc/hostname | od -c
0000000 e x a m p l e . o r g \r \n
Will show ASCII characters, including terminator.
To convert CR-LF to Unix-like LF vim is the easy way, just do :set ff=unix or :set fileformat=unix and save file.
So, problem is found. Someone (very good person) write hostname from windows like editor to /etc/hostname, and /bin/hostname can't normally read this file due CR-LF terminator.
And the last correct way to set hostname (if something goeing wrong with hostname command) is to set kernel parameter in sysctl.conf file (/etc/sysctl.conf).
Syntax is pretty simple:
kernel.hostname = example.org
then, reload kernel parameters
(Big thanks to provider admins for installing Ubuntu from broken template or for use Windows to write system configs in Linux).