Ubuntu16.04 使用sudo cat EOF 编辑文件,提示Permission denied错误的解决办法

时间:2022-03-30 02:42:14

 

一、执行命令报错
在Ubuntu16.04下,使用如下命令,修改hosts主机文件,居然提示权限错误:

catty@node186:~$ sudo cat <<EOF > /etc/hosts
127.0.0.1 localhost
192.168.1.101 master1
192.168.1.102 worker1
192.168.1.103 worker2
192.168.1.104 worker3
EOF
-bash: /etc/hosts: Permission denied
catty@node186:~$

二、解决办法1

catty@node186:~$ sudo bash -c "cat > /etc/hosts" <<EOF
127.0.0.1 localhost
192.168.1.101 master1
192.168.1.102 worker1
192.168.1.103 worker2
192.168.1.104 worker3
EOF
catty@node186:~$

三、解决办法2:

catty@node186:~$ sudo tee /etc/hosts >/dev/null <<EOF
127.0.0.1 localhost
192.168.1.101 master1
192.168.1.102 worker1
192.168.1.103 worker2
192.168.1.104 worker3
EOF
catty@node186:~$

 

参考链接:
https://superuser.com/questions/340074/bash-permission-denied-issue-when-trying-to-append-to-eof

https://www.iteye.com/topic/1127130

http://www.ebanban.com/?p=677