Jump to content
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Sign in to follow this  
glaukos

LinuxStories: How to change hostname on CentOS or RHEL 7

Recommended Posts

Question: What is a proper way to change hostname on CentOS / RHEL 7 (permanently or temporarily)?
 
In CentOS or RHEL, there are three kinds of hostnames defined: (1) static, (2) transient, and (3) pretty. The "static" hostname is also known as kernel hostname, which is initialized from /etc/hostname automatically at boot time. The "transient" hostname is a temporary hostname assigned at run time, for example, by a DHCP or mDNS server. Both static and transient hostnames follow the same character restriction rules as Internet domain names. The "pretty" hostname is allowed to have a free-form (including special/whitespace characters) hostname, presented to end users (e.g., Dan's Computer).
In CentOS/RHEL 7, there is a command line utility called hostnamectl, which allows you to view or modify hostname related configurations.
To view hostname related settings:

$ hostnamectl status
 
15113861225_e0e19783a7.jpg

To view static, transient or pretty hostname only, use "--static", "--transient" or "--pretty" option, respectively.

$ hostnamectl status [--static|--transient|--pretty]
 
To change all three hostnames: static, transient, and pretty, simultaneously:

$ sudo hostnamectl set-hostname
 
15113489172_4e25ac87fa_z.jpg

As shown above, in case of static/transient hostnames, any special or whitespace character will be removed, and any uppercase letter will automatically be converted to lowercase in the supplied argument. Once the static hostname is changed, /etc/hostname will automatically be updated accordingly. However, /etc/hosts will not be updated to reflect the change, so you need to update /etc/hosts manually.
If you want to change a particular hostname only (static, transient or pretty), you can use "--static", "--transient" or "--pretty" option.
For example, to change hostname permanently, you can change the static hostname:

$ sudo hostnamectl --static set-hostname

Note that you do not have to reboot the machine to activate permanent hostname change. The above command will change kernel hostname immediately. Log out, and log back in to see the new static hostname in the command-line prompt.
 

View the full article

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×