you can also use the hostname command to display the current hostname of the system. However, if you want to set the hostname, you should use hostnamectl on modern systems and hostname on older systems with the following syntax
For modern systems:
hostnamectl set-hostname new-hostname
For older systems:
hostname new-hostname
note that in the original question, the command to set the hostname is requested, so in this case, the most appropriate command would be hostnamectl.
Using the hostnamectl is a better and permanent choice, but for name change is needed the sub-command "set-hostname" or "hostname" following the command.
example:
TestVM@Ubuntu-VM:~$ hostnamectl set-hostname TestVM2
this other form is accepted
TestVM@Ubuntu-VM:~$ hostnamectl hostname TestVM3
So we will enter two command followed by an argument.
MAN page for hostnamectl:
HOSTNAMECTL(1) hostnamectl HOSTNAMECTL(1)
NAME
hostnamectl - Control the system hostname
SYNOPSIS
hostnamectl [OPTIONS...] {COMMAND}
DESCRIPTION
hostnamectl may be used to query and change the system hostname and
related settings.
We can use the hostname command , but is not a permanent change, only 'til next reboot
Man page for hostname:
HOSTNAME(1) Linux Programmer's Manual HOSTNAME(1)
NAME
hostname - show or set the system's host name
...
DESCRIPTION
Hostname is used to display the system's DNS name, and to display or
set its hostname or NIS domain name.
...
>> Note, that this is effective only until the next reboot.
>> Edit /etc/hostname for permanent change.
Both hostname and hostname can set the hostname so why ask for one command? How are users supposed to 'guess' what command they had in mind? Anyway, from manual pages:
# man hostname
When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname uses the sethostname(2) function, while all of the three domainname, ypdomainname and nisdomainname
use setdomainname(2). Note, that this is effective only until the next reboot. Edit /etc/hostname for permanent change.
# man hostnamectl
set-hostname NAME
Set the system hostname to NAME.
the machine’s hostname can also be defined with
the hostnamectl command. When supplied with sub-command set-hostname, command
hostnamectl will take the name given as an argument and write it in /etc/hostname, so in this case the question is without parameters, will be HOSTNAME the answer
Most of all Linux systems run on systemd , the answer is hostnamectl with the set-hostname option which will change the contents of the /etc/hostname file which defines the hostname of the machine.
"The /etc/hostname file can be edited directly, but the machine’s hostname can also be defined with the hostnamectl command. When supplied with sub-command set-hostname, command hostnamectl will take the name given as an argument and write it in /etc/hostname:"
https://learning.lpi.org/en/learning-materials/102-500/109/109.2/109.2_01/#:~:text=The%20/etc/hostname%20file%20can%20be%20edited%20directly%2C%20but%20the%20machine%E2%80%99s%20hostname%20can%20also%20be%20defined%20with%20the%20hostnamectl%20command.%20When%20supplied%20with%20sub%2Dcommand%20set%2Dhostname%2C%20command%20hostnamectl%20will%20take%20the%20name%20given%20as%20an%20argument%20and%20write%20it%20in%20/etc/hostname%3A
"There is also a command hostname, with which you can show or set the system’s host name."
https://learning.lpi.org/en/learning-materials/010-160/2/2.1/2.1_01/#:~:text=There%20is%20also%20a%20command%20hostname%2C%20with%20which%20you%20can%20show%20or%20set%20the%20system%E2%80%99s%20host%20name.
yes, but it has options, I guess hostname will be the correct one for this question
upvoted 4 times
...
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Rsantos123
1 month, 1 week agoiagor_ml
3 months, 3 weeks agoccpmad
1 year, 2 months agokaramazov
1 year, 3 months agoklever
1 year, 5 months agoalextestwin
1 year, 7 months agolucaverce
2 years, 1 month agolucaverce
2 years, 1 month agojedrek
2 years, 2 months agojedrek
2 years, 2 months agomarenco
2 years, 3 months agoRobert12
2 years, 4 months agodrliu1202
1 year, 10 months agoisaac_paul
2 years, 6 months agodrliu1202
1 year, 10 months agodimasaburov
2 years, 6 months agojegga
2 years, 7 months agoca_tecnico
2 years, 9 months agoRaul_Andre
3 years, 7 months agoHieuNguyen887
3 years, 1 month agolinder
4 years agomustache
3 years, 10 months ago