1

I am running Ubuntu 20.04 server. It is a fully configured LEMP email and web server. In my super long process of building this, i know I entered my servers IP address on one or many of my configuration and nginx/modsecurity files. The problem is that I am changing IP addresses for the server and i dont remember all of the files that i entered my ip address into for configuration purposes. Most of these configuratin files are in random directories only accessible by root. Most files end with .conf, .bak, .txt, or something else that I cant remember.

The problem is that I am changing IP addresses, so I need to replace my old ip address in all server files with my new IP address.

My question is, what command can i use to tell ubuntu to search all strings in all files for my old IP address, and replace them with my new IP address? This would save me the trouble of going through every file manually, and also ensure that I dont make a mistake and miss a file, thus having a server with some files with my old ip address and some files with my new ip address.

I dont know grep very well but i would imagine the command would include a grep command. Any help is much appreciated.

2
  • First, as admin you need to have some kind of "logbook" where you write down all changes!! Otherwise you will get in trouble sooner or later (as you are now). The option for grep is -r or completely: grep -r "192.168.0.2" /etc at least to find the files containing the ip. Better make it manual, otherwise your system might break and you are lost.
    – Marco
    Jun 8, 2023 at 6:11
  • 1
    Please do not do this and let an actual admin of that server do this. You do not use a comet from outer space to squat a fly that annoys you. Find out what software is running, find its conf file and alter that. And make documentation of what you change. Your method is a direct violation of the safety of your machine. 1 mistake and it is dead beyond repair.
    – Rinzwind
    Jun 8, 2023 at 8:45

1 Answer 1

1

You're unclear in what you ask. Here's how to search each and every file on your system for your old IP. Replace the / with another directory to limit the search.

sudo find / -type f -print0 |\
     xargs -0 -r sudo grep -F -l 'oldIP'

If you're happy with that list of files, do almost the same thing:

sudo find / -type f -print0 |\
     xargs -0 -r sudo grep -F -l --null 'oldIP' |\
     xargs -0 -r sudo perl -pi~ -e 's/oldIP/newIP/g' 

This will leave your original, unmodified files as filename~, for recovery, just in case. You should backslash-escape the dots in oldIP (but not newIP) in the perl expression, e.g. -e 's/192\.168\.97\.2/192.168.14.17/g'.

3
  • Thank you very much for this answer. This is what I was looking for my friend!
    – DanRan
    Jun 21, 2023 at 11:58
  • @DanRan Please click the checkmark to "accept" my answer, if it solved your problem, and might help others.
    – waltinator
    Jun 21, 2023 at 14:10
  • done and done. checkmarked.
    – DanRan
    Jun 22, 2023 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .