LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-31-2012, 04:57 AM   #1
shogun1234
Member
 
Registered: May 2004
Posts: 226

Rep: Reputation: 15
update /etc/hosts


I setup 4 virtualbox vms which have dynamic ip (my env uses dhcp ).

Now I need to ssh to each vm so when ip is changed, I have to check vm's ip and update /etc/hosts so that when next time ssh user@vm_host would work.

So I wrote a simple script and want to update /etc/hosts once vms are started up. But it seems the content of /etc/hosts won't be changed after executing the script, and the console shows '127.0.0.1: not found' What may go wrong? And hot to fix it?

Below is the script content.

Code:
for i in 1 2 3 4 
do 
	IP=`VBoxManage guestproperty get box$i "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{print $2}'`
	
	`sudo sed 's/^(.*)		box$i\.domain\.com	box$i/^$IP	box$i\.domain\.com	box$i/g' /etc/hosts`
done

/etc/hosts looks as below:
Code:
127.0.0.1	localhost
192.168.1.111	my_host.domain.com	my_host
192.168.1.112	box1.domain.com	box1
192.168.1.114	box2.domain.com	box2
192.168.1.159	box3.domain.com	box3
192.168.1.199	box4.domain.com	box4
Thanks for advice.

Last edited by shogun1234; 04-01-2012 at 02:12 AM.
 
Old 03-31-2012, 06:13 AM   #2
ericson007
Member
 
Registered: Sep 2004
Location: Japan
Distribution: CentOS 7.1
Posts: 735

Rep: Reputation: 154Reputation: 154
I know this is not specifically what you have asked, however, why don't you just configure the individual machines with a static ip? There are only 4, it is not like there are thousands.
 
Old 03-31-2012, 06:29 AM   #3
shogun1234
Member
 
Registered: May 2004
Posts: 226

Original Poster
Rep: Reputation: 15
I share network environment with other people in the building, so ip would be conflict if using the same one.

Quote:
Originally Posted by ericson007 View Post
I know this is not specifically what you have asked, however, why don't you just configure the individual machines with a static ip? There are only 4, it is not like there are thousands.
 
Old 03-31-2012, 06:34 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Just choose a different subnet for the VMs and you should be fine.
 
1 members found this post helpful.
Old 03-31-2012, 06:38 AM   #5
ericson007
Member
 
Registered: Sep 2004
Location: Japan
Distribution: CentOS 7.1
Posts: 735

Rep: Reputation: 154Reputation: 154
You can also create a seperate subnet as the previous post said, or if you have control over the DHCP server or know what the address pool range is, assign 4 ip addresses that is not in the shared pool. That way there won't be conflicts either.
 
Old 03-31-2012, 07:32 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If you are getting the IPs OK, this is a straight sed question.

The sed command will not work as intended because the single quotes prevent substitution of the "$i"s. Change from single to double quotes and see if that works.

BTW the first part doesn't work for me:
Code:
c@CW8:~/d$ VBoxManage guestproperty get CW8vDS "/VirtualBox/GuestInfo/Net/0/V4/IP"
No value set!
 
Old 04-01-2012, 02:09 AM   #7
shogun1234
Member
 
Registered: May 2004
Posts: 226

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
If you are getting the IPs OK, this is a straight sed question.

The sed command will not work as intended because the single quotes prevent substitution of the "$i"s. Change from single to double quotes and see if that works.

BTW the first part doesn't work for me:
Code:
c@CW8:~/d$ VBoxManage guestproperty get CW8vDS "/VirtualBox/GuestInfo/Net/0/V4/IP"
No value set!
In addition to change from single quote to double quote. Adding -r for extended-re is required. Following command achieve the effect.
Code:
#!/bin/sh

for i in 1 2 3 4 
do 
	IP=`VBoxManage guestproperty get box$i "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{print $2}'`
	`sudo sed -i~ -r "s/^(.*)\tbox$i\.domain\.com\tbox$i/$IP\tbox$i\.domain\.com\tbox$i/g" /etc/hosts`
done
Thanks for all your help.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CentOS 5.7: How can DHCP automatically update /etc/hosts? needsleep Linux - Newbie 1 10-30-2011 11:38 PM
Script for hosts, numbers of hosts and users connected to squid server arunabh_biswas Programming 5 08-28-2010 04:11 AM
can't restrict sshd access through hosts.allow and hosts.deny but was working earlier farhan Linux - Security 4 04-18-2008 07:41 AM
script to update /etc/hosts on dhcp obtained ip change. juanctes Linux - General 1 02-02-2008 09:17 AM
script to update /etc/hosts bradut Linux - Newbie 8 07-16-2002 10:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:28 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