使用shell脚本将行附加到/ etc/hosts文件

Ste*_*lls 39 linux bash shell ubuntu sed

我有一个新的Ubuntu 12.04 VPS.我正在尝试编写一个完成整个LAMP安装的安装脚本.我遇到问题的地方是在/etc/hosts文件中附加一行.我当前的hosts文件如下所示:

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
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)

我sed使用append(\a)命令尝试了各种命令.出于某种原因,Ubuntu要么只是hosts在终端中回显文件的内容,要么根本不做任何事情.如何使用bash脚本正确地将第二行注入文件?

dam*_*ois 37

确保使用-i选项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
Run Code Online (Sandbox Code Playgroud)

除此以外,

echo "192.241.xx.xx  venus.example.com venus" >> /etc/hosts
Run Code Online (Sandbox Code Playgroud)

将该行附加到文件的末尾,这可以按预期工作.

  • 这个为我解决了:sudo - sh -c"echo test >>/etc/hosts"如果我尝试附加某些内容,只是sudo不起作用...... (8认同)
  • -bash:/ etc/hosts:权限被拒绝.有任何想法吗? (4认同)
  • 请注意,在Mac上,您需要指定`-i'',例如`sed -i'''2i192.241.11.22 venus.example.com venus'/ etc/hosts`.sed的Mac版本需要`-i`的扩展参数,即使它是空的. (3认同)

bri*_*uth 18

插入/更新条目

如果你想使用bash以编程方式插入/更新hosts条目,这是我写的一个脚本:

#!/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
Run Code Online (Sandbox Code Playgroud)

该脚本旨在与OS X一起使用,但也可以在linux上进行微调.

  • 在我看来,这是一个更好的解决方案,只需向 etc/hosts 添加一行即可 (4认同)

Jso*_*ras 12

如果您在Mac中或需要sudo权限,请尝试以下操作:

sudo -- sh -c -e "echo '192.34.0.03   subdomain.domain.com' >> /etc/hosts";
Run Code Online (Sandbox Code Playgroud)

它仍然会要求您输入密码.

来自@kainjow的替代方式

echo '192.34.0.03 subdomain.domain.com' | sudo tee -a /etc/hosts
Run Code Online (Sandbox Code Playgroud)

  • 您也可以使用`tee`:`echo'192.34.0.03 subdomain.domain.com'| sudo tee -a/etc/hosts` (6认同)
  • @powerbuoy 双标头`--` 在shell 命令中用于表示命令选项的结束,之后只接受位置参数。`sh` 是与标志 `-c` 一起使用的 shell 命令解释器,它导致从字符串操作数而不是标准输入中读取命令。`-e` 标志对于我们的用例是不必要的,但如果任何未经测试的命令在非交互模式下失败,它会导致解释的命令立即退出。 (2认同)

Vla*_*rov 5

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
Run Code Online (Sandbox Code Playgroud)