How to Change Hostname in Linux Ubuntu Operating System using Command Line

Posted on


This article contains information about how to change hostname in Linux Ubuntu operating system. There are several ways for changing the hostname of a machine powered by Ubuntu Linux operating system. One of the available method is using a specific command. The command line interface will be the place for the command execution. Just type the command in the command line interface. This article will highlight the usage of a command in the command line for changing the hostname of a machine where Ubuntu Linux is the operating system.

Using ‘hostname’ command to change the hostname of a machine

The following is an example of a command execution in the command line using Linux Ubuntu 19.04 :

1. Check the current hostname. Just type ‘hostname’ in the command line as follows:

To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.

user@localhost:~$ hostname

According to the output above, the hostname is ‘localhost’. Just continue on to the next step to change it :

2. Next, for executing the ‘hostname’ command, it need a super user privilege. So, in order to have the privilege to execute the ‘hostname’ for changing the hostname, switch to root account or root user. Do the following command¬† in the command line as follows :

user@localhost:~$ sudo su –
[sudo] password for user:
root@localhost:~# hostname myserver

3. Furthermore, don’t forget to re-execute the ‘hostname’ command. But the ‘hostname’ command execution in this step is different.¬† In this time, there is an additional argument as the new hostname. Put the argument after the ‘hostname’ command so it will act as a parameter that will replace the old hostname. The following is the execution of the ‘hostname’ command with an additional parameter :

root@localhost:~# hostname myserver

4. Finally, in order to implement or to see the change, open a new terminal. In other way, create a new tab of the terminal by using Ctrl+T keyboard shortcut. The hostname exist in the command prompt line in the terminal will change into a new one as follows :


Leave a Reply