In some situations, you need to change the VLAN IP address such as 172.0.0.2 with 172.0.0.1 for ease of use. As you know, Linux and Unix-like operating system reserves all IP addresses and config in plain text files. Let's see where to find and replace ALL IP addresses with a new one on Linux.

Step 1 – Obtaining a list of files for the old IP address

Start using the  as follows:

grep -R -w '172.0.0.2' /etc/

 

A list of truncated files that needs to be updating:

/etc/keepalived/keepalived.conf:    unicast_src_ip  172.0.0.2 # Private IP address of master (redis1)
/etc/ufw/user.rules:### tuple ### allow tcp 6379 172.0.0.2 any 172.0.0.0/24 in_eth1 comment=4f70656e2054435020526564697320504f5254206f6e2072656469733120686f737420666f7220636c7573746572
/etc/ufw/user.rules:-A ufw-user-input -i eth1 -p tcp -d 172.0.0.2 --dport 6379 -s 172.0.0.0/24 -j ACCEPT
/etc/ufw/user.rules:### tuple ### allow tcp 26379 172.0.0.2 any 172.0.0.0/24 in_eth1 comment=4f70656e205443502053454e5449454c20504f5254206f6e2072656469733120686f737420666f7220636c7573746572
/etc/ufw/user.rules:-A ufw-user-input -i eth1 -p tcp -d 172.0.0.2 --dport 26379 -s 172.0.0.0/24 -j ACCEPT
/etc/systemd/network/.05-eth1.network.linode-last:Address=172.0.0.2/24
/etc/systemd/network/.05-eth1.network.linode-orig:Address=172.0.0.2/24
/etc/haproxy/haproxy.cfg:# redis1 - 172.0.0.2
/etc/haproxy/haproxy.cfg:        server redis2 172.0.0.2:6379 check inter 3s
/etc/hosts:172.0.0.2	redis1 haproxy1 keepalived1
/etc/redis/sentinel.conf:bind 172.0.0.2
/etc/redis/sentinel.conf:sentinel known-replica mymaster 172.0.0.2 6379
/etc/redis/redis.conf:bind 172.0.0.2
.....
..
..... 

Right now, you need to update services such as HAProxy, Nginx, Redis, MySQL, firewall rules, and more. The -R option read all files under each directory recursively, and it will also follow all symbolic links. The -w option will tell grep to select only those lines containing matches that form the whole word.

Step 2 – Obtaining and replacing IP address with a new one using sed command

This task is pretty simple, and the syntax for sed is as follows:

sed -i'.BACKUP' 's/OLD_IP_HERE/NEW_IP_HERE/g' input

The -i option tells sed to make a backup before updating file. I can verify using the diff command:

diff /etc/redis/redis.conf /etc/redis/redis.conf.BACKUP

Updated file /etc/redis/redis.conf line-by-line with original backup file named /etc/redis/redis.conf.BACKUP:

70c70
< bind 172.0.0.1
---
> bind 172.0.0.2

 

Step 3 – Shell script to find and replace ALL IP addresses

Here, you first need to update the grep command as mentioned to get a list of files:

grep -l -H -R -w '172.0.0.2' /etc/
 Outputs:

/etc/keepalived/keepalived.conf
/etc/ufw/user.rules
/etc/systemd/network/.05-eth1.network.linode-last
/etc/systemd/network/.05-eth1.network.linode-orig
/etc/haproxy/haproxy.cfg
/etc/hosts
/etc/redis/sentinel.conf
/etc/nginx/http.d/cyberciti.biz.conf
/etc/mysql/my.cnf

 

The -l option is most noted for mass editing as it will suppress standard output. Instead, grep will print the name of each input file. The -H options make the grep to shows each matching file name. Now moving to feed this list to sed using a while loop or bash for loop:

#!/bin/bash
set -euxo pipefail
OLD_IP='172.0.0.2'	# old server ip
NEW_IP='172.0.0.1'	# new server ip
DEST_DIR="/etc"		# search dir 
 
# Get file list
INPUT_FILES="$(grep -l -H -R -w ${OLD_IP} $DEST_DIR)"
 
# Ignore following dirs and files 
IGNORE_PATHS=(/etc/systemd/network/ /etc/network/ /etc/networks /etc/keepalived/keepalived.conf)
is_skip=0
 
# main
for f in $INPUT_FILES 
do
	for i in "${IGNORE_PATHS[@]}"
	do
		if [[ $f == *"${i}"* ]]
		then
			is_skip=1
			continue
		fi
	done
	[ $is_skip == 0  ] && sed -i'.BACKUP' "s/${OLD_IP}/${NEW_IP}/g" "$f"
	is_skip=0
done 

 

After running the script, restart those services one by one using the systemctl command. For example:

sudo systemctl restart haproxy.service

Done!! 

Was this answer helpful? 0 Users Found This Useful (0 Votes)