2017年4月22日 星期六

[Linux 常見問題] sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix

Source From Here 
Question 
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

OR 
sudo: no tty present and no askpass program specified

How do I fix this problem on Linux or Unix based systems? 

How-To 
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. 

What is the fix on a Linux or Unix bash shell? 
You have to run your ssh command as follows to avoid this error: 
// -t: Force pseudo-tty allocation. 
# ssh -t hostname sudo command
# ssh -t user@hostname sudo command
# ssh -t user@box.example.com sudo command1 /path/to/file

Sample session: 

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 and either comment it out the line or delete the line:
  1. ...  
  2. #Defaults    requiretty  

Save and close the file.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...