How to change your hostname in Linux/Unix?
Fedora Core 2
Yes there’s an easy way of changing the hostname in FC2. after Login just at command line enter ‘hostname’ to see what is the hostname seted there, if returns localhost then you have to change this in
1. ‘/etc/sysconfig/network’ edit this file and give hostname whatever you like in the line “hostname=” and then save this file and at the command line give the hostname command but now also you will get the same old host name as ‘localhost’ so set the hostname using command hostname (for eg: hostname vikas — here hostname command will set the hostname as vikas, pls note that the name you are giving for the host name must be same as the hostname you have specified in ‘/etc/sysconfig/network’ file.
No, it does not have to be the same. It is just that if it is different, then what is in /etc/sysconfig/network will be the hostname when you reboot. The hostname you set on the command line will be gone. If you are going to reboot then messing with the hostname command is a waste of time.
At boot time the initscripts read the hostname from /etc/sysconfig/network and set it to whatever the HOSTNAME variable is set to. Note the HOSTNAME= line in the above file must be uppercase. ie: HOSTNAME=foo will set the hostname to foo. hostname=foo will not.
2. ‘/etc/hosts’ edit this file and this file would look like ” IPAddress hostname localhost.localdomain localhost” in the place of hostname give the name as you wish(in the example same as hostname given in /etc/sysconfig/network file and hostname setted at command line), and save this file
If you have a working dns on the network and the new name resolves you do not need to mess with /etc/hosts. If you are going to mess with /etc/hosts leave the line with the localhost stuff in it and add another line with the new info. That way localhost will always resolve no matter what. Not having localhost resolve can cause problems sometimes.
3. Now restart the NFS and Network service by using the command ’service nfs restart’ and ’service network restart’
Why? This is not necessary, especially if you are going to reboot. What is the point of restarting a service and then rebooting the machine to restart the service again. It appears to me you are giving advice on a subject you know little about.
4. Restart your computer inorder to take effect the changes you have done. Not necessary. Pick one way or the other. If you are going to restart the machine simply edit /etc/sysconfig/network and /etc/hosts if necessary, then reboot.
If you do not want to reboot, then do the above plus “hostname new_hostname” and “service syslog restart” so that syslog rereads the hostname and labels the log files correctly. The OP wanted a simple way to change the hostname.
Your way while it will not hurt is unnecessarily complex and as you described it above will not work (hostname= != HOSTNAME=).
While the /etc/hosts file is generally the first to come to mind when you think about changing the name of a Unix system, modifying this file is only one step in the process. The other files that need to be changed depend on which particular version of “*nix” you are running.
The following is a list of UNIX flavors and methods of changing hostname permanently on each system.
1) Solaris
On Solaris, there are three files that you need to edit. These include /etc/hosts, /etc/hostname.
- Edit /etc/hosts with vi and update to the new hostname.
- Similarly update /etc/nodename using an editor or “echo newhost > /etc/nodename”
- Lastly, edit /etc/hostname.
Once you’ve updated the above 3 files, best thing is to reboot your system or if can’t reboot right away, you can execute ‘uname -S
2) Redhat Linux (Fedora, Mandriva, PCLos)
On Redhat based Linux systems, the primary setting for the system hostname is included in the /etc/sysconfig/network file.
- Open this file in editor, you should see something like this:
—————————————————-
NETWORKING=yes
HOSTNAME=abc.example.com
—————————————————-
- and of course the /etc/hosts file.
3) Debian/Ubuntu Linux
Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/hostname.sh.
- Edit /etc/hostname and update to your new hostname, you can then run /etc/init.d/hostname.sh to activiate the change.
- Update your /etc/hosts file to refelect new hostname
You can also use sysctl to change the hostname:
- sysctl kernel.hostname (To read the current hostname)
- sysctl kernel.hostname=NEW_HOSTNAME (to change it)
To change in gui in Ubuntu you can click to:
- System > Administration > Networking > General tab > Host name field
4) Slackware Linux
Similar to Debian except the it is uppercase HOSTNAME.
- Edit /etc/HOSTNAME and update to new value
- Update /etc/hosts to new hostname value as well
5) SuSE Linux
To change hostname permanently on SuSE Linux
- Edit the file /etc/HOSTNAME and change the value in it, Save and exit
- Update /etc/hosts to new hostname value
6) Mac OSX
To change hostname on a Mac OSX server…
- Edit /etc/hostconfig and update to your new hostname
- And of course the /etc/hosts file needs to be adjusted to your new host
You can also execute the following command:
- sudo scutil –set HostName abc.example.com
7) OpenBSD
Very similar to Solaris operating system.
- Update to new hostname in /etc/hosts file
- Make appropriate change in /etc/myname
- Lastly, /etc/hostname.
8 ) HP-UX
The script /sbin/set_parms changes system parameters including hostname and IP address.
- /sbin/set_parms
9) Digital Unix
- Edit /etc/hostname.driver
- As well as /etc/hosts file
10) FreeBSD
To change hostname do:
- Edit /etc/rc.conf and update to new hostname value
- Edit /etc/hosts
11) AIX
AIX stores all its information in ODM (Object Database Manager) including hostname value. There are a couple ways of doing it:
- chdev -l inet0 -a hostname=thomas
Or you can use smitty as well.
12) NetBSD
- Edit /etc/myname
- And of course edit /etc/hosts file
13) SCO Unix
I don’t know who in their right mind would run SCO these days. But anyhow the way to change hostname permanently is:
- sys-unconfig
14) IRIX
I can understand why IRIX may still be used. To change hostname:
- Edit /etc/sys_id and update to new host value
- Edit /etc/hosts as well
Then reboot your workstation/server for changes to take affect.
Finally, some general facts about all Linux distros. The following are commands that are unique to all flavors of Linux:
To print current system hostname on the system:
- hostname
- uname -n
- cat /etc/hosts ‘ grep 127.0.0.1
To print the fully qualified domain name of the system:
- hostname -fqd
To temporarily update the system to new hostname:
- hostname new_hostname
- echo “new hostname” > /proc/sys/kernel/hostname
On most Linux flavors, you can also execute the command:
- netconfig
In conclusion, please keep in mind that all UNIX flavors utilizes the /etc/hosts file to associate IP to hostname for your local server. So this file is global and you’ll need to make sure this file is not overlooked. Please add to the comments if I’ve missed other major flavors.

Comments
No comments yet.