How to Change the Hostname on a Linux Machine

How to Change the Hostname on a Linux Machine

 

This post was last updated on July 21st, 2020 at 01:24 pm

Sometimes you wanted to change the Hostname on a Linux machine like on your Ubuntu PC or a Fedora machine or any other Linux distro like a Citrix XenServer to somewhat relevant to the service running on a server or say you have some slogan of yours that you want to put as a Hostname on your computer. Here I will show you how exactly you can change the Hostname on a Linux powered computer machine.

If you are a newbie to Linux then you might be wondering what is a Hostname and where can you find it on a Linux machine?
When you type the following command on a Linux machine:

uname -a

You will get similar outputs as below:

Linux elinux-machine.explorelinux.com 2.6.18-308.20.1.el5 #1 SMP Tue Nov 6 04:38:29 EST 2012 x86_64 x86_64 x86_64 GNU/Linux

The string “infysim-machine” is the hostname or system name you can say. Sometimes the hostname will be visible on your command prompt itself. Like this:

[email protected]:~$

The “root” is the username and the “infysim-machine” is the Hostname.

Can I change the Hostname for the current session only?

Yes. Instead of changing the Hostname permanently if you want it to be changed for the current session only then yes you can do that easily but you need to be a ROOT user.
Just follow the below command to change the Hostname for the current session only and not permanently:

[email protected]:/# hostname
elinux-machine.explorelinux.com
[email protected]:/# hostname new_hostname

The hostname command (the 1st command) will print the current hostname set in the system.
The second command hostname NEW_HOSTNAME will change the hostname temporarily.

Hostname changes will be lost after logout or machine reboot.

How to change the Hostname permanently?

Usually the Hostname string is stored in a file. The filename may be different for different platform.
For Ubuntu and Fedora the file is located at: /etc/hostname and for Citrix Linux machine the file is at /etc/hosts. The file content may be looking like this:

[email protected]:/# cat /etc/hostname
elinux-machine.explorelinux.com

Or on a Citrix machine it may be looking like this:

[email protected]:/# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1	localhost.localdomain localhost
xxx.xxx.xxx.xxx	elinux-machine.explorelinux.com

Open the file and change the string to a desired one:

[email protected]:/# vim /etc/hostname
or
[email protected]:/# vim /etc/hosts
#Change the current hostname to something you want:
new_hostname.explorelinux.com [Ubuntu and Fedora]
or
xxx.xxx.xxx.xxx	new_hostname.explorelinux.com [Citrix]

In some cases the /etc/hostname will have only the hostname without the domain name string at the end:

[email protected]:/# cat /etc/hostname
infysim-machine

Open the file and change the string to a desired one:

[email protected]:/# vim /etc/hostname

infysim-machine
#Change the above name to something you want:

new_hostname
Previous How to Configure vncserver in Fedora Linux
Next Android Application Development Free Online Course
 

About author

Sibananda Sahu
Sibananda Sahu 191 posts

A Linux Kernel Developer and a Firmware Developer by profession. Have worked with few big companies: BROADCOM Corporation, Cypress Semiconductor, LSI Corporation, TOSHIBA Corporation, Western Digital; on various cutting edge technologies and product lines, such as: RAID storage Driver, SSD Firmware, WLAN Firmware etc. Having more than 9 years of experience in Software Engineering domain. Now, took a pledge to educate all aspirant students to teach about Linux Kernel Development.

View all posts by this author →

You might also like

Linux Topics 0 Comments

How To Install And Configure Fish Shell In Ubuntu 20.04 LTS?

This post was last updated on August 1st, 2020 at 10:33 amIn Unix-like operating systems such as Linux, a shell is a program that interprets the command, executes, takes input(s),

Linux Topics 0 Comments

What Is aptitude And How To Use It?

This post was last updated on August 1st, 2020 at 10:51 pmI already briefly discussed aptitude which is a debian package manager. In this article, we will see it in

Linux Topics 0 Comments

What Is PPA And How To Add Or Remove It In Ubuntu Using Command Line?

This post was last updated on August 1st, 2020 at 09:56 pmIn the context of the Linux operating system, PPA stands for “personal package archive”. By using PPA anyone can

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply