---
title: "How to Update Ubuntu IP and Hostname via Bash"
description: "Discover and learn how to change a Linux IP and Hostname via a Bash script in this new tutorial by ATA Learning!"
canonical: "https://adamtheautomator.com/change-linux-ip-hostname-bash-script/"
---

# How to Update Ubuntu IP and Hostname via Bash

> Discover and learn how to change a Linux IP and Hostname via a Bash script in this new tutorial by ATA Learning!

Source: https://adamtheautomator.com/change-linux-ip-hostname-bash-script/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Update Ubuntu IP and Hostname via Bash](https://adamtheautomator.com/wp-content/uploads/2022/07/How-to-Update-Ubuntu-IP-and-Hostname-via-Bash.jpg)

# How to Update Ubuntu IP and Hostname via Bash

[![](https://secure.gravatar.com/avatar/4147968aa2332aa682bcebf295e4e9d0eb2672dee3d9ae0523a00ac51e7d6017?s=192&d=mm&r=g)Bill Kindle](https://adamtheautomator.com/author/bill/)2 August 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Bash](/tag/bash/)[Linux](/tag/linux/)[Linux Shell Scripting](/tag/linux-shell-scripting/)[Networking](/tag/networking/)[Ubuntu Linux](/tag/ubuntu-linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Setting the Hostname with hostnamectl](#setting-the-hostname-with-hostnamectl)
*   [Setting the IP Address Information with netplan](#setting-the-ip-address-information-with-netplan)
*   [Putting It All Together in a Bash Script](#putting-it-all-together-in-a-bash-script)
*   [Conclusion](#conclusion)

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_.

Related:[Your One and Only Shell Scripting Tutorial](https://adamtheautomator.com/linux-shell-scripting-tutorial/)

## Setting the Hostname with hostnamectl

Every host should have a unique name. Using the [hostnamectl command](https://manpages.ubuntu.com/manpages/jammy/en/man1/hostnamectl.1.html) 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.

```bash
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](https://linuxhint.com/bash_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.

```bash
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.

```bash
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](https://manpages.ubuntu.com/manpages/jammy/man5/netplan.5.html) 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](https://git-scm.com/) 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](https://manpages.ubuntu.com/manpages/jammy/en/man1/touch.1posix.html).

```bash
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](https://manpages.ubuntu.com/manpages/jammy/en/man1/echo.1plan9.html) 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._

```bash
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._

```bash
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](https://manpages.ubuntu.com/manpages/jammy/man5/netplan.5.html)._

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`.

```bash
#! /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.

```bash
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.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fchange-linux-ip-hostname-bash-script%2F&text=How%20to%20Update%20Ubuntu%20IP%20and%20Hostname%20via%20Bash)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fchange-linux-ip-hostname-bash-script%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fchange-linux-ip-hostname-bash-script%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/03/openldap-4.jpg)

### [How to Install and Configure an OpenLDAP Ubuntu Server](/openldap/)

Unlock the power of OpenLDAP on Ubuntu for centralized user authentication, seamless access control management, and enhanced directory services!

![](https://adamtheautomator.com/wp-content/uploads/2024/03/pipe-command-in-linux-1.jpg)

### [Unleashing the Power of the Pipe Command in Linux](/pipe-command-in-linux/)

Unlock the prowess of the pipe command in Linux to streamline tasks, boost productivity, and simplify complex operations effortlessly!

![](https://adamtheautomator.com/wp-content/uploads/2022/06/Understanding-Bash-If-Else-and-other-Conditional-Statements.jpg)

### [Understanding Bash If Else and other Conditional Statements](/bash-if-else/)

Discover the ins-and-outs of Bash If Else and Bash Case scripting in this example-driven tutorial by ATA Learning!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
