- How do I change the computer name?
- 14 Answers 14
- How to Change Hostname on Ubuntu 20.04
- How to Check Current Hostname on Ubuntu 20.04
- Temporarily Change Hostname on Ubuntu 20.04
- Change Hostname on Ubuntu 20.04 (No Reboot Required)
- Step 1: Use set-hostname to Change the Hostname
- Step 2: Use hostnamectl to Confirm the Change
- Step 3: Change the Pretty Hostname (Optional)
- Change Hostname on Ubuntu 20.04 – Alternative Method (Reboot Required)
- Step 1: Open /etc/hostname and Change the Hostname
- Step 2: Open /etc/hosts and Change the Hostname
- Step 3: Reboot the System
- Change Hostname on Ubuntu 20.04 Using GUI
- Как изменить Hostname (имя компьютера)
- Просмотр текущего Hostname
- Как изменить Hostname
- Способ 1. Используем команду hostnamectl
- Способ 2. Редактируем файлы /etc/hostname и /etc/hosts
- Проверка результата
- Как временно изменить Hostname
How do I change the computer name?
Try editing /etc/ hosts. gksudo gedit /etc/hosts and replace server-name.belkin by whatever name you desire.
@warren the current consensus is to close by «quality»: meta.stackexchange.com/questions/147643/… Since «quality» is not measurable, I just go by upvotes. 😉 Likely it comes down to which question hit the best newbie Google keywords on the title.
14 Answers 14
You need to edit the computer name in two files:
These will both need administrative access, so run
Replace any instances of the existing computer name with your new one. When complete run
sudo service hostname start
The name will also be changed if you restart your computer.
sudo service hostname restart doesn’t work on default install of ubuntu server 14.04 on AWS. I had to do full server restart
On 14.04, I simply ran sudo hostname , and that did the trick. I didn’t notice any immediate change, but when I opened a new terminal, I saw my hostname had indeed changed.
hostnamectl set-hostname on 13.10+ desktop
This is the best way if you have systemd (13.10 onwards) and if cloud-init is not active (see below):
hostnamectl set-hostname 'new-hostname'
18.04 onwards: cloud-init
18.04 Introduced cloud-init which can control setting of the hostname so hostnamectl changes it won’t stick after a reboot if cloud-init is installed. TODO: how to check if it is installed, is it installed by default on the desktop image or just server?
If you want hostnamectl changes to stay after a reboot, then you’ll need to edit the cloud-init config files, disable cloud-init’s hostname set/update module:
sudo sed -i 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg
or disable cloud-init entirely:
sudo touch /etc/cloud/cloud-init.disabled
I had to add my new-hostname into /etc/hosts else using sudo would cause some error «sudo: unable to resolve host xxxxx»
Working also with Ubuntu 20 LTS as desktop, and only using sudo hostnamectl set-hostname myNewName . After new terminal and after boot (!), the` myNewName`, no problem.
- Edit /etc/hostname , make the name change, save the file.
- You should also make the same changes in /etc/hosts file
- Run sudo service hostname start
As long as you have no application settings depending on the ‘old’ hostname, you should be ok 😉
WARNING: If you do this without changing /etc/hosts accordingly you will be unable to use sudo because your hostname will fail to lookup
You’re right regarding editing the /etc/hosts, i forgot about it (just added it to my answer), though sudo seems to be working fine for me without changing it (i’ve restarted the machine and it still works)
It’s ok, you can do that as hostname is now a service managed from upstart . It’ll do exactly the same thing,restart the service.
Warning: won’t work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot.
It is safe to do, you just need to be sure you edit both the system hostname configuration file (/etc/hostname) and the hostname name resolution file (/etc/hosts). From a terminal execute the following:
sudo -s editor /etc/hostname editor /etc/hosts shutdown -ry now
Warning: won’t work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot.
In addition to editing /etc/hosts and /etc/hostname, various services might have issues with the change as well. Mysql and postfix are installed by default in ubuntu. A broken postfix won’t affect most ubuntu users, since it’s a background email server that isn’t used by much.
sudo editor /etc/postfix/main.cf sudo service postfix restart
The default config for mysql doesn’t use hostname, so it will work fine as-is. If you have customized it, edit the files in /etc/mysql/ and restart the service.
You may also want to edit /etc/motd (message of the day), which is shown on virtual terminals and remote logins. That one won’t harm anything though.
Other services that you may have installed that would need fixing are apache, bind9, etc. In each case, find and edit the hostname in their config and restart the service.
How to Change Hostname on Ubuntu 20.04
A hostname is a user-generated custom name which identifies a computer system in a network. In Ubuntu 20.04, users assign a hostname to the machine during OS setup.
You may decide to change your computer’s hostname for several reasons. For example, no two computers on a network can have the same hostname and, if you find yourself in this situation, you will have to change the hostname to avoid a network conflict.
In this tutorial, you will learn how to change a hostname on Ubuntu 20.04 using the Linux command line or GUI.
- Ubuntu 20.04 installed
- An account with sudo privileges
- Access to the command line for non-GUI methods
How to Check Current Hostname on Ubuntu 20.04
To check the current hostname of your Ubuntu system, use one of two available commands.
- The other command, hostnamectl , displays additional information about your computer system.
The Static hostname line displays your machine’s hostname.
Note: Valid hostnames are between 2 and 64 characters in length. They can contain only letters, numbers, periods, and hyphens, but must begin and end with letters and numbers only.
Temporarily Change Hostname on Ubuntu 20.04
Use the hostname command to make a temporary change to your computer’s hostname.
In the terminal, type the following, replacing new-hostname with the name you choose:
sudo hostname new-hostname
If successful, this step will not provide any output. To confirm the result of the process, check the current system hostname:
Change Hostname on Ubuntu 20.04 (No Reboot Required)
If you wish to permanently change the hostname without rebooting your computer, use the hostnamectl command.
Step 1: Use set-hostname to Change the Hostname
Type the following command:
hostnamectl set-hostname new-hostname
Use your own hostname choice instead of new-hostname .
Step 2: Use hostnamectl to Confirm the Change
Just like the hostname command, if successful, hostnamectl set-hostname does not produce any output. Therefore, use hostnamectl to check the result.
Step 3: Change the Pretty Hostname (Optional)
A “pretty” hostname is the hostname presented to the user, not to another computer on a network. A computer system identifies another computer only by its static hostname.
Note: The pretty hostname does not have the naming limitations of its static counterpart – any UTF-8 value is permitted.
To change a machine’s “pretty” hostname, use the same hostnamectl command with the —pretty option:
hostnamectl set-hostname "new-hostname" --pretty
Replace new-hostname with your own UTF-8 value.
After checking the result with hostnamectl , you will notice an additional line in the output, listing the computer’s “pretty” hostname.
Note: The “pretty” hostname is stored in /etc/machine-info. Updating this file is another way to perform this optional step.
Change Hostname on Ubuntu 20.04 – Alternative Method (Reboot Required)
Another way to permanently change the hostname is by editing two configuration files:
The changes take effect immediately after system reboot.
Step 1: Open /etc/hostname and Change the Hostname
Edit the file with a text editor of your choice. In this example, we will be using the Vim editor:
The /etc/hostname file contains only the current hostname. Replace it with your new choice.
Step 2: Open /etc/hosts and Change the Hostname
Now edit the /etc/hosts file in the same way.
The file /etc/hosts maps hostnames to IP addresses. Look for the hostname you wish to change and simply replace it with your new choice.
Step 3: Reboot the System
Reboot your computer to apply the changes:
Note: If you use the Cloud-Init package ( cloud-init ) to run a cloud instance of Ubuntu, you need to perform another step before rebooting. Go to /etc/cloud/cloud.cfg file and make sure the line preserve_hostname is set to TRUE .
Change Hostname on Ubuntu 20.04 Using GUI
The Ubuntu GUI also provides a way to edit the system hostname:
1. First, navigate to Settings > About.
2. Now, locate the Device Name field.
3 Clicking the Device Name field opens the Rename Device dialogue box.
4. In the Rename Device dialogue box, replace the current hostname with a new one and confirm your choice by clicking the Rename button. This action permanently changes the hostname.
This article presented three methods in which you can change the hostname on your Ubuntu 20.04 machine, using the command line or GUI. All the methods are simple and straightforward, so you can choose whichever one fits your use case best.
Как изменить Hostname (имя компьютера)
Hostname (имя компьютера, имя хоста) задается во время установки системы Linux. Hostname определяет название компьютера и используется преимущественно для идентификации компьютера в сети. Нельзя назначать два одинаковых Hostname для компьютеров в одной сети.
Просмотр текущего Hostname
Чтобы показать текущий Hostname можно использовать команду hostname без параметров:
Также Hostname можно отобразить командой hostnamectl :
Как изменить Hostname
Рассмотрим, как изменить Hostname двумя разными способами.
Способ 1. Используем команду hostnamectl
Во многих systemd-based дистрибутивах (например, в Ubuntu Linux) есть утилита hostnamectl .
Воспользуемся командой hostnamectl , чтобы установить новое значение Hostname равным pingvinus . Используется аргумент set-hostname:
sudo hostnamectl set-hostname pingvinus
Способ 2. Редактируем файлы /etc/hostname и /etc/hosts
Еще один способ изменения Hostname — это ручное редактирование файла /etc/hostname и файла /etc/hosts .
Сначала выполните команду (вместо pingvinus укажите свое значение):
Отредактируем файл /etc/hostname для редактирования в редакторе nano, выполняем команду:
Измените текущее значение в файле на новое. Сохраните изменения и закройте файл. Это можно сделать, нажав Ctrl+C , затем Y и Enter .
Hostname прописывается также в файле /etc/hosts , поэтому нужно изменить его значение в этом файле. Откроем файл:
Найдите строку вида « 127.0.0.1 ваш_hostname » и впишите новое имя хоста. Сохраните изменения и закройте файл.
Проверка результата
Чтобы проверить, изменилось ли значение Hostname, откройте новое окно терминала и введите команду:
Как временно изменить Hostname
Чтобы изменить Hostname в текущей работающей системе Linux, но до первой перезагрузки, можно использовать просто команду hostname , указав ей новое значение имени хоста: