Home Debian How To Set Or Change Hostname In Debian 12 and 11

How To Set Or Change Hostname In Debian 12 and 11

By sk
Published: Updated: 24.4K views 4 mins read

After successfully upgrading to Debian 11 or Debian 12, you update the old hostname to reflect the new Debian version. In this brief guide, I will explain how to set or change hostname in Debian 12 Bookworm and Debian 11 bullseye systems from commandline. For the purpose of this guide, I will be using Debian 11 bullseye, but the setting up hostname is same for Debian 12 as well.

View hostname in Debian

A hostname is a label assigned to a Computer in order to identify it from on LAN or WAN. A typical hostname consists of alphanumeric and special characters. We can use at most 253 characters in hostname and it shouldn't start with a hyphen or a dot.

Finding hostname in Debian Linux is easy!

To display the hostname of a Debian system, simply enter hostname command in the Terminal:

$ hostname
debian10

To show the fully qualified domain name (FQDN), use -f flag.

$ hostname -f

You can also use --fqdn flag to view the fully qualified domain name:

$ hostname --fqdn

All of the aforementioned commands displays the hostname only. If you want to display more details, such as hostname, chassis type, machine id, operating system details, kernel and architecture, use hostnamectl command:

$ hostnamectl

Sample output:

   Static hostname: debian10
         Icon name: computer-vm
           Chassis: vm
        Machine ID: aa47d2d8352c404898f47e00562730d3
           Boot ID: 9a12f2136ba74df5bd378bc837869e9b
    Virtualization: kvm
  Operating System: Debian GNU/Linux 11 (bullseye)
            Kernel: Linux 5.10.0-8-amd64
      Architecture: x86-64
Show current hostname in Debian
Show current hostname in Debian

As you see in the above outputs, my Debian system's hostname is debian10, but the operating system version is Debian 11. I already upgraded it to Debian 11, but haven't changed the hostname yet. Let us change its hostname now.

Set or change hostname in Debian 12 / 11

We can set or change the hostname in couple ways. Here, I have listed 6 different methods to set hostname in Debian 12 or 11.

Method 1 - Using hostname command

To quickly change or set hostname in a Debian 11 system, simply run:

$ sudo hostname debian11

This will set the hostname as debian11. Replace debian11 with a hostname of your choice.

To view the changes in the hostname, use one of the above commands:

$ hostname
debian11
Set hostname in Debian
Set hostname in Debian

Please note that this command will only set the hostname temporarily. The changes will be gone after system reboot.

If you want to permanently set or change hostname, use any one of the following methods.

Method 2 - Using hostnamectl command

This is the standard and most commonly used method to permanently set hostname in Debian and other Linux distributions.

To set permanently set or change hostname in Debian bullseye using hostnaectl command, run:

$ sudo hostnamectl set-hostname debian11

Log out and log in again to view the changes in hostname.

$ hostnamectl 
   Static hostname: debian11
         Icon name: computer-vm
           Chassis: vm
        Machine ID: aa47d2d8352c404898f47e00562730d3
           Boot ID: 9a12f2136ba74df5bd378bc837869e9b
    Virtualization: kvm
  Operating System: Debian GNU/Linux 11 (bullseye)
            Kernel: Linux 5.10.0-8-amd64
      Architecture: x86-64

If you want to view only the hostname, use --static flag.

$ hostnamectl --static
debian11
Display hostname in Debian
Display hostname in Debian

Heads Up: You will keep getting a warning message like below, whenever you use sudo command after changing the hostname.

sudo: unable to resolve host debian11: Name or service not known

To get rid of this warning, edit the /etc/hosts file:

Edit hosts file
Edit hosts file

Add/update the hostname in it.

Update hostname in hosts file
Update hostname in hosts file

Save and close the file. Log out and log back in to take effect the changes.

Method 3 - Using nmcli command

To set hostname using nmcli command in Debian, run:

$ sudo nmcli general hostname debian11

Replace debian11 in the above command with your own hostname. Log out and log back in to apply the changes in hostname.

Method 4 - Using sysctl command

To change or set hostname in Debian using sysctl command, run:

$ sudo sysctl kernel.hostname=debian11

Log out and log in again to apply the changes.

Method 5 - Add or update hostname in /etc/hostname file

Edit /etc/hostname file in your preferred editor:

$ sudo nano /etc/hostname

and add/update hostname in it:

Add hostname in /etc/hostname file
Add hostname in /etc/hostname file

Save the file and close it. Log out and log in again to update the hostname.

Method 6 - Update hostname in /proc/sys/kernel/hostname file

Open /proc/sys/kernel/hostname file in a text editor and add/update the hostname in it.

Alternatively, you can append the new hostname in /proc/sys/kernel/hostname file using the following one-liner command:

$ sudo sh -c "echo 'debian11' > /proc/sys/kernel/hostname"

In this guide, we have learned 6 possible ways to set, change and update hostname in Debian 11 bullseye from command line. Hope this helps.

You May Also Like

1 comment

Pim Dennendal August 24, 2021 - 10:55 pm

Hostname is used everywhere in Linux. EG: Bind name server is just one. SUDO is another one according to the text.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More