简体   繁体   English

如何使用sudo权限从Jenkins运行远程ssh会话?

[英]How to run remote ssh session from Jenkins with sudo rights?

Using 'Execute shell script on remote host using ssh' option and need sudo rights on remote server to change permissions and remove protected files. 使用“使用ssh在远程主机上执行shell脚本”选项,并且需要远程服务器上的sudo权限才能更改权限并删除受保护的文件。 How to run session with this rights? 如何使用此权限运行会话?

Getting message 取得讯息

sudo: sorry, you must have a tty to run sudo sudo:对不起,您必须有一个tty才能运行sudo

when trying to run sudo command. 尝试运行sudo命令时。

If the remote server accepts the direct login of the root user you can simply do: 如果远程服务器接受root用户的直接登录,则只需执行以下操作:

ssh -l root yourserver command_to_execute

Similar syntax is: 类似的语法是:

ssh root@yourserver command_to_execute

Mind that allowing the login of the root user via ssh to a remote server isn't always a good solution. 请注意,允许root用户通过ssh登录到远程服务器并不总是一个好的解决方案。 A better solution would be change the owner / permissions to allow a non-root user to modify the protected files. 更好的解决方案是更改所有者/权限,以允许非root用户修改受保护的文件。

To run sudo remotely you have 2 options 要远程运行sudo您有2个选项

  1. Allow the user to run sudo commands without a password. 允许用户运行不带密码的sudo命令。

Append username ALL=(ALL) NOPASSWD: ALL the /etc/sudoers file with sudo visudo . 附加username ALL=(ALL) NOPASSWD: ALL /etc/sudoers文件都带有sudo visudo Alternatively you can modify this line to only allow certain sudo commands to be run without a password 或者,您可以修改此行以仅允许某些sudo命令在没有密码的情况下运行

  1. Use the pseudo-tty to emulate tty remotely and enter your sudo password when requsted. 使用伪tty远程模拟tty并在需要时输入您的sudo密码。

To do this run ssh -t username@host command_to_execute 为此,请运行ssh -t username@host command_to_execute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM