原文链接:https://www.cyberciti.biz/faq/how-to-change-hostname-on-debian-10-linux/ 摘要:This step-by-step guide explains how to change the hostname on your Debian Linux 12/11/10/9 system using the hostnamectl and /etc/hosts file.

H ow do I change the hostname of a Debian Linux 12/11/10 (“bullseye” or “Buster”) operating system permanently using the command line tools?

You can use the hostnamectl command or hostname command to display or set the Debian Linux system’s host name. In this tutorial, you will learn how to change your hostname on Debian Linux.

Change hostname on Debian 12/11/10/9/8 Linux

  1. Login to your server: ssh user @ server-name
  2. Become a root user using either sudo -s or su -
  3. To set the hostname to server1, run: hostnamectl set-hostname server1
  4. Edit the file /etc/hosts and update entries: vi /etc/hosts
  5. Verify it by running the hostnamectl command again.

Let us see all commands in details.

Display the current hostname for Debian Linux

Simply run the following command:
$ hostnamectl

View the current hostname for Debian Linux 10

The current hostname is set to localhost

Change the hostname on Debian 12/11/10/9

Now you know how to view the current hostname. It is time to change it as per your needs. A hostname is nothing but a name that identifies your Debian box on a network. Typically for the server, you set it as FQDN (fully qualified domain name) such as server1.cyberciti.biz. The syntax is as follows:
$ hostnamectl set-hostname {name-here}
For example, to change the system hostname to deb10.cyberciti.biz in Debian 10, you can use the following command:
$ sudo hostnamectl set-hostname deb10.cyberciti.biz
Next, edit the /etc/hosts file, run:
$ vi /etc/hosts
Find all references to oldname and replace with newname except for the following entries:

127.0.0.1    localhost

# The following lines are desirable for IPv6 capable hosts

::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

For example, I added the correct entry to match my setup “ 192.168.2.100 deb10.cyberciti.biz “:

127.0.0.1    localhost
192.168.2.100   deb10.cyberciti.biz

# The following lines are desirable for IPv6 capable hosts

::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Save and close the file.

Verify the change

How do you know that hostname was successfully changed? You use the same command without any arguments. In other words, type the following command:
$ hostnamectl
Change hostname on Debian 10 Linux Buster

Set a particular pretty host name

The syntax is:
$ hostnamectl set-hostname "Vivek's Thinkpad" --pretty

Changing Debian host names remotely using ssh

From your Linux desktop, run:
$ hostnamectl set-hostname -H [username]@hostname $ hostnamectl set-hostname {new_name_here} -H root@192.168.2.10 $ hostnamectl set-hostname www42.nixcraft.com -H root@192.168.2.10
The hostnamectl tool will use SSH to connect to the remote system. One can see help page by passing the --help option to the hostnamectl as follows:
$ hostnamectl --help
Sample help page:

hostnamectl [OPTIONS...] COMMAND ...
 
Query or change system hostname.
 
  -h --help              Show this help
     --version           Show package version
     --no-ask-password   Do not prompt for password
  -H --host=[USER@]HOST  Operate on remote host
  -M --machine=CONTAINER Operate on local container
     --transient         Only set transient hostname
     --static            Only set static hostname
     --pretty            Only set pretty hostname
 
Commands:
  status                 Show current hostname settings
  set-hostname NAME      Set system hostname
  set-icon-name NAME     Set icon name for host
  set-chassis NAME       Set chassis type for host
  set-deployment NAME    Set deployment environment for host
  set-location NAME      Set location for host
 
See the hostnamectl(1) man page for details.

Conclusion

On Debian Linux hostnamectl command used to query and change the system hostname and related settings. For more information see the man page here or read it locally using the man command:
$ man hostnamectl

🥺

Was this helpful?

Please add a comment to show your appreciation or feedback.

CategoryList of Unix and Linux commands
AnsibleCheck version • Fedora • FreeBSD • Linux • Ubuntu 18.04 • Ubuntu • macOS
Archivingz commands
Backup ManagementDebian/Ubuntu • FreeBSD • RHEL
Database ServerBackup MySQL server • MariaDB Galera cluster • MariaDB TLS/SSL • MariaDB replication • MySQL Server • MySQL remote access
Download managerswget
Driver Management• lsmod
Documentationhelp • mandb • man • pinfo
Disk Managementdf • duf • ncdu • pydf
File Managementcat • cp • less • mkdir • more • tree
FirewallAlpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 • Ubuntu 24.04
KVM VirtualizationCentOS/RHEL 7 • CentOS/RHEL 8 • Debian 9/10/11 • Ubuntu 20.04
Linux Desktop appsChrome • Chromium • GIMP • Skype • Spotify • VLC 3
LXDBackups • CentOS/RHEL • Debian 11 • Fedora • Mount dir • Ubuntu 20.04 • Ubuntu 22.04
Modern utilitiesbat • exa
Network ManagementMonitoring tools • Network services • RHEL static IP • Restart network interface • nmcli
Network UtilitiesNetHogs • dig • host • ip • nmap • ping
OpenVPNCentOS 7 • CentOS 8 • Debian 10 • Debian 11 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Power Managementupower
Package Managerapk • apt-get • apt • yum
Processes Managementbg • chroot • cron • disown • fg • glances • gtop • iotop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searchingag • egrep • grep • whereis • which
Shell builtinscompgen • echo • printf
System Managementreboot • shutdown
Terminal/sshsshpass • tty
Text processingcut • rev
Text Editor6 Text editors • Save and exit vim
User Environmentexit • who
User Informationgroups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • w
User Management/etc/group • /etc/passwd • /etc/shadow • chsh
Web Server• • • Nginx Security •
WireGuard VPNAlpine • Amazon Linux • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 • qrencode