为了安全起见,我正在设置一个云节点(仅可通过 ssh 访问,已弃用 GUI,运行 Debian 7),并使用“PermitRootLogin no”。对于某些应用程序,我想将一些根拥有的配置文件从远程复制到本地(例如,设置本地客户端)。而且,为了可重复性,我想编写这个脚本(已经从格式错误的命令行中冲洗过几次)。启用远程的最佳实践是什么sudo
从脚本(是的,我知道我必须输入密码——完全的非交互性会很好,但不是必需的),如果给出“PermitRootLogin no”(并且没有物理访问)?我尝试过的一些事情:
(1) scp
:失败,因为我不知道scp
我想要sudo
远程。 (AFAICS——我错过了什么吗?)
(2) 裸体rsync-path
:我试过了
me@local ~ $ rsync --rsync-path='sudo rsync' remote:/path/to/file ${HOME}/backups/cloud_9/path/to/file
X11 forwarding request failed on channel 0
sudo: no tty present and no askpass program specified
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
...
可以启用非图形 Askpass 吗?随意的鸭鸭Go-ing 没有找到。笔记rsync-path
将要(IIRC)与“visiblepw”一起工作,但不是赤裸裸地/没有。
(3) sudoers.d/visiblepw
:发现DDG
http://www.sudo.ws/pipermail/sudo-users/2009-August/004142.html
> [sudo-users] Error: sudo: no tty present and no askpass program specified
> Todd C. Miller Todd.Miller at courtesan.com
> Mon Aug 3 10:43:04 EDT 2009
> Recent versions of sudo will refuse to prompt for a password if no tty is present,
> since it is not possible to turn off echo in this case. You can [enable this]
> with a line like:
> Defaults visiblepw
所以我创建了
me@remote:~$ sudo cat /etc/sudoers.d/visiblepw
> Defaults visiblepw
这是有效的,但是,如上所述,远程 ingecho
时 s pw sudo
...但现在我可以,例如ssh remote 'sudo whatever'
...只要没有人能看到我的 shell :-)
我想知道,有更好的方法吗?我错过了什么吗?再次注意,我希望它可以通过脚本运行。
还有一件事(在写作时注意到),是我在写作时发现的,但没有尝试过,因为它看起来很难看:
(4)mkfifo
跳圈:看起来很丑而且不太适合编写脚本。我错过了什么吗?
答案1
更好的方法可能是安西布尔- 通过 ssh 与遥控器交互的配置管理工具。它极大地简化了远程 ssh 身份验证和 sudo 或 su 的任务。
安装 Ansible 并配置主机清单后,执行此操作的命令是:
ansible remotehost -m fetch -a "src=/etc/sudoers dest=remote-soduers" -u root --sudo --ask-sudo-pass
如果您需要配置一个简单的清单:
cat > hosts <<EOF
[remotes]
remotehosts
EOF
ansible -i hosts remotehost -m fetch -a "src=/etc/sudoers dest=remote-soduers" -u root --sudo --ask-sudo-pass
或者,可以通过使用 ssh 上的“-t”选项强制伪终端分配来完成快速的一次性命令:
ssh -t remotehost "sudo rsync -av /path/on/remote localuser@localhost:/path/on/local"
这很可能会导致出现两个密码提示:一个用于 sudo,另一个用于从 root@remote 连接回本地。