How to change hostname on linux

How to change hostname on Linux

The hostname of a Linux system is important because it’s used to identify the device on a network. The hostname is also shown in other prominent places, such as in the terminal prompt. This gives you a constant reminder of which system you’re working with. It’s a real life saver when you’re managing multiple systems through SSH and those command line terminals start to blend together in your mind.

Of course, IP addresses are used when devices need to communicate with each other, but those can change frequently. Hostnames give us a way to know which device we’re interacting with either on the network or physically, without remembering a bunch of numbers that are subject to change. Thus, it’s important that your system bears a hostname which helps you to identify it quickly. For example, “backup-server” is much more informative than “server2.”

In this guide, we’ll show you how to change the hostname on Linux. This can be done via command line or from GUI, and we’ll be showing the methods for both. If you can’t easily identify a system’s purpose from the hostname, it’s time to change it.

In this tutorial you will learn:

Changing hostname on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Change hostname from command line

The following method will work on systems that use systemd (most of the popular Linux distros today). If your system uses init, we cover a separate method in the next section.

$ hostname linux-server $ hostnamectl Static hostname: linux-server Icon name: computer-vm Chassis: vm Machine ID: e94c741849c848f8baca2ec406f88c41 Boot ID: 55f7533a0cb6448ba677ef30b2104481 Virtualization: oracle Operating System: Ubuntu 20.04.1 LTS Kernel: Linux 5.4.0-42-generic Architecture: x86-64
$ sudo hostnamectl set-hostname linuxconfig
127.0.0.1 localhost 127.0.1.1 linux-server
127.0.0.1 localhost 127.0.1.1 linuxconfig

Editing the hosts file

That’s all there is to it. But there are more command line methods to change the hostname than just this one. We cover more below.

Читайте также:  Linux cuda проверить версию

Change hostname from command line (init systems)

An alternative method to the above instructions is to edit the following two files.

    Open /etc/hostname with nano or your preferred text editor and change the name inside of it.

Editing the /etc/hostname file

Change hostname from command line (hostname command)

The last method for changing the hostname from command line is with the hostname command. In most environments, this only temporarily changes the name and it will revert once the system is rebooted. For example, to change the hostname to linuxconfig :

Check the changes by just typing hostname :

Change hostname from GNOME GUI

If you’re running the GNOME desktop environment and don’t wish to use the command line method, follow along with the steps below.

Open the Settings menu

Change the device name (hostname) inside the About tab

Rename the device

Conclusion

In this guide, we saw various methods for changing the hostname of a Linux system via command line and GUI. We also learned about the importance of picking an applicable hostname to assist in the easy identification of a system. Administrators can use these methods to ensure that they have a network of appropriately named devices.

Comments and Discussions

NEWSLETTER

Subscribe to Linux Career Newsletter to receive latest news, jobs, career advice and featured configuration tutorials.

WRITE FOR US

LinuxConfig is looking for a technical writer(s) geared towards GNU/Linux and FLOSS technologies. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system.

When writing your articles you will be expected to be able to keep up with a technological advancement regarding the above mentioned technical area of expertise. You will work independently and be able to produce at minimum 2 technical articles a month.

TAGS

  • VIM tutorial for beginners
  • How to install the NVIDIA drivers on Ubuntu 20.04 Focal Fossa Linux
  • Bash Scripting Tutorial for Beginners
  • How to check CentOS version
  • How to find my IP address on Ubuntu 20.04 Focal Fossa Linux
  • Ubuntu 20.04 Remote Desktop Access from Windows 10
  • Howto mount USB drive in Linux
  • How to install missing ifconfig command on Debian Linux
  • AMD Radeon Ubuntu 20.04 Driver Installation
  • Ubuntu Static IP configuration
  • How to use bash array in a shell script
  • Linux IP forwarding – How to Disable/Enable
  • How to install Tweak Tool on Ubuntu 20.04 LTS Focal Fossa Linux
  • How to enable/disable firewall on Ubuntu 18.04 Bionic Beaver Linux
  • Netplan static IP on Ubuntu configuration
  • How to change from default to alternative Python version on Debian Linux
  • Set Kali root password and enable root login
  • How to Install Adobe Acrobat Reader on Ubuntu 20.04 Focal Fossa Linux
  • How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
  • How to check NVIDIA driver version on your Linux system
  • Nvidia RTX 3080 Ethereum Hashrate and Mining Overclock settings on HiveOS Linux

LATEST TUTORIALS

  • Easy Steps to Update Your Raspberry Pi
  • Connecting Your Raspberry Pi to Wi-Fi: A How-To
  • How to install RealVNC viewer on Linux
  • How to check Raspberry Pi RAM size and usage
  • How to check Raspberry Pi model
  • Understanding UEFI and BIOS in Relation to Linux Nvidia Driver Installation
  • How to orchestrate Borg backups with Borgmatic
  • How to monitor filesystem events on files and directories on Linux
  • Debian USB Firmware Loader Script
  • How to install and self host an Ntfy server on Linux
  • How to backup your git repositories with gickup
  • How to bind an SSH public key to a specific command
  • Creating a Bootable USB for Windows 10 and 11 on Linux
  • How to list all displays on Linux
  • List of QR code generators on Linux
  • How to extract text from image
  • Linux EOF explained
  • How to use xclip on Linux
  • List of window managers on Linux
  • What is zombie process on Linux
Читайте также:  Шаблон имени файла linux

Источник

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.

How to Change Hostname in Ubuntu 20.04

  • 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.

Checking hostname using the hostname command

  • The other command, hostnamectl , displays additional information about your computer system.

Checking hostname using the hostnamectl command

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:

Changing hostname using hostname command

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.

Changing hostname using hostnamectl set-hostname command

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.

Changing the pretty hostname using hostnamectl command with --pretty option

Note: The “pretty” hostname is stored in /etc/machine-info. Updating this file is another way to perform this optional step.

Читайте также:  Tp link tl wn722n driver linux

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.

Editing the /etc/hostname file

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.

Editing the /etc/hosts file

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.

Navigating to the About section in Ubuntu 20.04 settings

2. Now, locate the Device Name field.

Locating the Device Name field in the About section of Ubuntu 20.04 settings

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.

Changing the Device Name in the About section of Ubuntu 20.04 settings

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

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

Источник

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