Linux Maximum Length Of A Host Name

While browsing Linux kernel source code I came across POSIX (Portable Operating System Interface) variable called _POSIX_HOST_NAME_MAX . It defines the maximum length of a host name (not including the terminating null) as returned from the gethostname function.

ADVERTISEMENTS

This variable sets the limit to the number of characters in a hostname under Linux.
$ grep '_POSIX_HOST_NAME_MAX' /usr/include/bits/posix1_lim.h
Output:

#define _POSIX_HOST_NAME_MAX    255

You can change limit and recompile the kernel to get bigger hostname.

See comment below for correct information.

Updated for accuracy.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • Jeff May 1, 2008 @ 1:14

    This is a VERY bad idea. The length of hostnames is a standard defined by the RFC’s.

    If anyone adjusts this to excess of the standards and has problems, they are just a idiot to be laughed at.

  • Jeff Schroeder May 1, 2008 @ 12:29

    This is one that you are actually wrong on.

    The Single Unix Specification version 2 (SuSv2) guarantees ‘Host names are limited to 255 bytes’. POSIX 1003.1-2001 guarantees ‘Host names (not including the terminating NULL) are limited to HOST_NAME_MAX bytes’.

    $ grep HOST_NAME_MAX /usr/include/bits/local_lim.h
    #define HOST_NAME_MAX 64

    $ getconf HOST_NAME_MAX
    64

    This is part of the c library you use and libc defaults to 64. Changing your kernel and recompiling would still give you a max hostname size of 64 characters including the null termination byte at the end.

    Can you switch your blog over to recaptcha for better captchas and do the world some good?
    http://recaptcha.net/plugins/wordpress/

    http://www.digitalprognosis.com/blog

  • 🐧 nixCraft May 1, 2008 @ 16:54

    Thanks for correct information, The post has been updated.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.