Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
Related
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
I have written a bash script that goes to each machine and runs set of command. I am using the user iis that has sudo privlidges on that machine. However, when i run the sudo yum command i get sudo: sorry, you must have a tty to run sudo.
Not sure what is wrong ? I am using the -t command to force but it would still not work
bash script
#!/bin/bash
INPUT=ccidetails.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read privateip password
do
echo $privateip
scp /home/Data/Test.c iis#$privateip:/tmp
sshpass -p$password </dev/null ssh -t -o "StrictHostKeyChecking no" iis#$privateip "
hostname
cd /tmp
gcc Test.c -o TEST
./TEST
sudo yum -y update glibc
gcc Test.c -o TEST
./TEST
exit
" >> output.txt
done < $INPUT
IFS=$OLDIFS
Error:
sudo: sorry, you must have a tty to run sudo
On remote host comment the line below on /etc/sudoers:
grep tty /etc/sudoers
#Defaults requiretty
That will allow you to continue. Make sure you understand the consequences of doing so:
man sudoers | grep -i requiretty -A 5
requiretty If set, 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(8) or cgi-bin scripts. This flag is off by
default.
If you don't want disable requiretty globally you can disable it for a specific user:
example:
Defaults requiretty
Defaults:your_username_goes_here !requiretty
While this isn't a question for SO, you're almost there ... what you need to change is the number of -t above ... try this:
sshpass -p$password </dev/null ssh -ttt -o "StrictHostKeyChecking no" iis#$privateip "
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to use "swapon -s" remotely but getting "command not found"
$ ssh ns2 swapon -s
bash: swapon: command not found
Using it locally works perfectly, what could be the reason for this?
There are several possible reasons:
You're not root at the remote side. Check this with who am i or id. To make sure you're root, use ssh root#ns2 ...
Your path is wrong. This is often a problem with sudo. Try ssh ns2 sudo /sbin/swapon
Try to ran
which swapon
If it returns something like
no swapon in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:...)
it means that swapon script isn't found anywhere
Maybe you're not in the sudoers file or you need to enter a password.
Be sure that you're really root so try try this and look if it differs:
ssh server.tld id
ssh server.tld sudo id
The second one should give you an output which should be this:
uid=0(root) gid=0(root) groups=0(root)
If this is not the output check your /etc/suders which should have an entry like this:
foo ALL=NOPASSWD: ALL
The command I runned and which worked:
ssh -l foo server.tld sudo swapon -s
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm a linux noob and I'm stuck on a small detail of a class assignment.
I meant to do this from a root shell:
useradd myname -g sudo -p 'openssl passwd -crypt abc123'
To create a sudoer account for me.
Messed it up the first time, so now it says the user already exists.
I want to make sure I'm in the sudoers group and that I know my password to SSH in.
The passwd command can't be used, nor the adduser command.
I know the useradd command is available, but not sure if I can use that somehow...
Ideas?
Edit: And how could I double-check that it worked?
Edit2: I don't have access to an editor like nano or vim :/
Why not just examine (or edit, given the required powers) the /etc/passwd, /etc/shadow, /etc/group and /etc/sudoers files?
Just about everything to do with standard security can be found there
To double check if it works, simply log in from another terminal and try.
Found a work-around:
userdel myname
useradd -m -g sudo -p `openssl passwd -1 abc123` myname
For some reason, I'm able to use sudo but am not in /etc/sudoers
#paxdiablo, thank you for the help!
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Specifically, what commands do I run from the terminal?
Without a home directory
sudo useradd myuser
With home directory
sudo useradd -m myuser
Then set the password
sudo passwd myuser
Then set the shell
sudo usermod -s /bin/bash myuser
Here's the command I almost always use (adding user kevin):
useradd -d /home/kevin -s /bin/bash -m kevin
There's basicly 2 commands to do this...
useradd
adduser (which is a frendlier front end to useradd)
You have to run them has root.
Just read their manuals to find out how to use them.