How to Update Ubuntu IP and Hostname via Bash

Published:2 August 2022 - 5 min. read

Bill Kindle Image

Bill Kindle

Read more tutorials by Bill Kindle!

There are several ways to manage your Ubuntu computer’s IP address and hostname. System admins have the nmcli, netplan, hostname, and hostnamectl tools at their disposal. While these tools do a fine job on their own, how can you include these tools to up your automation game?

You can create a Bash script that will handle the configuration for you. In this article, you will learn how to set a new hostname and configure IP address information, all with a Bash script that accepts inputs.

Each example will build upon the other. By the end of the article, you will have the required knowledge to build a fully functional configuration script in Bash for use on a new or existing Ubuntu host.

Prerequisites

You do not have to be an expert at Bash shell scripting to follow this tutorial, but be sure to have the following requirements.

  • A host running on Ubuntu. This tutorial uses Ubuntu 22.04 LTS.
  • Root privileges to the Ubuntu host.
  • A text editor, such as vim, nano, or VSCode.

Setting the Hostname with hostnamectl

Every host should have a unique name. Using the hostnamectl command allows you to query or change the hostname. In this instance, the syntax is as shown below to change your current hostname to whatever you desire.

sudo hostnamectl set-hostname "new-hostname"

Running the hostnamectl command with the common parameter of set-hostname followed by a string value in double (“”) quotes as the new hostname. If you were running this command manually, it should work without a glitch.

But what if you wanted to put this into a script that accepts user input? Start by using the read command, followed by the parameter -p, which stands for prompt, and a string to tell a user what to enter for this prompt. You then save the input to a new variable, called NEW_HOSTNAME.

read -p "Please type the desired hostname: " NEW_HOSTNAME

Once the hostname is stored in the NEW_HOSTNAME variable, you can provide the variable to the hostnamectl command like the example below.

sudo hostnamectl set-hostname "$NEW_HOSTNAME"

And that’s it! Combining these two one-liners allows the commands to work in a script! But, you have one additional task: configuring the network options.

Setting the IP Address Information with netplan

The netplan command is the de facto method for managing the IP address information of a network interface for modern Ubuntu hosts. The netplan tool abstracts network configurations from various backends using YAML configuration files.

The netplan tool reads these configuration files during system boot and applies them to the target network interface(s). Why is that important, you might ask?

Having your network configuration in YAML empowers you to begin saving or backing up your configuration to a Git repository. And since Git provides change tracking capabilities, you can use that same configuration file without having to recreate it for future deployments using CI / CD pipelines. You know, DevOps!

Before adding lines to a configuration file, you must create the config file using the touch command.

sudo touch ~/99-custom.yaml

The above command will place the new configuration file into your home directory. From here, you will run the echo command to echo what you type and inject that into the configuration file line by line using input redirection for STDOUT > (add) and >> (append).

Note: Use double spaces and not tabs for indentation in YAML.

echo "network:" > ~/99-custom.yaml
echo "  ethernets:" >> ~/99-custom.yaml
echo "    enp0s31f6:" >> ~/99-custom.yaml
echo "      dhcp4: false" >> ~/99-custom.yaml
echo "      addresses:"
echo "       - [$IP_ADDRESS]" >> ~/99-custom.yaml
echo "      routes:"
echo "       - to: default"
echo "         via: $GATEWAY_ADDRESS" >> ~/99-custom.yaml
echo "      nameservers:" >> ~/99-custom.yaml
echo "        addresses: [$PRIMARY_DNS_ADDRESS, $SECONDARY_DNS_ADDRESS, 208.67.222.222, 208.67.220.220]" >> ~/99-custom.yaml
echo "  version: 2" >> ~/99-custom.yaml

With each echo command, you build the YAML configuration using appropriate spacing by redirecting input using STDOUT. Alternatively, you could build this file manually using a text editor. But, you do not need to build the file manually.

Instead, prompt the user to input the IP address information you need using the same read command.

Tip: One way to list all network interface information on Ubuntu is by running the ip address command.

read -p "Now type the IP address in CIDR notation, i.e. 192.168.1.1/24: " IP_ADDRESS
read -p "The gateway IP: " GATEWAY_ADDRESS
read -p "The primary DNS IP: " PRIMARY_DNS_ADDRESS
read -p "And finally, the secondary DNS IP: " SECONDARY_DNS_ADDRESS

That’s better. You can now redirect the user input again to a few variables for use in a script.

There are many other properties that you can set that this tutorial doesn’t cover in detail. If you would like to see additional common properties, be sure to read the netplan man pages.

With the network options complete, it’s time to build a single script!

Putting It All Together in a Bash Script

Now, you will combine everything into a single Bash script with what you learned from the previous sections. Open your editor of choice, paste the code below, and save the file as ~/host_deploy.sh.

#! /usr/bin/env bash
# host_deploy.sh
# Description:
# For use with Ubuntu Server ovf template. Assumes Ubuntu Cloud image and default sudo account.
# This script is meant to run with sudo.
# Example:
# sudo bash ~/host_deploy.sh

# Gather input from user
read -p "Please type the desired hostname: " NEW_HOSTNAME
read -p "Now type the IP address in CIDR notation, i.e. 192.168.1.1/24: " IP_ADDRESS
read -p "The gateway IP: " GATEWAY_ADDRESS
read -p "The primary DNS IP: " PRIMARY_DNS_ADDRESS
read -p "And finally, the secondary DNS IP: " SECONDARY_DNS_ADDRESS

# Set a new hostname
sudo hostnamectl set-hostname "$NEW_HOSTNAME"

# Create a new netplan yaml config file
sudo touch ~/99-custom.yaml

# Apply network config to netplan yaml config file
# Making some assumptions here about the adapter name
echo "network:" > ~/99-custom.yaml
echo "  ethernets:" >> ~/99-custom.yaml
echo "    enp0s31f6:" >> ~/99-custom.yaml
echo "      dhcp4: false" >> ~/99-custom.yaml
echo "      addresses:"
echo "       - [$IP_ADDRESS]" >> ~/99-custom.yaml
echo "      routes:"
echo "       - to: default"
echo "         via: $GATEWAY_ADDRESS" >> ~/99-custom.yaml
echo "      nameservers:" >> ~/99-custom.yaml
echo "        addresses: [$PRIMARY_DNS_ADDRESS, $SECONDARY_DNS_ADDRESS, 208.67.222.222, 208.67.220.220]" >> ~/99-custom.yaml
echo "  version: 2" >> ~/99-custom.yaml

# Copy the custom config to the netplan folder and apply
sudo cp ~/99-custom.yaml /etc/netplan/99-custom.yaml

# Apply the new config
sudo netplan apply

# Reminder to restart to reset state
echo "Please restart to complete host deployment!!"

To execute the script, run the below command to invoke the script with bash in your terminal.

sudo bash ~/host_deploy.sh

The script prompts you to enter a new hostname and IP address information and reminds you to reboot the host for the changes to take effect.

Now that you have a working script in your user directory, you could create a golden image to reuse or share with technicians and those who routinely perform this task.

Conclusion

Congratulations! You’ve made it to the end of the article and built a fully functional Bash script that you can use repeatedly.

You could also try to use this script with Ansible and answer the questions using an Ansible play. The tasks in this tutorial are only a tiny sample of initial configuration items that you could begin using in your build and deployment scripts.

You now know to accept user input, set basic system parameters using a Bash script, and apply that technique in changing hostnames and IP addresses in Ubuntu Linux.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!