如何使用sudo命令来添加内容到文件中
source link: https://www.lujun9972.win/blog/2017/06/04/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8sudo%E5%91%BD%E4%BB%A4%E6%9D%A5%E6%B7%BB%E5%8A%A0%E5%86%85%E5%AE%B9%E5%88%B0%E6%96%87%E4%BB%B6%E4%B8%AD/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何使用sudo命令来添加内容到文件中
从https://www.cyberciti.biz/faq/sudo-append-data-text-to-file-on-linux-unix-macos/ 中看到的
当我们想往root所属文件中添加内容时,你可能会写出这样的语句
sudo echo '192.168.1.254 route' >> /etc/hosts
然而,这种写法是错误的,它的意思是执行sudo命令,然后将sudo命令的结果添加到/etc/hosts中.
所以,当你用非root用户执行上一语句时,会提示 bash: /etc/hosts: Permission denied
.
正确的做法是为sudo命令使用tee命令或者shell本身:
使用tee命令
echo '192.168.1.254 router' | sudo tee -a /etc/hosts
使用shell本身
sudo sh -c 'echo "192.168.1.254 router" >> /etc/hosts'
Related Issues not found
Please contact @lujun9972 to initialize the comment
Generated by Emacs 27.x(Org mode 9.x)
Copyright © 2014 - 2021 lujun9972
-
Powered by EGO
Themed with emacs_love
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK