How do I change hostname under Linux operating system?
You need to use the hostname command. It is used to either set or display the current host or domain name of the system. The hostname is used by many of the networking programs (such as sendmail, Apache servers) to identify the machine.
Display Hostname
Type the following command:
hostnameSample ouputs:
server.nixcraft.net.in
Step # 1: Change Hostname
You need to update two files:
- Linux Distribution specific file. Edit appropriate file as per your distribution as follows.
- /etc/hosts
Redhat / CentOS / Fedora: Change Hostname
Edit /etc/sysconfig/network, enter:
vi /etc/sysconfig/network
Set HOSTNAME=newhost.example.com, enter:
HOSTNAME=server2.nixcraft.comSave and close the file. Type the following command:
hostname server2.nixcraft.com hostname
Now, proceed to step # 2.
Debian / Ubuntu: Change Hostname
Edit, /etc/hostname file, enter:
vi /etc/hostnameDelete old name and add new name:
server2.nixcraft.com
Save and close the file. Type the following command:
hostname server2.nixcraft.com hostname
Now, proceed to step # 2.
Slackware: Change Hostname
Edit /etc/HOSTNAME, enter:
vi /etc/HOSTNAME
Delete old name and add new one:
server2.nixcraft.com
Save and close the file. Type the following command:
hostname server2.nixcraft.com hostname
Now, proceed to step # 2.
Suse / OpenSuse: Change Hostname
Edit /etc/HOSTNAME, enter:
Delete old name and add new one:
server2.nixcraft.com
Save and close the file. Type the following command:
hostname server2.nixcraft.com hostname
Now, proceed to step # 2.
Step #2: Update /etc/hosts
Now, you need to edit /etc/hosts file, enteR:
vi /etc/hosts
Change all old hostname with newer one.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 4 comments… read them below or add one }
echo “servername” > /proc/sys/kernel/hostname
None of this does anything to change the prompt which still reads: [root@localhost /] in CentOS6. After years of Linux mods, why is it still so damn complicated to change a single prompt ?!
Sure it does, but you have to re-connect ssh to get it visible
For my debian-based xubuntu
after editing /etc/hostname with sudo
>> cd /etc/init.d
>> sudo start hostname