How to change hostname on debian

How to change hostname on Debian 10 Linux

last updated July 13, 2019 in Categories

 

How do I change the hostname of a Debian Linux 10 “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 10 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 10The current hostname is set to localhost

Change the hostname on Debian 10

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:

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 [email protected]
hostnamectl set-hostname www42.nixcraft.com -H [email protected]

The hostnamectl tool will use SSH to connect to the remote system. One can see help page by typing:
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.

 

Guess you like

Origin www.cnblogs.com/mouseleo/p/12050730.html