CentOS Linux 5/6 Change Hostname Command

I am a new CentOS Linux server user. How do I change hostname on CentOS Linux? How can I change server hostname on CentOS Linux version 5 or 6?

You need to use the following commands to set or view current hostname, domain name and/or FQDN (Fully Qualified Domain Name):
Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements None
Time 5m

ADVERTISEMENTS

  1. dnsdomainname command : Display the system’s DNS domain name.
  2. hostname command : Show or set the system’s host name.

You also need to edit the following files for a hostname change:

  1. /etc/sysconfig/network file.
  2. /etc/hosts file.

In this tutorial, I will show you how to set/change a server hostname in CentOS Linux to “server1.cyberciti.biz”. For CentOS / RHEL version 7.x users, see this page for more info.
CentOS Change Hostname Commands

Display current hostname

Type the following command:
# hostname
Sample outputs:

server1

To display current domain name:
Type the following command:
# dnsdomainname
Sample outputs:

cyberciti.biz

Hostname change procedure on CentOS

Open the terminal or login to the remote server using ssh client. Edit /etc/sysconfig/network file, enter:
# vi /etc/sysconfig/network
Find and modify HOSTNAME value to set to FADN such as server1.cyberciti.biz:

HOSTNAME="server1.cyberciti.biz"

Save and close the file.

Edit hosts file

Edit /etc/hosts file, enter:
# vi /etc/hosts
You need to set or change the host that is set to your IP address on server.

127.0.0.1 localhost
10.240.40.209 server1.cyberciti.biz server1  # Added by nixCraft

Save and close the file.

Set hostname manually without rebooting the box

Type the following command:
# hostname server1.cyberciti.biz

Restart the CentOS networking and other services (if any)

You need to restart the networking service on CentOS Linux, enter:
# service network restart
OR
# /etc/init.d/network restart

Verify new hostnames

Simply type the following commands:
# hostname
# dnsdomainname

See also
This entry is 7 of 19 in the Linux/Unix Set hostname Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Change Hostname (computer name)
  2. Debian Linux: Change Hostname / Computer Name Permanently
  3. Linux change my hostname / computer system name
  4. Linux Change Hostname
  5. RHEL / Centos Linux 7: Change and Set Hostname Command
  6. OpenBSD Change Hostname
  7. CentOS Linux 5/6 Change Hostname Command
  8. Linux setting hostname and domain name of my server
  9. FreeBSD Change Hostname without reboot
  10. SuSe Linux Change Hostname without reboot
  11. How to set the hostname on Fedora Linux
  12. Ubuntu 18.04 LTS change hostname permanently
  13. Ubuntu set hostname permanently (computer name) command
  14. OpenSUSE Linux set hostname permanently (computer name) command
  15. RHEL 8 Linux set hostname permanently (computer name) command
  16. CentOS 8 Linux set hostname permanently (computer name) command
  17. Ubuntu 20.04 LTS set hostname permanently (computer name) command
  18. Set / Change FQDN on Ubuntu 20.04 LTS
  19. Alpine Linux - Setting system hostname
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Joro Mar 21, 2015 @ 11:34

    Hey pal,

    Why are you restarting the network at all?!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.