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 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
In my linux machine, path are configured as follows
non-root user:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java
root user:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
when i tried to
sudo echo $PATH
it shows non-root user path only not root path
but when i put
echo $PATH
in script and tried to execute with sudo, it gives root path. Do anyone knows this reason? Actually sudo is for executing command as root but in my first case it is not working fine.
sudo echo $PATH executes echo $PATH as root in the current non-root environment, i.e. with the non-root value of $PATH. When you do sudo bash somescript.sh, bash is executed as root and during its initialization, it loads the root environment containing root's value of $PATH.
When you run sudo echo $PATH, the shell expands $PATH before ever calling sudo, so you are really running this:
sudo echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java
which gives the results you are seeing - the non-root user PATH being displayed.
When echo $PATH is embedded in a script and you do sudo somescript.sh, the shell that is running the script runs as root, and so when it expands $PATH as part of interpreting the script, it sees root's environment and displays the root version of the PATH.
In order to avoid the early expansion in the first case, you could do this:
sudo bash -c 'echo $PATH'
assuming your sudoers is set up to allow that. The single quotes prevent the non-root users shell from expanding the variable before passing it as a command to bash.
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
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 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.