How to assign hostname in Linux without a reboot ?

In this article, we will see how to assign hostname to a linux machine temporarily or permanent without a reboot.

We have already seen the tip of assigning the domain name here.

Here we will extend it to see how to assign hostname to the system and make it effective without a system reboot.

Lets see how to change hostname step by step:

Changing Linux system hostname permanently

1. Check your current hostname.

[root@nglinux ~]# hostname 
nglinux
[root@nglinux ~]# 

2. Define new system hostname in /etc/sysconfig/network

### For example:- let me define new hostname for my system as ngelinux.
[root@nglinux ~]# vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=ngelinux
DOMAINNAME=ngelinux.com
~                          

3. Restart the network service for changes to take effect.

[root@nglinux ~]# service network restart
Shutting down interface eth2:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth2:  Determining if ip address 192.168.52.138 is already in use for device eth2...
                                                           [  OK  ]
[root@nglinux ~]# 

4. Logout and Login to see the effective changes in hostname.

### Form:
[root@nglinux ~]#

### To:
[root@ngelinux ~]# 

Now we can see the hostname of our system is updated.

Temporarily changing hostname in current shell

Lets see how to temporarily change the hostname in current bash shell.
1. Change using hostname command

[root@nglinux ~]#hostname ngelinux-new
[root@nglinux ~]#hostname
ngelinux-new

2. Change HOSTNAME Variable value.

[root@nglinux ~]#echo $HOSTNAME 
ngelinux
[root@nglinux ~]#export HOSTNAME=ngelinux-new
[root@nglinux ~]#echo $HOSTNAME 
ngelinux-new
[root@nglinux ~]#

We need to perform above both steps for temporary change to take effect on the current shell.

I hope now you can change your machine hostname easily.

Please do post your comments/suggestions below.

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments