Change hostname on linux system

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.

  1. Edit /etc/hostname , make the name change, save the file.
  2. You should also make the same changes in /etc/hosts file
  3. 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)

Читайте также:  Linux clear cache and buffer

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.

Источник

Как изменить Hostname (имя компьютера)

Как изменить 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 , указав ей новое значение имени хоста:

Читайте также:  Управление скоростью вращения вентиляторов linux

Источник

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 change your hostname in Linux

What’s in a name, you ask? Everything. It’s how other systems, services, and users «see» your system.

military identification tag

Great Linux resources

Your hostname is a vital piece of system information that you need to keep track of as a system administrator. Hostnames are the designations by which we separate systems into easily recognizable assets. This information is especially important to make a note of when working on a remotely managed system. I have experienced multiple instances of companies changing the hostnames or IPs of storage servers and then wondering why their data replication broke. There are many ways to change your hostname in Linux; however, in this article, I’ll focus on changing your name as viewed by the network (specifically in Red Hat Enterprise Linux and Fedora).

Background

A quick bit of background. Before the invention of DNS, your computer’s hostname was managed through the HOSTS file located at /etc/hosts . Anytime that a new computer was connected to your local network, all other computers on the network needed to add the new machine into the /etc/hosts file in order to communicate over the network. As this method did not scale with the transition into the world wide web era, DNS was a clear way forward. With DNS configured, your systems are smart enough to translate unique IPs into hostnames and back again, ensuring that there is little confusion in web communications.

Читайте также:  Linux macos big sur

Modern Linux systems have three different types of hostnames configured. To minimize confusion, I list them here and provide basic information on each as well as a personal best practice:

  • Transient hostname: How the network views your system.
  • Static hostname: Set by the kernel.
  • Pretty hostname: The user-defined hostname.

It is recommended to pick a pretty hostname that is unique and not easily confused with other systems. Allow the transient and static names to be variations on the pretty, and you will be good to go in most circumstances.

Working with hostnames

Now, let’s look at how to view your current hostname. The most basic command used to see this information is hostname -f . This command displays the system’s fully qualified domain name (FQDN). To relate back to the three types of hostnames, this is your transient hostname. A better way, at least in terms of the information provided, is to use the systemd command hostnamectl to view your transient hostname and other system information:

output of hostname -f and hostnamectl

Before moving on from the hostname command, I’ll show you how to use it to change your transient hostname. Using hostname (where x is the new hostname), you can change your network name quickly, but be careful. I once changed the hostname of a customer’s server by accident while trying to view it. That was a small but painful error that I overlooked for several hours. You can see that process below:

changing hostname with hostname x

It is also possible to use the hostnamectl command to change your hostname. This command, in conjunction with the right flags, can be used to alter all three types of hostnames. As stated previously, for the purposes of this article, our focus is on the transient hostname. The command and its output look something like this:

hostname change with hostnamectl

The final method to look at is the sysctl command. This command allows you to change the kernel parameter for your transient name without having to reboot the system. That method looks something like this:

hostname change using sysctl

GNOME tip

Using GNOME, you can go to Settings -> Details to view and change the static and pretty hostnames. See below:

hostname change with GNOME

Wrapping up

I hope that you found this information useful as a quick and easy way to manipulate your machine’s network-visible hostname. Remember to always be careful when changing system hostnames, especially in enterprise environments, and to document changes as they are made.

Want to try out Red Hat Enterprise Linux? Download it now for free.

Источник

Оцените статью
Adblock
detector