• Home
  • Disclaimer
  • Contact
  • Archives
  • About
  • Subscribe
  • Support
  • Advertise

Kernel Talks

Unix, Linux, & Cloud!

  • How-to guides
    • Howto
    • Disk management
    • Configurations
    • Troubleshooting
  • OS
    • HPUX
    • Linux
  • Miscellaneous
    • Software & Tools
    • Cloud Services
    • System services
    • Virtualization
  • Certification Preparations
    • AWS Certified Solutions Architect – Associate
    • AWS Certified Solutions Architect – Professional
    • AWS Certified SysOps Administrator – Associate
    • AWS Certified Cloud Practitioner
    • Certified Kubernetes Administrator
    • Hashicorp Certified Terraform Associate
    • Oracle Cloud Infrastructure Foundations 2020 – Associate
  • Tips & Tricks
  • Linux commands
You are here: Home / Linux

All you need to know about hostname in Linux

Published: May 31, 2017 | Modified: June 24, 2020



Learn what is hostname, how to set hostname and how to change hostname in Debian and RedHat based Linux systems.

Learn about hostname in Linux

The hostname is a prime identity of Linux servers in the human world! Obviously, the IP address is the main component to identify the system in the environment. In this article, we are going to see anything and everything about the hostname. We will walk through what is the hostname, how to set hostname, how to change hostname etc. Let’s start with the basics of the hostname.

What is hostname

The hostname is the humanly readable identity of the server. Any server is identified by IP address in the network but to identify easily hostname is also given. Normally FQDN (Fully Qualified Domain Name) is expected for the system but even Domain name (the name before the dot) is also fine for systems under private networks. The hostname can be alpha-numeric

Generally hostname standards to the maximum of 255 bytes long. But normally people prefer to keep it 10-12 characters long so that it’s easy to remember. Kernel variables _POSIX_HOST_NAME_MAX or HOST_NAME_MAX defines your current max limit of hostname. You can get their values using getconf a command like below :

# getconf HOST_NAME_MAX
64

How to set hostname in Linux

A quick command in all-new Linux distros is hostnamectl. Use set-hostname switch and your new hostname as an argument.

# hostnamectl set-hostname kerneltalks

For more details read on …

Hostname is defined in files

  • /etc/hosts for networking
  • /etc/hostname : This will be read by boot scripts on boot time and set its value.
  • /proc/sys/kernel/hostname : Current hostname.
  • /etc/sysconfig/network : Networking (HOSTNAME=”server1″ parameter)

In above files, you can only view current hostname (being used by the live kernel) under proc file only. Rest all files are used to lookup or set hostname at boot time. So if you change hostname using hostname command then it won’t reflect in rest files. It will only reflect in the proc file.

You can set the hostname of your choice in /etc/hostname or /etc/sysconfig/network and restart network service to notify kernel about it.

How to change hostname in Linux

The current hostname can be checked by typing hostname command without any argument. The hostname can be changed by simply using hostname command followed by the name of your choice.

Cautions : Do not change hostname on live production systems!

# hostname
server5
# hostname kerneltalks.com
# hostname
kerneltalks.com

Please make a note that change is dynamic and not permanent. After the system reboot, the hostname will be returned to what it was earlier.

Change hostname permanently in Linux

On RedHat systems : You can edit file /etc/sysconfig/network (define in HOSTNAME=”xyz”)  & reboot system

# cat /etc/sysconfig/network
HOSTNAME=kerneltalks.com

On Debian systems : You can edit file /etc/hostname & call /etc/init.d/hostname.sh script (/etc/init.d/hostname.sh start)

You can even change the hostname using the system control command. Use parameter kernel.hostname and define its value like below :

# sysctl kernel.hostname=kerneltalks
kernel.hostname = kerneltalks

On Suse systems: Edit file /etc/HOSTNAME and add hostname in it. There will be no parameter and value format. Only you have to enter hostname like below :

# cat /etc/HOSTNAME
kerneltalks.com

Change hostname permanently in clone, template VM & cloud clones

If you have a system which is prepared using clone, template from VMware or cloud clone deploy then you should do the following :

Edit file /etc/cloud/cloud.cfg and change parameter 'preserve_hostname' to true. You can do it using one-line sed script as below :

root@kerneltalks # sed --in-place 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg

Also, change DHCP related parameter DHCLIENT_SET_HOSTNAME in file /etc/sysconfig/network/dhcp to no. So that hostname wont be changed by DHCP in the next reboot. Again, you can use one line sed to do that as below :

root@kerneltalks # sed --in-place 's/DHCLIENT_SET_HOSTNAME="yes"/DHCLIENT_SET_HOSTNAME="no"/' /etc/sysconfig/network/dhcp

That’s it. These are two extra steps you need to take on cloud or VM servers.

How to configure FQDN in Linux

Another thing around the hostname is to set FQDN for Linux server i.e. Fully Qualified Domain Name. Generally you should be doing in via DNS in your environment but /etc/hosts always get checked first. So its good practise to define FQDN at /etc/hosts file

Use <IP> <FQDN> <Hostname> format to add/edit entry in /etc/hosts and you are good to go. Sample entry below –

root@testsrv1 # echo "10.1.1.5 testsrv1.kerneltalks.com testsrv1">>/etc/hosts

You can verify Linux server’s FQDN by using command hostname -f

root@testsrv1 # hostname -f
testsrv1.kerneltalks.com
⇠ Previous article
How to tune kernel parameters in Linux
Next article ⇢
Network routes in Linux

Related stuff:

  • cut command and its examples
  • How to configure NTP client in Linux
  • 5 different examples to send email through Linux terminal
  • How to remove password expiry in linux
  • Difference between /etc/passwd and /etc/shadow
  • Documentary films on Linux!
  • RHEL 8 BETA installation with screenshots
  • Process states in Linux
  • LVM commands tutorial : Part 1 : Physical Volume (pvcreate, pvdisplay)
  • Account lock unlock status in Linux
  • How to get directory size in Linux
  • 7 new features in RHEL7!

Filed Under: Linux Tagged With: change hostname permanently, hostname basics, hostname config file in linux, hostname in linux, hostname limitations in linux, how to change hostname

If you like my tutorials and if they helped you in any way, then

  • Consider buying me a cup of coffee via paypal!
  • Subscribe to our newsletter here!
  • Like KernelTalks Facebook page.
  • Follow us on Twitter.
  • Add our RSS feed to your feed reader.

Comments

  1. Ch.S.Chakrapani says

    December 20, 2020 at 8:37 am

    I am using redhat 5.9x version of linux, I want to display the hostname at pre login prompt i.e. “localhost login:” before entering into the system.
    I want configure my linux system for sending mails , please guide for a proper tool:

    Reply

Share Your Comments & Feedback: Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get fresh content from KernelTalks

  • Email
  • Facebook
  • RSS
  • Twitter

Get Linux & Unix stuff right into your mailbox. Subscribe now!

* indicates required

This work is licensed under a CC-BY-NC license · Privacy Policy
© Copyright 2016-2023 KernelTalks · All Rights Reserved.
The content is copyrighted to Shrikant Lavhate & can not be reproduced either online or offline without prior permission.