Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - change the hostname in the CLI

I don't know how to search for this and that is why I asked for it (all my searches did not reveal any relevant information).

I have a Fedora 18 server that looks like this:

[root@dhcp-192-168-5-100 ~]#

I want to change that to:

[root@server1 ~]#

Currently, this machine is set to get IP through DHCP, but that is not the IP address of the machine and that is why I need to change the CLI to something more relevant.

Thank you

like image 372
user1984646 Avatar asked Jan 31 '13 22:01

user1984646


People also ask

Can we change the hostname in Linux?

To change the hostname of a device in Linux, we recommend using CLI tools. These methods are universal and will work on almost any Linux distro. Furthermore, there is also no need to install any additional packages. First, check the hostname of the current system as follows.

How do I set the hostname variable in Linux?

How to Set the Hostname. Let's just edit the /etc/hostname file to change the hostname. To do that, we should be root or the sudo user. Then, the modification is immediately reflected by the hostname command and isn't lost after rebooting.


2 Answers

  1. Edit /etc/sysconfig/network and change/add HOSTNAME variable like so HOSTNAME=server1.domain.com After restart it should have server1.
  2. Edit /etc/hosts and add server1 and server1.domain.com to 127.0.0.1 line, so it will look like: 127.0.0.1 localhost.localdomain localhost server1 server1.domain.com

More about network file you can read here: https://docs.fedoraproject.org/en-US/Fedora/15/html/Deployment_Guide/ch-The_sysconfig_Directory.html#s2-sysconfig-network

like image 134
Enobe Avatar answered Oct 21 '22 01:10

Enobe


you can type "hostname HOSTNAME" where HOSTNAME is the new name you want. The next time you log in / connect via ssh, that's what you'll see.

like image 37
Joe T Avatar answered Oct 21 '22 00:10

Joe T