Changing Hostname in Linux
January 10, 2012 Leave a comment
The hostname in Linux is what appears at the left of the cursor in terminal. Using hostnames over ip addresses when creating shortcuts, network printers and mapped network drives is generally preferable because the path is abstracted from the assigned ip address. If the ip ever changes, you will not need to reset up your links.
Here is how to change it on Mac, Linux – Debian Based, Linux – RedHat Based, and BSD
Identify your current hostname
On Linux systems you can find out what your hostname is by typing in the command
Temporarily Change Name
You could temporarily change the hostname by typing in:
This works on linux and mac systems.
The change will not persist across reboots.
Scroll down for the persistent solution.
Permanently Change Host Name
Linux – Redhat / Fedora and Derivatives
Changing the hostname on a Red Hat or derivative machine requires editing the ‘network’ file located in /etc/sysconfig.
The name should already be populated from the initial install.
sudo nano /etc/sysconfig/network
( I am aware of the typo in fedora, This post was a result of needing to fix it)
Linux – Ubuntu and Debian Derivatives
Linux Distros based on Debian such as ubuntu and CentOS have a slightly different way of changing the hostname.
For them, you must edit the /etc/hostname file
sudo nano /etc/hostname
You will then need to reboot, or if you are in a hurry, you can type in this command
The file should already have the name of your system listed. By default it is just ubuntu
After changing it, you should restart the computer. Logging out isn’t sufficient.
Mac – OSx
While there may be a way to edit a file on mac, it is simpler and quicker to run the scutil command. Enter the word HostName followed by the new name you want to assign it. Note that there are 2 dashes ’ — ‘ with no spaces in-between them
scutil — set HostName Spencers-New-Name
As always, you should restart the computer to update all references of the old name stored in memory.
You must exit and log back in as root
Add this line to the end of the config file. Including quotes
hostname = “some-new-name”
You should restart to make the changes stick.