LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 04:59 AM   #1
glennbtn
Member
 
Registered: Dec 2009
Posts: 154

Rep: Reputation: 19
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, 05:05 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 05:09 AM   #3
glennbtn
Member
 
Registered: Dec 2009
Posts: 154

Original Poster
Rep: Reputation: 19
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, 05:28 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

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

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You can change ip on the fly using ifconfig too...
 
Old 08-01-2016, 07: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 07:49 PM. Reason: adding more stuff
 
Old 08-02-2016, 02:49 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 05:24 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,697

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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



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 02:57 AM
sed command to change variable names globally shridhar22 Linux - Newbie 21 02-06-2013 05:28 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
Sed Command - - -How to change part of the name of multiple files sahil.jammu Linux - Newbie 6 04-02-2009 10:57 PM

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

All times are GMT -5. The time now is 03:59 PM.

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