Change $HOST in /etc/hosts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change $HOST in /etc/hosts
# 1  
Old 04-04-2009
Change $HOST in /etc/hosts

I want to change the following lines in /etc/hosts on FreeBSD:
Code:
::1         localhost localhost.my.domain
127.0.0.1       localhost localhost.my.domain

into the following lines
Code:
::1         localhost foo.example.com
127.0.0.1       localhost foo.example.com

I have the following script to do that:
Code:
# set subdomain in hosts file
sed -i '' -e 's/localhost.my.domain/$HOST/' /etc/hosts

in an attempt to use the prepopulated environment variable $HOST for that. But the result is as follows:
Code:
::1         localhost $HOST
127.0.0.1       localhost $HOST

What can I do to correct this? Thanks in advance
# 2  
Old 04-04-2009
Use double quotes instead of single quotes in a sed command if you use shell variables.

Regards
# 3  
Old 04-04-2009
try this::

Quote:
sed 's/localhost.my.domain/' $HOST '/' /etc/hosts
# 4  
Old 04-05-2009
@dennis.jacob: tried your suggestion, but to no avail
@Franklin52: thanks, that worked
# 5  
Old 04-05-2009
Quote:
Originally Posted by figaro
@dennis.jacob: tried your suggestion, but to no avail
@Franklin52: thanks, that worked
It works well for me. Can you paste the error which you are getting.

Quote:
/home/aj >echo $HOST
foo.example.com
/home/aj >cat abc
::1 localhost localhost.my.domain
127.0.0.1 localhost localhost.my.domain
/home/aj >sed 's/localhost.my.domain/'$HOST'/' abc
::1 localhost foo.example.com
127.0.0.1 localhost foo.example.com
# 6  
Old 04-05-2009
dennis.jacob: Thank you for your response. Your code is working indeed after removing superfluous spaces. Previous error message was as follows:
Code:
# sed -i '' 's/localhost.my.domain/' $HOST '/' /etc/hosts
sed: 1: "s/localhost.my.domain/": unterminated substitute in regular expression

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Change from weak ssh host key to strong one

Hi, I got instructions from Security audit team for Solaris-10 server. They mentioned - "The sshd configuration on the host supported weak host keys and allowed password authentication on Solaris server. Enable stronger keys (2048 or 4096 bit)". I am not clear enough, what they mean by weak... (7 Replies)
Discussion started by: solaris_1977
7 Replies

2. Solaris

How to copy a tar file on a series of remote hosts and untar it on those hosts?

Am trying to copy a tar file onto a series of remote hosts and untar it at the destination. Need to do this without having to do multiple ssh. Actions to perform within a single ssh session via shell script - copy a file - untar at destination (remote host) OS : Linux RHEL6 (3 Replies)
Discussion started by: sankasu
3 Replies

3. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

4. Solaris

able to ping all hosts but not able to traceroute any host

i am using solaris 10 and i am able to ping all the hosts but i am not able to traceroute any of them. how to fix this? (9 Replies)
Discussion started by: chidori
9 Replies

5. Solaris

Can't change fully qualified host name

I tried changing my /etc/inet/hosts file for my server to: <ip address> <hostname> <fqdn> but when I go to reboot the file changes right back to: <ip address> <hostname> how do I get the <fqdn> to stick on a reboot. Thanks (2 Replies)
Discussion started by: jastanle84
2 Replies

6. Shell Programming and Scripting

rsh to many hosts the ftp output to single host

Hi guys. i need some help, i need to create a script in tcsh that rsh into all my hosts that we have at our business, then cd to a directory (cd /apps/users) then grab a file from the users folder and ftp it back to my windows machine. can someone please help? Kind regards. Brian Behrens (2 Replies)
Discussion started by: brian112
2 Replies

7. Shell Programming and Scripting

ssh to multiple hosts and saving the output in the local host

hi I have a script to login from a host "A" to a list of hosts in a file and perform some commands inside it...its somethin like this for i in `cat file` do ssh -t $i " command1 ; command2; ..." done I wanna save the outputs in a file in the current host "A" i.e from where I am... (3 Replies)
Discussion started by: ningy
3 Replies

8. Shell Programming and Scripting

Change .bashrc on a list of hosts

Hi, My scripting skills are somewhat basic... I need a way to log into a list of hostname/IPs as a user, su to root and then create/append root's .bashrc Thanks (0 Replies)
Discussion started by: jag7720
0 Replies

9. Shell Programming and Scripting

change password for multiple host

1-How can i change root password of 5 Fedora 11 machines (server1 server2 server3 server4 server5) with a single script , for example make password : 123456 NB. from server1 i can via ssh connect to the others machines without a password Please help. (3 Replies)
Discussion started by: chang-lee
3 Replies

10. AIX

NIM host name change

Hi I have a NIM server on AIX 5.3.08.02. I had to change the host name and I now have nimsh connection issues to the NIM clients. All host files and DNS entries are correct with the new host name. I have tried editing the niminfo files on the NIM server and NIM clinet. I have tried removing the... (2 Replies)
Discussion started by: kimyo
2 Replies
Login or Register to Ask a Question