Error sudo: no tty present and no askpass program specified while using with sshpass
As details by this answer on StackExchange, the /etc/sudoers
file on the remote host is likely disallowing you from running sudo commands without a tty. It also has a number of detailed recommendations for working around the problem if you can't fix it.
Jenkins sudo: no tty present and no askpass program specified with NOPASSWD
I've tested the solution described by @Jayan in the comments of the question. You must include the new line at the end of the file:
Solution: https://stackoverflow.com/a/24648413/54506
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
jenkins ALL=(ALL) NOPASSWD: ALL
Python + Paramiko + Error: sudo: no tty present and no askpass program specified
I am able to get the solution for this problem.
I have used send(..) instead of execute_command(...).
SETUP:
self.client = paramiko.client.SSHClient()
self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
self.client.connect(hostname=self.str_host_name, port=22,
username=self.str_user_name, password=self.str_user_pass)
self.transport = paramiko.Transport(self.str_host_name, 22)
self.transport.connect(username=self.str_user_name, password=self.str_user_pass)
EXECUTION:
if self.shell:
self.shell.send(str_sudo_command + "\n")
if self.shell is not None:
time.sleep(2)
self.str_sudo_command_result += str(self.shell.recv(1024).decode('utf-8'))
self.str_sudo_command_result = str(self.str_sudo_command_result).strip()
self.str_sudo_command_result = self.str_sudo_command_result.splitlines()
if len(self.str_sudo_command_result) > 0:
if "[sudo] password for " in self.str_sudo_command_result[-1]:
self.str_sudo_command_result = ""
self.shell.send(self.str_user_pass + "\n")
time.sleep(2)
else:
while True:
result = str(self.str_sudo_command_result)
result = result.splitlines()
time.sleep(2)
if self.str_result_end_line not in result[-1]:
while self.shell.recv_ready():
self.str_sudo_command_result += str(self.shell.recv(9999).decode('utf-8'))
else:
break
Suggestions and corrections are welcomed.
Cannot SUDO SU anymore, no tty present and no askpass program specified
sudo
tries to open /dev/tty
for read-write and prints that error if it fails. You've indicated in comments that /dev/tty is missing on your system.
Sudo has an option -S
to read the password from standard input instead of /dev/tty. You should be able to run sudo -S
to become root.
Regarding how to recover /dev/tty, It's possible that rebooting the server would be sufficient; the system might recreate all devices in /dev during bootup. Alternately, to create a device, you use the mknod
command, but you need to know the correct major and minor numbers for the tty device. On an Ubuntu system I have available, I see these entries in /dev:
crw------- 1 root root 5, 1 Apr 16 18:36 console
crw-rw-rw- 1 root tty 5, 2 Sep 24 15:35 ptmx
crw-rw-rw- 1 root tty 5, 0 Sep 24 14:25 tty
In this case, the major number is 5 and the minor number is 0. /dev/console and /dev/ptmx have the same major number. So I'd inspect /dev/console or /dev/ptmx to find the correct major number, then run:
mknod /dev/tty c major 0
where "major" is the correct major number.
After recreating /dev/tty, make sure the permissions are correct:
chmod 666 /dev/tty
sudo: no tty present and no askpass program specified in github actions
So I went over this with my team, and we figured it out. We updated the sudoers file on the runner machine.
/etc/sudoers
Added in this line:
admin ALL=(ALL) NOPASSWD: ALL
And everything just worked.
shell script giving sudo: no tty present and no askpass program specified when trying to execute sudo command
Try to replace this:
su - devops -c "sh /path/to/myscript.sh"
with this:
sudo -u devops -H sh -c "sh /path/to/myscript.sh"
The -c
option of su
doesn't support interactive mode:
-c, --command COMMAND
Specify a command that will be invoked by
the shell using its -c.The executed command will have no controlling terminal. This option
cannot be used to execute interractive programs which need a
controlling TTY.
(man su
)
By the way, I wouldn't use sudo
within a script everywhere. The script might simply require root
permissions. Within the script you might drop privileges where necessary by means of the above-mentioned sudo
command.
Related Topics
Mount Smb/Cifs Share Within a Docker Container
How to Redirect Output to a File and Stdout
How to Declare 2D Array in Bash
How to Debug the Linux Kernel With Gdb and Qemu
Forcing Bash to Expand Variables in a String Loaded from a File
How to Change 'Rpath' in an Already Compiled Binary
How to Rename All Folders and Files to Lowercase on Linux
Is Gettimeofday() Guaranteed to Be of Microsecond Resolution
Can _Start Be the Thumb Function
Kernel Stack and User Space Stack
Format and Then Convert Txt to CSV Using Shell Script and Awk
Iterate Over a List of Files With Spaces
How to Add a Default Include Path For Gcc in Linux
How to Print a Number in Assembly Nasm
Asynchronous Io Io_Submit Latency in Ubuntu Linux
How to Run a Perl Script as a System Daemon in Linux