1833953 Members
2275 Online
110063 Solutions
New Discussion

/etc/hosts

 
Stew McLeod
Occasional Advisor

/etc/hosts

What is the max length of the host in /etc/hosts? 24 characters or 64 or 8?
5 REPLIES 5
Steven Sim Kok Leong
Honored Contributor

Re: /etc/hosts

Hi,

In unistd.h:

extern int gethostname __((char *, size_t));

man gethostname:

int gethostname(char *hostname, size_t size);

size specifies the length of the hostname array.

In _size_t.h,

# ifndef _SIZE_T
# define _SIZE_T
_NAMESPACE_STD_START
typedef unsigned long size_t;
_NAMESPACE_STD_END
# endif /** _SIZE_T **/

Thus, based on this interpretation, the maximum length of a hostname entry in /etc/hosts is unsigned long which is 4294967295.

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Steven Sim Kok Leong
Honored Contributor

Re: /etc/hosts

Hi,

To add on:

man gethostent:

gethostent() Reads the next line of /etc/hosts, opening the file if necessary.

The gethostent(), gethostbyname(), and gethostbyaddr() functions each return a pointer to a structure of type hostent, defined as follows in :

struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Bill Hassell
Honored Contributor

Re: /etc/hosts

64 characters which is found in the man page for hostname:

"The name_of_host argument is restricted to MAXHOSTNAMELEN characters as defined in ."

Typical manspeak assumes that everyone knows where is located (it's really in /usr/include/sys/param.h). The confusion over hostname lengths is historic as there are two major way to ID a system's network name.

The oldest method is uname -n which is the UUCP (Unix-to-Unix CoPy) name. UUCP is protocol used by Unix long before the popular Internet existed and involves automated modem dialing for networking. The UUCP name is limited to 8 characters (man 2 uname, look for UTSLEN).

hostname is a separate name which may be up to 64 characters in length. So the direct answer to your question is: 64

But the unasked question is: how to handle a short and long name? Start by editing the /etc/rc.config.d/netconf file and adding a new line:

NODENAME=

So the netconf file now shows:

HOSTNAME=really_long_hostname
NODENAME=host8

Now set the new values with:

# /sbin/init.d/hostname start

And test it with:

# uname -n
# hostname

Then edit /etc/hosts and put both names on the same line for your IP address as in:

12.34.56.78 host8 really_long_hostname

Now test the network resolution with:

# nslookup host8
# nslookup really_long_hostname

There are a few more details if you are using a DNS server, but the question was for /etc/hosts only. Technically, the largest entry in /etc/hosts includes long domain/organization names, things like abc.mycompany.com so the limit is much larger, but for simple networks, 64 and 8 are the two limits.


Bill Hassell, sysadmin
Steven Sim Kok Leong
Honored Contributor

Re: /etc/hosts

Hi,

I guess Bill is right. Had missed out the MAXHOSTNAMELEN.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
fg_1
Trusted Contributor

Re: /etc/hosts

Kelly

All hostnames are limited to 64 characters (A-Z, 1-9) no special characters.

The parameter MAXHOSTNAMELEN is defined in the /sys/param.h file, but it is highly recommended that you do not modify this file.

Good luck.