Setting the linux hostname

Ubuntu Linux Change Hostname (computer name)

I am a new Ubuntu Linux laptop user. I setup my computer name to ‘tom’ during installation but now I would like to change the computer name to ‘jerry’. Can you tell me how do I remove tom and set it to jerry on Ubuntu Linux? How do I change the Ubuntu computer name from ‘ubuntu’ to ‘AvlinStar’? Can you tell me more about Ubuntu Linux change hostname command?

You can use the hostname command or hostnamectl command to see or set the system’s host name. The host name or computer name is usually at system startup in /etc/hostname file. Open the terminal application and type the following commands to set or change hostname or computer name on Ubuntu Linux.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Ubuntu Linux
Est. reading time 2 minutes

Ubuntu change hostname command

The procedure to change the computer name on Ubuntu Linux:

  1. Type the following command to edit /etc/hostname using nano or vi text editor:
    sudo nano /etc/hostname
    Delete the old name and setup new name.
  2. Next Edit the /etc/hosts file:
    sudo nano /etc/hosts
    Replace any occurrence of the existing computer name with your new one.
  3. Reboot the system to changes take effect:
    sudo reboot

Gif 01: Ubuntu change the computer name demo

Display the current Ubuntu hostname

Simply type the following command:
$ hostname
Sample outputs:

Fig.01: Ubuntu Linux Show the hostname/computer name command

How to change the Ubuntu server hostname without a system restart?

Type the following commands:
$ sudo hostname new-server-name-here
Next edit the /etc/hostname file and update hostname:
$ sudo nano /etc/hostname
Finally, edit the /etc/hosts file and update the lines that reads your old-host-name:
$ sudo nano /etc/hosts
From:
127.0.1.1 old-host-name
To:
127.0.1.1 new-server-name-here
Save and close the file.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Ubuntu Linux Change Hostname Using hostnamectl

Systemd based Linux distro such as Ubuntu Linux 16.04 LTS and above can simply use the hostnamectl command to change hostname. To see current setting just type the following command:
$ hostnamectl
Sample outputs:

To change hostname from nixcraft to viveks-laptop, enter:
$ hostnamectl set-hostname viveks-laptop
$ hostnamectl

Conclusion

In this tutorial, you learned how to change hostname on Ubuntu Linux. For more information see this page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Last file should be /etc/hosts instead of /etc/hostname

On any system using SystemD to check current:
hostnamectl
or:
hostname
To change:
hostnamectl set-hostname viveks-desktop

Very useful. I didn’t konw about changing /etc/hosts.

How about hostnamectl command?

Dear Vivek,
The above is good and solves to Change/Modify hostname/hosts..
But the same is not holds for System Name
Ex: vivek@ubuntu is changed as vivek@vivek_desktop or anything else..
But It could not change “vivek” to newname

Hi it worked and Thank you very much for your help
I have a Doubt about AWS EC2
i created a new instance using existing instance and i want to change the hostname of new instance automatically but it showing its private ip address only for the new one
please help me

my code is
aws ec2 create-tags –resources `aws ec2 run-instances –image-id –count 1 –instance-type t2.micro –key-name –user-data file://my-userdata.sh –subnet-id –security-group-ids |jq -r “.Instances[0].InstanceId”` –tags “Key=Name,Value=servername”

It’ll hold the value to the system, i mean persist the value even after restart if you add it to /etc/sysconfig/network
HOSTNAME=

Источник

Ubuntu 20.04 LTS change hostname permanently

I am a brand-new Ubuntu Linux 20.04 LTS VM user. By default, my server name to ‘localhost’ during cloud installation, but now I would like to change the computer name to ‘ubuntu-2004-nixcraft’. Can you tell me how do I remove the localhost and set it to ubuntu-2004-nixcraft on Ubuntu 20.04 Linux? How do I change the Ubuntu computer name from ‘localhost’ to ‘ubuntu-2004-nixcraft’? Can you tell me more about Ubuntu Linux change hostname command?

One can use the hostnamectl command to view or set the Ubuntu 20.04 LTS server’s host name. The host name or computer name is usually at system startup in /etc/hostname file. Open the Terminal application and type the following commands to set or change the hostname or computer name on Ubuntu.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Ubuntu 20.04
Est. reading time 5 minutes

How to display the current Ubuntu hostname

Run the following command:
hostnamectl
Of course, you can use the echo command/printf command to print hostname too:

Finally, try the cat command on /etc/hostname cat /etc/hostname

Ubuntu 20.04 LTS change hostname permanently

