append line to /etc/hosts file with shell script

匿名 (未验证) 提交于 2019-12-03 02:46:02

问题:

I have a new Ubuntu 12.04 VPS. I am trying to write a setup script that completes an entire LAMP installation. Where I am having trouble is appending a line to the /etc/hosts file. My current hosts file looks like this:

127.0.0.1       localhost Venus  # The following lines are desirable for IPv6 capable hosts ::1     ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

I would like it to look like this:

127.0.0.1       localhost Venus 192.241.xx.xx  venus.example.com venus  # The following lines are desirable for IPv6 capable hosts ::1     ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

I have tried a variety of sed commands using the append (\a) command. For some reason Ubuntu either just echoes the contents of the hosts file in terminal or does nothing at all. How would I properly inject the second line into the file with a bash script?

回答1:

Make sure to use the -i option of sed.

-i[SUFFIX], --in-place[=SUFFIX]   edit files in place (makes backup if extension supplied)  sed -i "2i192.241.xx.xx  venus.example.com venus" /etc/hosts 

Otherwise,

echo "192.241.xx.xx  venus.example.com venus" >> /etc/hosts 

would append the line at the end of the file, which could work as you expect.



回答2:

Insert/Update Entry

If you want to programmatically insert/update a hosts entry using bash, here's a script I wrote to do that:

#!/bin/bash  # insert/update hosts entry ip_address="192.168.x.x" host_name="my.hostname.example.com" # find existing instances in the host file and save the line numbers matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)" host_entry="${ip_address} ${host_name}"  echo "Please enter your password if requested."  if [ ! -z "$matches_in_hosts" ] then     echo "Updating existing hosts entry."     # iterate over the line numbers on which matches were found     while read -r line_number; do         # replace the text of each line with the desired host entry         sudo sed -i '' "${line_number}s/.*/${host_entry} /" /etc/hosts     done <<< "$matches_in_hosts" else     echo "Adding new hosts entry."     echo "$host_entry" | sudo tee -a /etc/hosts > /dev/null fi 

The script is intended for use with OS X but would work on linux as well with minor tweaking.



回答3:

If your in mac or you need sudo permission to this try this:

sudo -- sh -c -e "echo '192.34.0.03   subdomain.domain.com' >> /etc/hosts"; 

It will still ask you for password.



回答4:

echo "127.0.0.1 localhost `hostname`">./temp_hosts echo "192.241.xx.xx  venus.example.com">>./temp_hosts cat /etc/hosts |tail -n +2 >>./temp_hosts cat ./temp_hosts > /etc/hosts rm ./temp_file 


回答5:

I should point out that sed (the stream editor) is not actually intended for editing files, although it can be used to do that. (Standard sed doesn't have a built-in mechanism for writing to other than standard output.) A more appropriate tool would be ed.

The following ed script says "find the line containing the (admittedly sloppy) regular expression /127.0.0.1/ and append at the next line." (The lone period tells ed to stop appending.)

ed /etc/hosts <<-'EOF'     /127.0.0.1/a     192.241.xx.xx  venus.example.com     .     wq EOF 

That said, you can really just append this line to the end of your /etc/hosts file very trivially:

echo '192.241.xx.xx  venus.example.com' >> /etc/hosts 


回答6:

you can use sed, like:

sed '/Venus/ a\
192.241.xx.xx venus.example.com venus' /etc/hosts



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!