How to find and replace ALL IP addresses with a new one?

In some situations, you need to change the VLAN IP address such as with 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 '' /etc/


A list of truncated files that needs to be updating:

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

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:

< bind
> bind


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 '' /etc/



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:

set -euxo pipefail
OLD_IP=''	# old server ip
NEW_IP=''	# 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)
# main
for f in $INPUT_FILES 
	for i in "${IGNORE_PATHS[@]}"
		if [[ $f == *"${i}"* ]]
	[ $is_skip == 0  ] && sed -i'.BACKUP' "s/${OLD_IP}/${NEW_IP}/g" "$f"


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

sudo systemctl restart haproxy.service


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