Linux change the name

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.

Источник

How to change the identity of a Linux system

Posted: February 1, 2021 | by Glen Newell

Photo by Pixabay from Pexels

I’d like to show you a magic trick. Go to your favorite shell prompt, and type:

More about automation

The hostname command is provided in Linux to query «who» the computer thinks it is. How did that happen? How does my computer know what it’s called? I show you how the trick is done in a bit, but first, let me show you another one:

Thanks once again to systemd (which is implemented in most but not all modern GNU/Linux distributions, including Red Hat-based systems) changing the name of your Linux computer is pretty easy. (By the way, the name in the prompt will change with the next login.) I’ve discussed how DNS allows us to identify a computer on the network by mapping a name to an IP address. This is on the other side of that. Here you’re looking at how the machine identifies itself, which is important for making things like authentication work properly if the computer is part of a domain.

After the hardware is enumerated and drivers have been loaded at bootup, on a systemd-based distribution, the bootloader loads and runs the systemd scripts. Near the beginning of this process is when the hostname is set:

Apart from that, in systems where systemd is not managing the user-space, you can configure the hostname in a couple of files. These files are actually where systemd looks for this info and records it when you use the hostnamectl command. The hostname and hostnamectl commands actually use kernel-level system calls named gethostname , getdomainname , and some functions provided by the resolver system call. These have been around since pretty early on in the history of the Linux kernel. These are the same system calls used by the shell to populate the machine’s name in the prompt, among other places.

Both of the files you’re going to look at reside in /etc , which you’ll recall is the default location for most of the files used to configure and manage a Linux system. The first one is called, appropriately, /etc/hostname :

Pretty simple: When you invoke hostname from the shell prompt, Linux looks at the /etc/hostname file for the answer. You can also use the hostname command to change the computer name, but in systemd-managed kernels, it’s generally preferred to use the hostnamectl command instead.

The other file is /etc/hosts , which is where Linux looks to translate IP addresses to names before it checks /etc/resolv.conf for DNS info. Here is the /etc/hosts file:

The first couple of lines are what you want to look at. Note the format is:

More Linux resources

So you have the IP address, then the hostname.domainname (or Fully Qualified Domain Name), followed by the hostname. You can also add aliases or additional domain aliases if they are used in the environment.

Use the -f switch when you invoke the hostname command to see the FQDN of your host:

The hostnamectl command has some other interesting and useful switches and flags: You can set the environment type (development vs. production, for example), and you can even put location notes (rack 3, room 200, for example). You can query and set these remotely. As usual, you can learn all kinds of cool things from the good ol’ man pages.

Источник

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=

Источник

Читайте также:  Перестал отображаться второй жесткий диск windows 10
Оцените статью