Change hostname on RHEL 6/CentOS 6

20151224013419As part of my current job I have to deploy VMs for new and existing customers all the time. Most of the work is handled by automation, using a combination of PowerCLI and SaltStack. Sometimes, when VMs get stuck or fail to connect online for whatever reason I have to manually configure them. Changing the hostname is one of those manual steps. As I don’t do it manually often enough I sometimes forget some of the steps. So, as reminder to myself, here are the steps to change the hostname on RHEL 6 or CentOS 6:

The Steps

1. The first step is to run the hostname command followed by the new server name and hit Enter:

[root@web01 ~]# hostname web02

2. As you can see from the above the current hostname was web01, I changed it to web02. Running the hostname command on it’s own will show you the current and new hostname of web02:

[root@web01 ~]# hostname
web02

Changing the hostname using the method above does not permanently change the hostname, in other words if you reboot the server the changes will not survive, the hostname will revert back to its previous name. To make the hostname change permanent continue to step 3 below.

3. The next step is to update the /etc/sysconfig/network file by opening it with vi or any other editor of your choice:

[root@web01 ~]# vi /etc/sysconfig/network

4. Within the file you will see something like the below:

NETWORKING=yes
HOSTNAME=web01

Simply change the hostname to the new name, in my case it is web02:

NETWORKING=yes
HOSTNAME=web02

Exit and save the changes to the file.

5. We have one final file to update, which is /etc/hosts. Edit it vi:

[root@web01 ~]# vi /etc/hosts

You should see a line with the server’s static IP followed by the hostname, per the below:

127.0.0.1   localhost
172.16.0.50     web01

Once again, change it to reflect the new desired hostname, in my case web02:

127.0.0.1   localhost
172.16.0.50     web02

Exit and save the changes to the file.

6. To commit the changes made in the network and hosts files above in steps 4 & 5 respectively you can either reboot the server or restart the network services. If it’s a production system and you want to avoid any downtime restart the network services by running:

[root@web01 ~]# service network restart

After restarting the network services the operating system will use the new hostname.

Change the Product Key on Windows Server 2008 via command line

To change the Product Key on Windows Server 2008 and activate it via command line run the following commands:

slmgr.vbs -ckms

The command above removes all KMS keys on the server and will generate the following dialogue box:

20150519151326

Click OK to proceed and then run command below:

slmgr.vbs -upk

This will remove all installed keys on the server and will result in the following dialogue box:

20150519151452

Click OK to proceed and then run command below:

slmgr.vbs -ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX

This command installs your new key on the server (replace XXXXX etc with your new Product Key) and will produce the following confirmation:

20150519151552

Click OK to proceed and then run command below:

slmgr.vbs -ato

This command activates the new key and you will see the following activation confirmation:

20150519151724

And that’s it! The Product Key has been changed and activated successfully. If you have to do this many times it makes sense to script this, so the batch file below achieves all the steps above in one go:

::Remove all KMS keys
slmgr.vbs -ckms

::Remove all installed keys
slmgr.vbs -upk

::Install new product key
slmgr.vbs -ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX

::Activate new product key
slmgr.vbs -ato

I hope this helps you save a bit of time and energy!