sudo: no tty present and no askpass program specified

29 篇文章 0 订阅

通过ssh  host  cmd ,在本机可以直接在远端服务器上执行命令并返回;

但是如果 cmd 中有使用sudo的相关命令和脚本时,报错:sudo: no tty present and no askpass program specified 

可以通过 ssh -t host cmd解决;


ssh的-t参数

[html]  view plain copy print ?
  1. -t      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.  

SSH TTY 问题

远程执行命令时,SSH默认不会分配tty。没有tty,sudo就无法在获取密码时关闭回显。使用-tt选项强制SSH分配tty(使用两次-tt)。

另一方面,sudoers中的Defaults选项requiretty要求只有拥有tty的用户才能使用sudo。可以通过visudo编辑配置文件,禁用这个选项:

# Disable "ssh hostname sudo <cmd>", because it will show the password in clear text. 
# You have to run "ssh -t hostname sudo <cmd>".
#
# Defaults    requiretty

更多补充:如果启动后台脚本:

参见:http://unix.stackexchange.com/questions/98746/ssh-with-sudo-doesnt-start-services


sudo: Sorry, you must have a tty to run sudo Error and Soution

by NIX CRAFT on AUGUST 8, 2007 · 6 COMMENTS· LAST UPDATED DECEMBER 8, 2013

in BASH SHELLLINUXTROUBLESHOOTING

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
This is done in Fedora, RHEL, CentOS, many other Linux distribution, and Unix-like systems for security concern as it will show the password in clear text format.

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
 
above origin: http://www.cyberciti.biz/faq/linux-unix-bsd-sudo-sorry-you-must-haveattytorun/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值