[英]Modify /etc/hosts file using a shell script
I do not have passwordless ssh enabled between my two servers a and b. 我没有在两个服务器a和b之间启用无密码ssh。 So I am using sshpass to connect to the server b from a.
所以我正在使用sshpass从a连接到服务器b。
I have a requirement to add host entries in the /etc/hosts of server b from a. 我需要从a在服务器b的/ etc / hosts中添加主机条目。 But the user that i am logging into server b is non-root user but has sudo privileges to edit files owned by root.
但是我登录到服务器b的用户是非root用户,但是具有sudo权限来编辑root拥有的文件。
How do i add host entries to /etc/hosts of server b from server a through a shell script while using sshpass. 在使用sshpass时,如何通过Shell脚本从服务器a将主机条目添加到服务器b的/ etc / hosts中。
Here is the script that was tried: 这是尝试过的脚本:
#!/bin/bash
export SSHPASS="password"
SSHUSER=ciuser
WPC_IP=10.8.150.28
sshpass -e ssh -o UserKnownHostsFile=/dev/null -o 'StrictHostKeyChecking no' $SSHUSER@$WPC_IP "echo test >> /etc/hosts"
Output: 输出:
bash test.sh
Warning: Permanently added '10.8.150.28' (RSA) to the list of known hosts.
bash: /etc/hosts: Permission denied
Thank you. 谢谢。
sudo
doesn't work with redirects directly, so you can use sudo tee -a
to append to a file: sudo
不能直接用于重定向 ,因此可以使用sudo tee -a
附加到文件:
echo '1.2.3.4 test' | sudo tee -a /etc/hosts
In your command, this would be: 在您的命令中,这将是:
sshpass -e ssh -o UserKnownHostsFile=/dev/null -o 'StrictHostKeyChecking no' "$SSHUSER@$WPC_IP" "echo test | sudo tee -a /etc/hosts"
Note that this requires passwordless sudo access without a tty, which is not necessarily the same as your sudo privileges. 请注意,这需要不带tty的无密码sudo访问,这不一定与您的sudo特权相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.