Question

I have this:

su $username -c ./script.sh

The problem is that within script I have 'sudo' commands and they says me

sudo: no tty present and no askpass program specified

How to do this right?

UPD: I need both sudo and su. What I need to do is run script as USER $username and be able to run certain commands within script as root (for example, pacman -S)

SOLUTION: I've added NOPASSWD option to /etc/sudoers before running script and delete this entry using sed after script finished.

Was it helpful?

Solution

First set chmod +x to your scripts

try:

#!/bin/bash
echo "hello"
su - <your-user> -c /path/to/script.sh
echo "good bye"

UPDATE:

You should find a way to force bash to use pseudo-tty

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.

If the user is not as sudoers do the following steps:

This is what you need to do in /etc/sudoers:

# User privilege specification
root        ALL=(ALL:ALL) ALL
newuser    ALL=(ALL:ALL) ALL

you have also ways to do: you can pipe password if it has password:

echo "yourpassword" | sudo -S

OR

You can run the following script:

#!/usr/bin/expect -f 
spawn sudo -s <<EOF 
expect "assword for username:" 
send -- "user-password\r" 
expect eof

Also you can do that:

sudo -kS bash - << EOF
password
whoami
echo "Not a good idea to have a password encoded in plain text"
EOF
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top