- Linux change my hostname / computer system name
- How to display current hostname
- Change the Linux hostname
- Change hostname permanently on a Debian/Ubuntu Linux
- Linux change hostname using hostnamectl command ( systemd only)
- Query hostname with hostnamectl
- Change hostname with hostnamectl
- Set the deployment environment description
- How to set the location string for the system, if it is known
- A note about an RHEL (Red hat) / CentOS / Fedora Linux users
- Ubuntu Linux Change Hostname (computer name)
- Ubuntu change hostname command
- Display the current Ubuntu hostname
- How to change the Ubuntu server hostname without a system restart?
- Ubuntu Linux Change Hostname Using hostnamectl
- Conclusion
Linux change my hostname / computer system name
How to display current hostname
Just type hostname command to print the name of the system on screen:
$ hostname
Sample outputs:
Change the Linux hostname
Set hostname to desktop.nixcraft.com:
# hostname desktop.nixcraft.com
# hostname
Sample outputs:
Change hostname permanently on a Debian/Ubuntu Linux
You need to edit a file called /etc/hostname:
# vi /etc/hostname
Set new hostname:
Save and close the file. You need to reboot the system or run any one of the following command:
# /etc/init.d/hostname.sh start
OR
# invoke-rc.d hostname.sh start
Linux change hostname using hostnamectl command ( systemd only)
Most modern Linux distribution comes with systemd. If you are using systemd based distro try hostnamectl command.
Query hostname with hostnamectl
Let us print out current hostname:
$ hostnamectl
Change hostname with hostnamectl
The syntax is as follows to set hostname to ‘viveks-laptop’:
$ hostnamectl set-hostname ‘viveks-laptop’
Sample outputs:
Fig.01: hostnamectl in action on a Linux based system
- 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 ➔
Set the deployment environment description
ENVIRONMENT must be a single word without any control characters. One of the following is suggested: “development”, “integration”, “staging”, “production”. The syntax is:
$ hostnamectl set-deployment ENVIRONMENT
$ hostnamectl set-deployment production
How to set the location string for the system, if it is known
The syntax is:
$ hostnamectl set-location LOCATION
$ hostnamectl set-location «NYC Home»
$ hostnamectl set-location «DC 2, right rack, 2nd shelf»
A note about an RHEL (Red hat) / CentOS / Fedora Linux users
If you are using CentOS or Fedora or Redhat (RHEL) Linux, see this FAQ.
🐧 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.
Thanks. That’s exactly what I needed!
hostname papa.nixcraft.com —temp changing the hostname
For Permanent
vim /etc/hosts
vim /etc/sysconfig/network
i did as you tell but can’t ping to hostname. My hostname is linuxserver, when i use command ping it appear connect: Network is unreachable.
after changing, type “bash” and the prompt will change to the newest hostname your just changed.
this just doesn’t work, it seems like it does but you wont be able to use “sudo” anymore…..
hostname change command
Hi,
Thanks for pointing me to the right direction.
As for me and perhaps ppl using certain distro, may have to edit the /etc/hosts file as well.
sudo gedit /etc/hosts
127.0.0.1 localhost
127.0.1.1 newhostnamehere
——
Look for the line that says:
127.0.1.1 originalname
and change it to the newhostname
Regards,
Bob
PS: until I fix this, sudo took a long time trying to but fail to “resolve” the name, before asking for password and the GUI ‘update’ taskbar icon and GUI package manager stop working.
The same thing happened to me, thanks for the comment
hi
my need change host name in centos 5.7 ??
Really from my own point of view it is so amazing to use it please i need some e-book how to use it.
Thanks’
Martin
Setting up a CentOS 6 box today and was able to use the hostname command successfully without errors. Not sure why using the other FAQ is suggested.
Источник
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:
- 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. - Next Edit the /etc/hosts file:
sudo nano /etc/hosts
Replace any occurrence of the existing computer name with your new one. - 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=
Источник