I am using Ubuntu Linux. How do I change my hostname or the machine name using the command line option?
You need to use hostname command or hostnamectrl command on modern Linux distro. It is used to either set or display the current host or domain name of the system. This name is used by many of the networking programs to identify the Linux based machine. The following instructions should work on both Debian and Ubuntu Linux systems or any Linux distro if you are using hostnamectrl.
How to display current hostname
Just type hostname command to print the name of the system on screen:
$ hostname
Sample outputs:
viveks-desktop
Change the Linux hostname
Set hostname to desktop.nixcraft.com:
# hostname desktop.nixcraft.com
# hostname
Sample outputs:
desktop.nixcraft.com
Change hostname permanently on a Debian/Ubuntu Linux
You need to edit a file called /etc/hostname:
# vi /etc/hostname
Set new hostname:
desktop.nixcraft.com
Save and close the file. You need to reboot the system or run any one of the following command:
# /etc/init.d/hostname.sh start
OR
# invoke-rc.d hostname.sh start
Linux change hostname using hostnamectl command (systemd only)
Most modern Linux distribution comes with systemd. If you are using systemd based distro try hostnamectl command.
Query hostname with hostnamectl
Let us print out current hostname:
$ hostnamectl
Change hostname with hostnamectl
The syntax is as follows to set hostname to ‘viveks-laptop’:
$ hostnamectl set-hostname 'viveks-laptop'
Sample outputs:

Set the deployment environment description
ENVIRONMENT must be a single word without any control characters. One of the following is suggested: “development”, “integration”, “staging”, “production”. The syntax is:
$ hostnamectl set-deployment ENVIRONMENT
$ hostnamectl set-deployment production
How to set the location string for the system, if it is known
The syntax is:
$ hostnamectl set-location LOCATION
$ hostnamectl set-location "NYC Home"
$ hostnamectl set-location "DC 2, right rack, 2nd shelf"
A note about an RHEL (Red hat) / CentOS / Fedora Linux users
If you are using CentOS or Fedora or Redhat (RHEL) Linux, see this FAQ.
- Ubuntu Linux Change Hostname (computer name)
- Debian Linux: Change Hostname / Computer Name Permanently
- Linux change my hostname / computer system name
- Linux Change Hostname
- RHEL / Centos Linux 7: Change and Set Hostname Command
- OpenBSD Change Hostname
- CentOS Linux 5/6 Change Hostname Command
- Linux setting hostname and domain name of my server
- FreeBSD Change Hostname without reboot
- SuSe Linux Change Hostname without reboot
- How to set the hostname on Fedora Linux
- Ubuntu 18.04 LTS change hostname permanently
- Ubuntu set hostname permanently (computer name) command
- OpenSUSE Linux set hostname permanently (computer name) command
- RHEL 8 Linux set hostname permanently (computer name) command