这是一个非常简单的问题,至少看起来应该是这样,关于Linux中的sudo权限。

很多时候,我只是想在/etc/hosts或类似的文件中添加一些内容,但最终无法添加,因为>和>>都不允许,即使使用root。

有没有什么方法可以让这个工作,而不需要su或sudo su到root?


当前回答

有些用户在使用多条线路时不知道如何解决。

sudo tee -a  /path/file/to/create_with_text > /dev/null <<EOT 
line 1
line 2
line 3
EOT

其他回答

sudo sh -c "echo 127.0.0.1 localhost >> /etc/hosts"

在bash中,您可以将tee与> /dev/null结合使用以保持stdout干净。

 echo "# comment" |  sudo tee -a /etc/hosts > /dev/null

你可以改变文件的所有权,然后在使用cat >>追加后将其更改回来吗?

sudo chown youruser /etc/hosts  
sudo cat /downloaded/hostsadditions >> /etc/hosts  
sudo chown root /etc/hosts  

这样的东西对你有用吗?

如何: Echo text | sudo dd status=none of=特权文件 我想更改/proc/sys/net/ipv4/tcp_rmem。 我做了: Sudo dd status=none of=/proc/sys/net/ipv4/tcp_rmem <<<"4096 131072 1024000" 消除单行文档的回显

问题是shell执行输出重定向,而不是sudo或echo,所以这是作为普通用户执行的。

试试下面的代码片段:

sudo sh -c "echo 'something' >> /etc/privilegedfile"