I'm trying to run the following command:
ssh user@box.example.com sudo command1 /path/to/file
But, an error is displayed follows:
sudo: sorry, you must have a tty to run sudo
How do I fix this problem on Linux or Unix based systems?
| Tutorial details | |
|---|---|
| Difficulty | Easy (rss) |
| Root privileges | No |
| Requirements | None |
| Estimated completion time | 2m |
Fixing: Sorry, you must have a tty to run sudo
You have to run your ssh command as follows to avoid this error:
ssh -t hostname sudo command ssh -t user@hostname sudo command ssh -t user@box.example.com sudo command1 /path/to/file
The -t option force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g., when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
The requiretty option in sudoers file
The requiretty if set in sudo config file sudoers, sudo will only run when the user is logged in to a real tty. When this flag is set, sudo can only be run from a login session and not via other means such as cron, shell/perl/python or cgi-bin scripts. This flag is set on many distores by default. Edit /etc/sudoers, file, enter:
# visudo
Find line that read as follows:
Defaults requiretty
Either comment it out the line or delete the line:
#Defaults requiretty
Save and close the file.
How Do I run command without using the -t option?
You can use the su command instead of the sudo command as follows:
su --session-command="/path/to/command1 arg1 arg2"
OR
ssh user@server1.nixcraft.in su --session-command="/path/to/command1 arg1 arg2"
You can run /scripts/job5143 as vivek user using the same syntax:
ssh user@server1.nixcraft.in su --session-command="/scripts/job1 /nas" vivek

本文介绍了解决在使用SSH执行包含sudo命令的远程命令时遇到的'tty not present'错误的方法。通过添加'-t'参数强制SSH分配伪终端,以及修改sudoers配置文件禁用'tty required'选项,可以解决此问题。此外,还提供了使用替代命令su的解决方案。

2万+

被折叠的 条评论
为什么被折叠?



