LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-01-2016, 05:59 AM   #1
glennbtn
Member
 
Registered: Dec 2009
Posts: 128

Rep: Reputation: 18
Change ip using sed command


Hi All

We have a backup server which sometimes needs to be the master machine which just involves changing the ip address.

What I would like to do is have a bash script that will change the ip restart the service etc so it's up and running quick.

Can anyone tell me how I would change the ip using say the sed command from say my 10.10.10.2 address to 10.10.10.3

I have tried different examples found on the net but nothing seems to work. I would be looking to change the /etc/network/interfaces file

Thanks

Glenn
 
Old 08-01-2016, 06:05 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,177

Rep: Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752
Safer to have another file correctly set up in advance and merely copy it over. Make sure you have a backup of the original.

As for the sed, looks trivial - show us what you did, and what happened.
 
Old 08-01-2016, 06:09 AM   #3
glennbtn
Member
 
Registered: Dec 2009
Posts: 128

Original Poster
Rep: Reputation: 18
I have tried various 1's but non seem to work. No errors but no changes either

sed 's/10.10.10.2/10.10.10.3/'
sed -i -e "s@\b$10.10.10.2\b@$10.10.10.3@g"
sed -i -e "s/$10.10.10.2\b/$10.10.10.3/g"

Thanks

Glenn
 
Old 08-01-2016, 06:28 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,177

Rep: Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752Reputation: 3752
First two look sane - would need to see the file content and permissions.
 
Old 08-01-2016, 12:04 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
You can change ip on the fly using ifconfig too...
 
Old 08-01-2016, 08:45 PM   #6
24x7servermanagement
Member
 
Registered: Jul 2016
Location: India
Distribution: CentOS, Redhat, Ubuntu and Debian
Posts: 57

Rep: Reputation: Disabled
sed -i.bak 's/127.0.0.1/127.0.0.2/g' Path/of/filename

Should work fine. (-i.bak - create backup file of original file)

What is the error you are getting when you fire above command?

Last edited by 24x7servermanagement; 08-01-2016 at 08:49 PM. Reason: adding more stuff
 
Old 08-02-2016, 03:49 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,949

Rep: Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634
I'm with syg00, make 2 copies of the file and simply copy over whichever you need at any given time.
This way if something happens, you can easily recover & cp is simpler than sed
 
Old 08-02-2016, 06:24 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,257

Rep: Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542Reputation: 4542
I agree with keefaz you can change the IP on the fly by adding an alias or changing the interface by using the ip or the legacy ifconfig command which depends on your distribution. No need to restart anything.

I assume your services are configured to listen to all interfaces.

https://tty1.net/blog/2010/ifconfig-...arison_en.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command to global change if it match some pattern susanau Linux - Newbie 4 05-12-2015 03:57 AM
sed command to change variable names globally shridhar22 Linux - Newbie 21 02-06-2013 06:28 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 01:27 PM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 08:43 AM
Sed Command - - -How to change part of the name of multiple files sahil.jammu Linux - Newbie 6 04-02-2009 11:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration