Linux setting hostname and domain name of my server

last updated in Categories , , , ,

I would like to change hostname and domain name for my Linux server. Where can I set the local hostname and domain name of my server?

You can use hostname/hostnamectl command to show or set the system’s host name and dnsdomainname command to show the system’s DNS domain name. But the changes are temporary if you use these commands. Local hostname and the domain name of your server defined in text configuration file located in /etc directory.


How to use hostnamectl command

If you are using systemd based distro such as RHEL/CentOS 7.x, Fedora latest, Ubuntu Linux 16.04 LTS, Debian 9.x+, Arch Linux and more, use hostnamectl command. To see current hostname, run:
$ hostnamectl
To change hostname to foo, run:
$ hostnamectl set-hostname 'foo'
$ hostnamectl

A note about GUI tools

If you are using Red Hat or Fedora Linux (older version) use redhat-config-network GUI tool. Type following command and click on DNS tab > Setup hostname and domain name:
# redhat-config-network
On other hand you can edit a text file. Find out and set up the value for HOSTNAME in the file /etc/sysconfig/network:
# vi /etc/sysconfig/network
Setup/replace HOSTNAME
Where, web is hostname and is your DNS domain name.

If you are using Debian Linux

You need to edit /etc/hostname file and put in this file:
# vi /etc/hostname
Set or replace hostname:

Debian, Suse and other Linux distro support GUI tool called network-admin. Type following command and click on DNS tab > Setup hostname and domain name:
$ network-admin

Please note that you must have properly configured DNS server to work FQDN based hostname. See how to configure Linux as a dns client.

See also:

This entry is 8 of 17 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


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

10 comment

  1. I think we have to restart the network service after making the changes.

    service network restart

  2. Do you also want to tell in your artice about restarting services so the new changes can come to effect? At the moment the article is half baked.

  3. Step1: Change the New Values in the Host file
    [root@erp ~]# cat /etc/hosts
    # Do not remove the following line, or various programs
    # that require network functionality will fail. localhost.localdomain localhost erp
    [root@erp ~]#

    Step2: Change the New Values in the network file
    [root@erp ~]# cat /etc/sysconfig/network

    Step3: Change the New Values in the sysctl.conf and resolv.conf files
    [root@erp ~]## cat /etc/sysctl.conf

    [root@erp etc]# cat /etc/resolv.conf
    option attemts : 5
    option timeout :15
    [root@erp etc]#

    Step4:Ping the Host Name or Changed IP Address.
    [root@erp etc]# ping
    PING ( 56(84) bytes of data.
    64 bytes from ( icmp_seq=1 ttl=128 time=1.06 ms

    Step5:Restart the Network or Reboot the system for effecting the changes
    [root@erp ~]# init 6 or

    [root@erp ~]# service network restart

  4. Please help me connect to server with a Linux netbook. There is a box for the name of the server… but I don’t know it.

    Still, have a question? Get help on our forum!