Bulk Replace
Web and code

How to Change an IP Address or Hostname in Many Config Files at Once

Change an old IP address or hostname across every config, ini and script file. Exact-match regex so 10.0.0.1 never hits 10.0.0.15, plus a safe checklist.

Updated 4 minute read The Bulk Replace team

To replace an IP address in multiple config files, run a regex find and replace across the config folder with the dots escaped and guards on both sides, so only that exact address changes. For example, find (?<![\d.])10\.0\.0\.1(?![\d.]) and replace with the new address. The escaped dots stop the pattern matching other characters, and the guards stop 10.0.0.1 from matching inside 10.0.0.15 or 110.0.0.1. Back up the files first, check the match count, then restart or reload the services that read them.

This guide gives tested patterns for IP addresses, hostnames and ports, a checklist of where old addresses hide, and a safe order of work for production servers.

Why a plain text replace is risky for IP addresses

A plain search for 10.0.0.1 finds every place those characters appear, including inside longer addresses:

Text in filePlain search 10.0.0.1Guarded regex
server=10.0.0.1Match (correct)Match
10.0.0.15Match (wrong, becomes 10.0.0.995 if the new address is 10.0.0.99)No match
110.0.0.1Match (wrong)No match
10.0.0.10Match (wrong)No match
10.0.0.1:8080Match (correct)Match, port kept

Whole-word matching gets you part of the way. Digits are word characters, so with whole word on, 10.0.0.1 no longer matches inside 10.0.0.15 or 110.0.0.1. But a dot counts as a word boundary, so it still matches inside longer dotted strings such as 10.0.0.1.5 in a version number or an SNMP identifier. A regex with explicit guards handles every case.

The exact-match IP pattern

(?<![\d.])10\.0\.0\.1(?![\d.]) has three parts:

  1. (?<![\d.]): not preceded by a digit or a dot, so 110.0.0.1 is skipped.
  2. 10\.0\.0\.1: the address, with each dot escaped so it only matches a literal dot. An unescaped dot matches any character.
  3. (?![\d.]): not followed by a digit or a dot, so 10.0.0.15 and 10.0.0.1.5 are skipped.

The guards are look-arounds, so they check the neighbors without changing them. A port after a colon, a quote, a semicolon or a slash is left exactly as it was. Look-arounds are explained in lookahead and lookbehind examples.

One edge case: an address at the end of a sentence, as in a comment that says moved to 10.0.0.1., is followed by a dot, so the pattern above skips it. If you want those too, use (?!\d|\.\d) as the right-hand guard: "not followed by a digit, or by a dot and a digit".

Changing a whole subnet

When a network is renumbered, every host keeps its last number but the network part changes. Capture the host number and reuse it:

  • Find: (?<![\d.])192\.168\.1\.(\d{1,3})(?![\d.])
  • Replace: 10.0.5.$1

192.168.1.100 becomes 10.0.5.100, and 192.168.10.5 is not touched. If only some hosts move, list them in separate exact-match rules instead. The capture groups guide explains $1 in detail.

Hostnames and ports

Hostnames have the same problem as IPs: db-old.example.local also appears inside staging-db-old.example.local. Word boundaries do not help, because a hyphen is not a word character. Guard the name with the characters that can appear in a hostname:

Find (?<![\w.-])db-old\.example\.local(?![\w-]) and replace with the new name. It changes db-old.example.local and db-old.example.local:5432, but not staging-db-old.example.local or db-old.example.localhost.

For a port, include its context so you only change the right number:

  • A port after a colon: find (?<=:)8080\b, replace 9090.
  • A port setting: find (?<=\bport\s*=\s*)8080\b with case-insensitive matching, replace 9090. The lookbehind keeps port = out of the match, so the replacement is just the number. In tools without variable-length lookbehind, capture the label instead and replace with ${1}9090 (the braces stop $19090 being read as one big group number).

Where old addresses hide

A server move usually fails because of the one file nobody remembered. Search, without replacing, across all of these before you plan the change:

LocationTypical files
Application config.config, .ini, .conf, .json, .xml, .yml, .properties
Environment files.env and its variants
Scripts.ps1, .bat, .cmd, .sh, .py
Web server and proxy configSite and virtual host config files
Connection stringsOften inside web.config or appsettings.json
Windows hosts fileC:\Windows\System32\drivers\etc\hosts (needs an administrator to edit)
Documentation and runbooks.md, .txt

Some settings do not live in text files at all: the registry, scheduled tasks, DNS records, firewall rules and database tables. A file search will not find those, so keep a separate list.

A safe order of work

  1. Inventory. Search for the old address across every config location and save the list of files and match counts.
  2. Back up. Copy the config folders, or commit them if they are under version control.
  3. Replace with exact-match patterns, filtered to config and script extensions. Leave out logs, which record history and should not be rewritten.
  4. Search again for the old address. The count should be zero, or only the matches you chose to keep, such as comments.
  5. Validate the files. For XML and JSON, parse them to confirm nothing broke; see find and replace in XML and JSON files.
  6. Restart or reload services, one at a time, and check their logs. Most services only read their config at start-up.

Doing it with a batch tool

Bulk Replace was built with this job in mind: target exact paths, ports and hostnames with regex and whole-word matching across a folder tree. Each address or hostname can be its own rule in one pass, and the before/after diff shows every changed line in every file, so a match inside the wrong address stands out before anything is written. Bulk Replace keeps each file's encoding, skips binary files automatically, and backs up every run, so if a service will not start afterwards you can restore the originals in one click. The CSV change report also gives you a record of exactly which files were edited, which helps with change control.

Quick reference

  • Exact IP: (?<![\d.])10\.0\.0\.1(?![\d.])
  • Exact IP, also at a sentence end: (?<![\d.])10\.0\.0\.1(?!\d|\.\d)
  • Subnet: (?<![\d.])192\.168\.1\.(\d{1,3})(?![\d.]) → 10.0.5.$1
  • Hostname: (?<![\w.-])db-old\.example\.local(?![\w-])
  • Port after a colon: (?<=:)8080\b

Frequently asked questions

How do I stop 10.0.0.1 matching 10.0.0.15?

Escape the dots and add guards: (?<![\d.])10\.0\.0\.1(?![\d.]) matches only that exact address.

Why escape the dots in an IP address?

An unescaped dot matches any character, so 10.0.0.1 would also match 10a0b0c1. \. matches only a literal dot.

Should I restart services after changing config files?

Usually yes. Most services read their configuration at start-up, so restart or reload them and check the logs afterwards.

Get three guides like this every month, plus software update notices.Subscribe →