#!/bin/bash
read -p "Enter the new IP address i.e: 10.50.4.243/20 : " ip_address
read -p "Enter the new gateway i.e:- 10.50.0.1 : " gateway
read -p "Enter the new hostname i.e: co7-50-4-243 : " hostname
# Get the interface name
interface=$(ip route | awk '/default/ { print $5 }')
# Get the ip address
ipa=$(hostname -I | awk '{ print $1 }')
# Update the IP address and gateway
echo "Updating IP address and gateway..."
nmcli con mod "$interface" ipv4.address "$ip_address"
nmcli con mod "$interface" ipv4.gateway "$gateway"
# Update the hostname
echo "Updating hostname..."
hostnamectl set-hostname "$hostname"
# Update the /etc/hosts file
echo "Updating /etc/hosts file..."
echo "$ipa $hostname" >> /etc/hosts
# Restart the network service
echo "Restarting network service..."
systemctl restart NetworkManager
echo "IP address, gateway, hostname, and /etc/hosts file updated successfully."
Monday, July 24, 2023
How to Update CentOS IP and Hostname via Bash Script
I have created below mentioned bash script which will help you to update the IP Address and the hostname of the CentOS machines via this bash script.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment