- Linux Find Out My Machine Name/Hostname
- The procedure to find the computer name on Linux:
- Linux find hostname using hostnamectl
- Linux find computer name using hostname command
- How Do I Change My Hostname?
- Conclusion
- Getting help
- Linux hostname command
- Description
- Syntax
- Options
- The FQDN
- Files
- Examples
- Related commands
- How do I change the computer name?
- 14 Answers 14
- 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 Find Out My Machine Name/Hostname
The procedure to find the computer name on Linux:
- Open a command-line terminal app (select Applications > Accessories > Terminal), and then type:
- hostname
OR
hostnamectl
OR
cat /proc/sys/kernel/hostname - Press [Enter] key
Linux find hostname using hostnamectl
Type the following command if you are using systemd based Linux distro:
$ hostnamectl
It is possible to filter output using the grep command:
$ hostnamectl | grep ‘hostname’
OR simply use the cat command as follows too:
$ cat /proc/sys/kernel/hostname
Linux find computer name using hostname command
At the terminal type hostname command:
$ hostname
Sample outputs
vivek-laptop.nixcraft.in
Where,
- vivek-laptop : You computer name.
- nixcraft.in : Your dNS domain name.
- vivek-laptop.nixcraft.in : Your computer name with Fully Qualified Domain Name (FQDN).
To see the domain part of the FQDN (Fully Qualified Domain Name), enter:
$ dnsdomainname
Sample outputs:
- 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 Do I Change My Hostname?
The hostname commands set the host name using the following syntax. Please note that only the super-user / root can change the names. To switch to the root user by typing su – and entering the root password, when prompted.
# hostname newhostname.nixcraft.in
# hostname laptop.nixcraft.net.in
# hostnamectl set-hostname laptop.nixcraft.in
You need to edit /etc/hostname or /etc/sysconfig/network file to set hostname permanently. See our previous FAQ about changing hostname using configuration files.
Conclusion
The hostnamectl command may be used to query and change the system hostname and related settings on modern Linux distros such as Debian, Ubuntu, CentOS/RHEL, Fedora, Arch Linux and more.
Getting help
I strongly suggest that your read the man pages by typing the man command:
$ man hostname
$ man hostnamectl
$ hostnamectl —help
Источник
Linux hostname command
On Unix-like operating systems, the hostname command shows or sets the system hostname.
This page covers the GNU/Linux version of hostname.
Description
hostname is used to display the system’s DNS name, and to display or set its hostname or NIS (Network Information Services) domain name.
When called without any arguments, hostname displays the name of the system as returned by the gethostname function.
When called with one argument or with the —file option, hostname sets the system’s hostname using the sethostname function. Only the superuser can set the hostname.
The hostname is usually set once at system startup in the script /etc/init.d/hostname.sh normally by reading the contents of a file which contains the hostname, e.g., /etc/hostname.
Syntax
Options
-a, —alias | Display the alias name of the host (if used). This option is deprecated and should not be used anymore. |
-A, —all-fqdns | Displays every FQDN of the machine. This option enumerates all configured network addresses on all configured network interfaces, and translates them to DNS domain names. Addresses that cannot be translated (i.e., because they do not have an appropriate reverse DNS entry) are skipped. Note that different addresses may resolve to the same name, therefore the output may contain duplicate entries. Do not make any assumptions about the order of the output. |
-b, —boot | Always set a hostname; this allows the file specified by -F to be non-existant or empty, in which case the default hostname localhost will be used if none is yet set. |
-d, —domain | Display the name of the DNS domain. Don’t use the command domainname to get the DNS domain name because it shows the NIS domain name and not the DNS domain name. Use dnsdomainname instead. See the warnings in the FQDN section, and avoid using this option if at all possible. |
-f, —fqdn, —long | Display the FQDN (fully qualified domain name). A FQDN consists of a short hostname and the DNS domain name. Unless you are using BIND (Berkeley Internet Name Domain) or NIS for host lookups, you can change the FQDN and the DNS domain name (which is part of the FQDN) in the /etc/hosts file. See the warnings in the FQDN section, and avoid using this option if at all possible; use hostname —all-fqdns instead. |
-F, —file file name | Read the hostname from the specified file. Comments (lines starting with a `#‘) are ignored. |
-i, —ip-address | Display the network address(es) of the hostname. Note that this works only if the hostname can be resolved. Avoid using this option if at all possible; use hostname —all-ip-addresses instead. |
-I, —all-ip-addresses | Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output. |
-s, —short | Display the short hostname. This is the hostname cut at the first dot. |
-v, —verbose | Be verbose with all output. |
-V, —version | Print version information on standard output and exit successfully. |
-y, —yp, —nis | Display the NIS domain name. If a parameter is given (or —file name ) then root (the superuser) can also set a new NIS domain. |
-h, —help | Print a help message and exit. |
The FQDN
The FQDN (fully qualified domain name) of the system is the name that the resolver returns for the hostname, such as mysubdomain.example.com. It is usually the hostname followed by the DNS domain name (the part after the first dot). You can check the FQDN using hostname —fqdn or the domain name using dnsdomainname.
You cannot change the FQDN with hostname or dnsdomainname.
The recommended method of setting the FQDN is to make the hostname be an alias for the fully qualified name using /etc/hosts, DNS, or NIS. For example, if the hostname was «mysubdomain«, one might have a line in /etc/hosts which reads:
Technically: The FQDN is the name getaddrinfo returns for the hostname returned by gethostname. The DNS domain name is the part after the first dot.
Therefore it depends on the configuration of the resolver (usually in /etc/host.conf) how you can change it. Usually, the hosts file is parsed before DNS or NIS, so it is most common to change the FQDN in /etc/hosts.
If a machine has multiple network interfaces/addresses or is used in a mobile environment, then it may either have multiple FQDNs/domain names or none at all. Therefore avoid using hostname —fqdn, hostname —domain and dnsdomainname.
hostname —ip-address is subject to the same limitations so it should be avoided as well.
Files
/etc/hostname | Historically this file was supposed to only contain the hostname and not the full canonical FQDN. Nowadays most software can cope with a full FQDN here. This file is read at boot time by the system initialization scripts to set the hostname. |
/etc/hosts | Usually, this is where one sets the domain name by aliasing the hostname to the FQDN. |
Examples
Displays the system hostname.
Related commands
hostid — Print the numeric identifier for the current host.
uname — Print information about the current system.
Источник
How do I change the computer name?
When I connect to my server (ubuntu server 10.10), I get this:
How can I remove «.belkin»?
14 Answers 14
You need to edit the computer name in two files:
These will both need administrative access, so run
Replace any instances of the existing computer name with your new one. When complete run
The name will also be changed if you restart your computer.
hostnamectl set-hostname on 13.10+ desktop
This is the best way if you have systemd (13.10 onwards) and if cloud-init is not active (see below):
- does not require rebooting
- persists after reboots
18.04 onwards: cloud-init
18.04 Introduced cloud-init which can control setting of the hostname so hostnamectl changes it won’t stick after a reboot if cloud-init is installed. TODO: how to check if it is installed, is it installed by default on the desktop image or just server?
If you want hostnamectl changes to stay after a reboot, then you’ll need to edit the cloud-init config files, disable cloud-init’s hostname set/update module:
or disable cloud-init entirely:
Edit /etc/hostname , make the name change, save the file.
You should also make the same changes in /etc/hosts file
Run sudo service hostname start
As long as you have no application settings depending on the ‘old’ hostname, you should be ok 😉
It is safe to do, you just need to be sure you edit both the system hostname configuration file (/etc/hostname) and the hostname name resolution file (/etc/hosts). From a terminal execute the following:
In addition to editing /etc/hosts and /etc/hostname, various services might have issues with the change as well. Mysql and postfix are installed by default in ubuntu. A broken postfix won’t affect most ubuntu users, since it’s a background email server that isn’t used by much.
The default config for mysql doesn’t use hostname, so it will work fine as-is. If you have customized it, edit the files in /etc/mysql/ and restart the service.
You may also want to edit /etc/motd (message of the day), which is shown on virtual terminals and remote logins. That one won’t harm anything though.
Other services that you may have installed that would need fixing are apache, bind9, etc. In each case, find and edit the hostname in their config and restart the service.
Источник
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=
Источник