The procedure to change the computer name on Ubuntu 20.04 LTS:

  1. Type the hostnamectl command :
    sudo hostnamectl set-hostname newNameHere
    Delete the old name and setup new name.
  2. Next Edit the /etc/hosts file:
    sudo nano /etc/hosts
    Replace any occurrence of the existing computer name with your new hostname.
  3. Reboot the system to changes take effect:
    sudo reboot

Did you know? All Linux hostname must be from 1 to 63 characters long, and the entire hostname, including the dots, can be at most 253 characters long. Valid names for hostnames are letters from a to z, the digits from 0 to 9, and the hyphen (-). A hostname may not start with a hyphen.

How to change hostname on Ubuntu 20.04 without a system restart

Let us view current computer name setting, just type the following command:
hostnamectl
Next change hostname from localhost to ubuntu-2004-nixcraft, enter:
sudo hostnamectl set-hostname ubuntu-2004-nixcraft
Verify new changes:
hostnamectl

Find your public or private IP address by running the ip command:
ip a
ip a s eth0

Update /etc/hosts file with that IP address such as 10.105.28.43 using a text editor such as vim command/nano command:
$ sudo vi /etc/hosts
OR
$ sudo nano /etc/hosts
Update/append:
10.105.28.43 ubuntu-2004-nixcraft
Make sure you replace all occurrence of the old computer name with your new one too. Save and close the file in vim.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How to set pretty hostname on Ubuntu 20.04 LTS Linux

A pretty hostname is nothing bug human-readable UTF-8 machine name. For example, a name like “ Vivek’s ThinkPad Laptop ” is a pretty hostname. It is useful to display to the user and does not suffer from internet domain names’ syntax limitations. You must configure static hostname in /etc/hostname for fallback purposes. For example:
$ hostnamectl set-hostname nixcraft-wks01 —static
$ hostnamectl set-hostname » nixCraft’ workstation 01 » —pretty
Verify it by typing the any one of the following hostnamectl command:
$ hostnamectl
$ hostnamectl status
$ hostnamectl status —static
$ hostnamectl status —pretty
## or use the cat command ##
$ cat /etc/machine-info
$ cat /etc/hostname

How to Change Hostname on Ubuntu 20.04 LTS Linux

Note: We can pass the empty string “” as the hostname to reset the selected hostnames to their default (usually “localhost”):
hostnamectl set-hostname «»

Changing computer name using GUI method

Summing up hostnamectl commands and options

Command Description Examples
status Show current hostname settings hostnamectl status
set-hostname NAME Set system hostname hostnamectl set-hostname server1
set-icon-name NAME Set icon name for host. An icon identifying this machine must set as per the XDG Icon Naming Specification hostnamectl set-icon-name computer-tablet
set-chassis NAME Set chassis type for host. Currently, we can use “desktop”, “laptop”, “convertible”, “server”, “tablet”, “handset”, “watch”, and “embedded”, as well as the special chassis types “vm” and “container” for virtualized systems. hostnamectl set-chassis tablet
set-deployment NAME Set deployment environment for host. Can be “development”, “integration”, “staging”, or “production”. hostnamectl set-deployment production
set-location NAME Set location for host. Typically values are, “home”, “office”, “data center racke 1, 2nd shelf”, “London, UK” hostnamectl set-location «My home office, NYC»
—help Show this help hostnamectl —help
—version Show package version hostname —version
—no-ask-password Do not prompt for password hostnamectl —no-ask-password
—host=[USER@]HOST Operate on remote host. This will use SSH to talk to the remote machine manager instance. hostnamectl —host=vivek@server1 status hostnamectl —host=vivek@server1 set-hostname www-server-1
—machine=CONTAINER Operate on local container. In other words, Execute operation on a local container. Specify a container name such as ‘app’ to connect to hostnamectl —machine=app status
—transient Only set transient hostname. The “transient” hostname is a dynamic host name maintained by the kernel. It is initialized to the static host name by default, whose value defaults to “localhost”. It can be changed by DHCP or mDNS at runtime. Hence, we avoid setting up transient hostname manually. hostnamectl set-hostname «box1» —transient
—static Set static hostname hostnamectl set-hostname «server1.cyberciti.biz» —static
—pretty Only set pretty hostname hostnamectl set-hostname «server1 at linode data center, mumbai» —pretty

Conclusion

You learned how to change hostname permanently when using Ubuntu 20.04 LTS server or desktop computer as per your project needs. Please see the following man pages:
$ man hostname
$ man 7 hostname
$ man machine-info
$ man hostnamectl
$ hostnamectl —help

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Читайте также:  Чем заархивировать файлы для mac os
Оцените статью