Friday, June 12, 2015

[CentOS/ Ubuntu]: How to change hostname or FQDN in CentOS/ Redhat/ Ubuntu

How to change hostname or FQDN in CentOS or Redhat

In this tutorial we will learn about,how to change Hostname or FQDN in CentOS and Red Hat.
The method is applicable to all Red Hat based operating system.

Step 1: Edit the /etc/hosts file and give IP Address,FQDN name and Server name
 
         #vi /etc/hosts 
 
         ipaddress_of_system server1.sharadchhetri.com server1 
 
Step 2: Edit /etc/sysconfig/network file ,give HOSTNAME as machine name i.e server1
         # vi /etc/sysconfig/network
         NETWORKING=yes
         HOSTNAME=server1
       

Step 3: Restart the network service.
         /etc/init.d/network restart

         
Now check the hostname and FQDN with command called hostname and hostname -f respectively.
 
#hostname
 server1
 
#hostname -f
 server1.sharadchhetri.com

Note: There is another method through which you can change the hostname but generally it is not so much preferable.


The command is
sysctl kernel.hostname                      (It will show the current hostname)
sysctle kernem.hostname Give_hostname        (It will set the hostname)


Set hostname and FQDN in Ubuntu without reboot


Edit Your /etc/hostname File

Open a terminal window to get started. In Ubuntu’s Unity desktop, click the Ubuntu button to pull up the dash, search for Terminal, and press Enter.
open-terminal-on-ubuntu-linux
You’ll need to edit your /etc/hostname file, which is where Ubuntu and other Debian-based distributions store the hostname. Run the following command in the terminal to open the /etc/hostname file in the graphical “gedit” text editor for editing:
sudo gedit /etc/hostname
(You could also use terminal-based text editors like nano or vi for this — you’d run the sudo nano /etc/hostname or sudo vi /etc/hostname commands to open the file for editing.
The /etc/hostname file is very simple. It’s contains only one thing — your computer’s hostname. To change it, just delete the existing hostname from the file. Replace it with your new desired hostname and then save the text file.
etc-hostname-file-on-ubuntu,-mint,-or-debian
Ubuntu and other Debian-based distribution read the /etc/hostname file while booting up and set your computer’s hostname to the one contained in the file. Your change won’t take effect immediately — you’ll have to reboot or use the hostname command to change it immediately.
set-new-hostname-on-ubuntu-linux

Edit Your /etc/hosts File

The hostname is also stored in the /etc/hosts file, where it’s set to redirect to your local computer — localhost. You’ll need to change the hostname in your hosts file, too.
Open the /etc/hosts file for editing with a command like the following one:
sudo gedit /etc/hosts
(Once again, you could use any other text editor you want — nano, vi, or another graphical text editor of your choice.)
open-hosts-file-for-editing-on-ubuntu
Locate your old hostname in the hosts file. It’ll be on a line that looks something like:
127.0.1.1 your-old-hostname
add-new-hostname-to-hosts-file-on-ubuntu
Once again, replace the old hostname with your new hostname. You can then save the /etc/hosts file.
new-hostname-added-to-etc-hosts-file-on-ubuntu-linux

Change Your Hostname Immediately

The change above will take effect when you reboot, but you can use the hostname command in a terminal to change your hostname immediately. This command only changes the hostname until the next time you reboot, so you do have to change the /etc/hostname file to change it permanently.
Run the hostname command in a terminal window to specify a new hostname. If you chose an invalid hostname, this command will tell you and you can try again. Run the command like so:
sudo hostname your-new-hostname
change-hostname-without-rebooting-on-ubuntu
Your computer’s hostname will be changed immediately, although the change won’t appear in the terminal immediately. The terminal will notice when you re-open it — or if you just open a new one.
change-hostname-immediately-on-ubuntu

That’s it; you should be done. On Linux distributions not based on Debian, there’s a different process. Check your Linux distribution’s documentation for more information

No comments: