Thursday, November 30, 2023

How To: Update your hosts file

Firstly the /etc/hosts file is the primary resource for name resolution without DNS. The /etc/hosts file has priority over the DNS in the process of addressing network nodes. This is a table lookup file of host names and IP addresses. Our host name as well as others may be stored, in this file which we may edit.

The general file format should look like the following, and can include multiple host names for one IP:


       # The following lines are desirable for IPv4 capable hosts
       127.0.0.1       localhost

       # 127.0.1.1 is often used for the FQDN of the machine
       127.0.1.1       thishost.example.org   thishost
       192.168.1.10    foo.example.org        foo
       192.168.1.13    bar.example.org        bar

       # The following lines are desirable for IPv6 capable hosts
       ::1             localhost ip6-localhost ip6-loopback
       ff02::1         ip6-allnodes
       ff02::2         ip6-allrouters


So we use our favorite text editor, and update this file to fully implement any recent network updates.
We may also use the /etc/hosts file to include address changes to our LAN and implement network resolution restrictions.

If we remove a host name from /etc/hosts, we also will potentially render a service inoperable due to a defunct host name. Therefore each administrator may consider system integrity, with respect to any installed software (i.e. software may not start properly, without a valid host name).

You can also alter the order of name resolution, in the case you find /etc/hosts having priority to be inconvenient. The /etc/nsswitch.conf file can be edited, and should contain an entry like the following:

        hosts:          files dns

Given these options, we can take a flexible approach to managing the systems host name resolution.

Saturday, October 14, 2023

How To: Change a hostname in *nix

The hostname of most Linux and UNIX systems is a unique identifier set during OS installation. The process of changing this hostname can be accomplished through several methods.

Step 1: Checking the Current Hostname

Before you make any changes, find your current hostname. Use the following command:

hostname
 
Note alternatively you can use 'cat', 'echo', or 'printf' to print hostname too:

echo "$HOSTNAME"
 
printf "%s\n" $HOSTNAME 
cat /etc/hostname

This command will display your current hostname.
 
 
Note there is another command 'hostnamectl' which will print hostname, along with the Chassis, Machine ID, Boot ID, OS (Operating System), Kernel, Architecture, Hardware Vendor & Model:
 
hostnamectl 
 
This command will display additional information your current hostname and the machine.
 
Step 2: Changing the Hostname Temporarily

If you want to change your hostname temporarily (meaning it will revert back to the original after a system reboot), use the following command:

sudo hostname new_hostname

Replace "new_hostname" with your desired hostname.
 
 
Step 3: Changing the Hostname Permanently

To permanently change your hostname, you'll need to edit a specific file (/etc/hostname). To do this, use the nano text editor with the following command:

sudo nano /etc/hostname

hostnamectl


Once inside the file, delete the existing hostname and replace it with your new one. Save and exit by pressing Ctrl+X, then Y, and finally Enter.

Note: In some Linux distributions, you may also need to edit the /etc/hosts file for changes to take effect properly.
 
Step 4: Reboot and Apply Changes

For these changes to take effect we will complete a system reboot, use the following command:

sudo reboot

Now, if you check your hostname again using the "hostname" command, it should display your new hostname.

Please refer to your distribution's documentation for the most accurate instructions. 

To finalize your changes in the entire system, see the next article:

How To: Update your hosts file (it bypasses DNS)