I am debugging something related to LDAP loggins, Ldap SSH Login not working - Same configs worked on 20+ other servers - Ubuntu and I noticed that in some servers, the logs use localhost and in others they have the hostname. Having the hostname seems to make the most sense, especially if we were to centralize the log files.

How is this configured? Why isn't it default to use hostname? Just kinda made me curious...

Examples :

Oct 29 11:23:56 daily sshd[20625]: pam_unix(sshd:session): session opened for user LDAPUSERNAME by (uid=0)


Oct 29 10:56:36 localhost sshd[2560]: pam_unix(sshd:auth): check pass; user unknown


user@qa-ops:~$ hostname
user@daily:~$ hostname

Perhaps it has to do with Daily having a full domain name in hostname? I always thought /etc/hostname was just supposed to be the short part of the hostname, not the full hostname.domain.com etc.

  • More recent versions of syslog will show square brackets in the hostname field too, so will read [localhost] which may be indicative of this problem.
    – Criggie
    Jan 10 at 2:03

1 Answer 1



Rsyslog uses the glibc routine gethostname() or gethostbyname() to determine the hostname of the local machine. The gethostname() or gethostbyname() routine check the contents of /etc/hosts for the fully qualified domain name (FQDN) if you are not using BIND or NIS.

More specifically, if the localhost entry for your IP comes first in /etc/hosts, then it will take precedence.

(Assuming that files is the first value in the hosts: line in /etc/nsswitch.conf. Or alternatively, that your hostname cannot be resolved using DNS).

The Arch Wiki page goes on to explain:

You can check what the local machine's currently configured FQDN is by running hostname --fqdn. The output of hostname --short will be used by rsyslog when writing log messages. If you want to have full hostnames in logs, you need to add $PreserveFQDN on to the beginning of the file (before using any directive that write to files). This is because, rsyslog reads config file and applies it on-the-go and then reads the later lines.

The /etc/hosts file contains a number of lines that map FQDNs to IP addresses and that map aliases to FQDNs. See the example /etc/hosts file below:


#<ip-address> <hostname.domain.org>   <hostname>
#<ip-address>      <actual FQDN>                       <aliases> localhost.localdomain somehost.localdomain  localhost somehost
::1               localhost.localdomain somehost.localdomain  localhost somehost

localhost.localdomain is the first item following the IP address, so gethostbyname() function will return localhost.localdomain as the local machine's FQDN. Then /var/log/messages file will use localhost as hostname.

To use somehost as the hostname. Move somehost.localdomain to the first item:


#<ip-address> <hostname.domain.org>                           <hostname>
#<ip-address>      <actual FQDN>                                              <aliases> somehost.localdomain localhost.localdomain  localhost somehost
::1               somehost.localdomain localhost.localdomain  localhost somehost

It might be hard to tell exactly how the relevant information is selected from /etc/hosts (or the DNS) in various circumstances. Reading the source code again, I think rsyslog tries to resolve the system hostname (output of hostname command) into an FQDN.

I think this means where it says "gethostname or gethostbyname" above, it should really say "gethostname and gethostbyname". So those instructions could probably be improved, but they at least point you at the right place.

There is also a very similar question asked on the rsyslog-users mailing list.

  • Ill test this later and accept! Seems preferred to have a hostname in logs so id probably make this change to all my servers! Thanks for a really good answer. Oct 29, 2016 at 21:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .