Debian Linux: Change Hostname / Computer Name Permanently

How do I change the hostname of a Debian Linux system permanently using the command line tools?

You can use the 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.

ADVERTISEMENTS

How To change your hostname on Debian server

Steps to change your hostname on Debian Linux:

  1. Login to your server: ssh user@server-name
  2. Become a root user: sudo -s or su -
  3. Edit the file /etc/hostname: vi /etc/hostname
  4. Edit the file /etc/hosts: vi /etc/hosts
  5. Run command: /etc/init.d/hostname.sh start

Let us see steps in details to change a system’s hostname i.e. rename a computer/server name when using a Debian Linux based system.

Find your current computer name

Type the following command:
$ hostname
Sample outputs:

server1

Debian change hostname

Let us change your computer name on Debian. For example, change hostname to server2, enter:
$ hostname server2
$ hostname

Sample outputs:

server2
How To Change Your Hostname on Debian

How Do I Change Hostname Permanently?

Edit the file /etc/hostname, enter:
# vi /etc/hostname
Delete the existing name and replace it with a new name:

server2

Save and close the file. You may also need to edit the /etc/hosts file, enter:
# vi /etc/hosts
Find all references to server1 and replace with server2:

127.0.0.1	localhost
127.0.1.1	server2

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Save and close the file. Please note that the host name is usually set once at system startup in /etc/init.d/hostname.sh (normally by reading the contents of a file which contains the host name, e.g. /etc/hostname). Just type the following command to apply new changes without rebooting the server:
# /etc/init.d/hostname.sh start
OR
# invoke-rc.d hostname.sh start

A note about setting machine name when using systemd

With systemd it is possible to use a single command as follows:
# hostnamectl set-hostname machine-name-here
# hostnamectl set-hostname server2

This entry is 2 of 19 in the Linux/Unix Set hostname Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Change Hostname (computer name)
  2. Debian Linux: Change Hostname / Computer Name Permanently
  3. Linux change my hostname / computer system name
  4. Linux Change Hostname
  5. RHEL / Centos Linux 7: Change and Set Hostname Command
  6. OpenBSD Change Hostname
  7. CentOS Linux 5/6 Change Hostname Command
  8. Linux setting hostname and domain name of my server
  9. FreeBSD Change Hostname without reboot
  10. SuSe Linux Change Hostname without reboot
  11. How to set the hostname on Fedora Linux
  12. Ubuntu 18.04 LTS change hostname permanently
  13. Ubuntu set hostname permanently (computer name) command
  14. OpenSUSE Linux set hostname permanently (computer name) command
  15. RHEL 8 Linux set hostname permanently (computer name) command
  16. CentOS 8 Linux set hostname permanently (computer name) command
  17. Ubuntu 20.04 LTS set hostname permanently (computer name) command
  18. Set / Change FQDN on Ubuntu 20.04 LTS
  19. Alpine Linux - Setting system hostname
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • arun Jan 6, 2012 @ 0:43

    on RedHat you need to update
    /etc/sysconfig/network

  • abouthack Jan 17, 2012 @ 12:49

    got it at last

  • rsm Jun 26, 2013 @ 11:21

    Very usefull. Thanks!

  • eduard Jul 15, 2016 @ 14:14

    I kept having problems after the otherwise excellent instructions: my hostname did not change (It insisted on naming my machine “(none)”)
    Turned out the name was not acceptable. I wanted to use RB_1 (being the first rasperry in my house), and apparently I can’t use an underscore. Named it just RB and then followed your instructions.

  • Dogukan May 31, 2017 @ 19:19

    Works on debian 9; Thank you very much;

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.