- How do I change the hostname without a restart?
- 14 Answers 14
- Ubuntu 13.04 onwards
- Without Restart
- Without Reboot
- Как изменить Hostname (имя компьютера)
- Просмотр текущего Hostname
- Как изменить Hostname
- Способ 1. Используем команду hostnamectl
- Способ 2. Редактируем файлы /etc/hostname и /etc/hosts
- Проверка результата
- Как временно изменить Hostname
- How to Set or Change System Hostname in Linux
- Set System Hostname Permanently in Linux
- Conclusion
- How to configure a hostname on a Linux system
- Configure a static hostname
- Configure a persistent hostname
- Training & certification
- Wrap up
How do I change the hostname without a restart?
I want to change the OS hostname but I do not want to restart. I have edited /etc/hostname but it requires a restart to get implemented. How to avoid this?
Warning: won’t work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot.
14 Answers 14
It’s easy. Just click the Gear icon (located at upper right corner of the screen), open «About this computer» screen (located at Gear icon ) and edit «Device name».
Or, in a terminal, use the following command:
sudo hostname your-new-name
This will set the hostname to your-new-name until you restart. See man hostname and How do I change the computer name? for further information. Do not use _ in your name.
After a restart your changes in /etc/hostname will be used, so (as you said in the question), you should still use
(or some other editor) so that file contains the hostname.
To test that the file is set up correctly, run:
sudo service hostname start
You should also edit /etc/hosts and change the line which reads:
so that it now contains your new hostname. (This is required otherwise many commands will cease functioning.)
I know. that’s why you should use the ‘hostname’ command. I just wanted to clarify, that the ‘hostname’ command does only change the hostname until you restart/crash/etc. Afterwards it will read the name from the file again.
Depends. If you entered the command you do not need to restart. The hostname is changed already. But only UNTIL your next restart.
In short, although hostname will cause the new name to take immediate effect, it is not «permanent» unless you also change /etc/hostname and /etc/hosts , since those are the files that will be read after a restart or a crash, which of course, you may not be expecting ahead of time.
Ubuntu 13.04 onwards
The hostnamectl command is part of the default installation on both Desktop and Server editions.
It combines setting the hostname via the hostname command and editing /etc/hostname . As well as setting the static hostname, it can set the «pretty» hostname, which is not used in Ubuntu. Unfortunately, editing /etc/hosts still has to be done separately.
hostnamectl set-hostname new-hostname
This command is part of the systemd-services package (which, as of Ubuntu 14.04, also includes the timedatectl and localectl commands). As Ubuntu migrates to systemd , this tool is the future.
Note this isn’t available everywhere. Just looked on my Ubuntu 14.04 LTS ARM-7 installation, and hostnamectl doesn’t exist. Guess some flavours of Ubuntu aren’t using systemd yet.
@Stéphane the commands are part of the systemd suite, but they are available independently. In this case, 13.04-14.10 don’t use systemd as init, by the command is available.
@H2ONaCl the shell variable is set on startup, just open another shell and the hostname variable should be fine. As for sudo, the error about hostname resolution has never actually prevented me from doing anything.
@H2ONaCl ah, but the HOSTNAME variablle isn’t standard — IIRC dash and ksh don’t have it, zsh uses HOST, so as far as shell variables go, it’s the bottom of the heap.
Without Restart
Changing the hostname or computer name in ubuntu without restart
Edit /etc/hostname and change to the new value,
Edit /etc/hosts and change the old 127.0.1.1 line to your new hostname
127.0.0.1 localhost 127.0.1.1 ubuntu.local ubuntu # change to your new hostname/fqdn
Note : i have read it on a forum > Edit /etc/hosts and change the old 127.0.1.1 line to your new hostname (if you don’t do this, you wont be able to use sudo anymore. If you have already done it, press ESC on the grub menu, choose recovery, and edit your host file to the correct settings)
Now after a reboot, your hostname will be the new one you chose
Without Reboot
To change without a reboot, you can just use hostname.sh after you edit /etc/hostname . You must keep both your host names in /etc/hosts (127.0.0.1 newhost oldhost) until you execute the command below:
sudo service hostname start
Note : Above command to make the change active. The hostname saved in this file ( /etc/hostname ) will be preserved on system reboot (and will be set using the same service).
Как изменить 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 , указав ей новое значение имени хоста:
How to Set or Change System Hostname in Linux
Device or system hostnames are used to easily recognize a machine within a network in a human readable format. It is not much of a surprise, but on Linux system, the hostname can be easily changed by using simple command as “hostname“.
Running hostname on its own, without any parameters, will return the current hostname of your Linux system like this:
$ hostname TecMint
If you want to change or set hostname of your Linux system, simply run:
Of course, you will need to replace “NEW_HOSTNAME” with the actual hostname that you wish to set. This will change the hostname of your system immediately, but there is one problem – the original hostname will be restored upon next reboot.
There is another way to change the hostname of your system – permanently. You might have already figured it out that this will require change in some configuration files and you will be correct.
Set System Hostname Permanently in Linux
Newer version of different Linux distributions such as latest Ubuntu, Debian, CentOS, Fedora, RedHat, etc. comes with systemd, a system and service manager that provides a hostnamectl command to manage hostnames in Linux.
To set system hostname on SystemD based distributions, we will use hostnamectl command as shown:
$ sudo hostnamectl set-hostname NEW_HOSTNAME
For Older Linux distributions, which uses SysVinit in short init, can have their hostnames changed by simply editing the hostname file located in:
You then have to add another record for the hostname in:
On RHEL/CentOS based systems that use init, the hostname is changed by modifying:
Here is a sample of that file:
/etc/sysconfig/network NETWORKING=yes HOSTNAME="tecmint.com" GATEWAY="192.168.0.1" GATEWAYDEV="eth0" FORWARD_IPV4="yes"
To keep a permanent hostname change the value next to «HOSTNAME» to the one of your hostname.
Conclusion
This simple article meant to show you a simple Linux trick and I hope that you learned something new.
How to configure a hostname on a Linux system
Make it easier to access your Linux computer by giving it a human-friendly name that’s simpler to use than an IP address.
Each person has a unique identity, such as their name and birth date. Computers also have individual identities, specifically, their hostnames and internet protocol (IP) addresses. Each machine has a valid IP address, but referring to a system by its IP address is not practical.
Instead, you can configure a computer’s hostname, which is the machine’s human-friendly name. You can map the hostname to the IP address so that it’s easy to connect to a machine using its name.
Configure a static hostname
Display the system’s hostname using:
You can also use the hostname command to modify the system’s name temporarily. Here’s an example:
This change is only temporary. After a reboot, all changes will revert.
Configure a persistent hostname
To persistently change the hostname, use the hostnamectl command, or directly modify the default configuration file /etc/hostname .
Here’s an example of modifying the hostname permanently using the hostnamectl command. This shows the change:
$ hostnamectl set-hostname server1.example.com
Training & certification
After executing this command, don’t forget to verify the change using the hostname command.
You can confirm this entry by displaying the /etc/hostname file contents.
Wrap up
These examples show you how to configure the hostname for your machine. Note that during the configuration steps, your system will not automatically resolve the hostname with the IP address. This article covers only how to configure the hostname for a machine.