I have created a shell script as follows.
username='root'
sudo -H -u "$username" bash 2<&0 << 'END_COMMAND'
useradd -m -s /bin/bash suhail
passwd suhail
mkdir ~/test
END_COMMAND
But i am getting the out put as follows when i trying to run this script file
user#uvais-desktop:~/ssp$ ./test
sudo: unable to resolve host uvais-desktop
Enter new UNIX password: Retype new UNIX password: Sorry, passwords do not match
passwd: Authentication token manipulation error
passwd: password unchanged
It is not prompting for password for the new user.Everything happened in a second. !!
Please help me if anyone could.
One problem is that you can't run passwd. It asks to write password twice which you can't do in the script. So you enter password once then for the second time you enter mkdir ~test, hence the message "Sorry, passwords do not match"
Instead of passwd suhail try:
echo suhail| passwd $username --stdin
Related
This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 1 year ago.
I wrote a short script that would ssh to a bunch of machines on a file called config that would iterate through the machines, ssh through them and create a new user on them. problem is - these commands require sudo privileges, and when I'm trying to execute sudo on them, I get a wrong password error, probably because sudo is not allowed over ssh? I'm not quite sure.
The code is as follows:
#!/bin/bash
read -p "enter remote admin username " adminuser
read -p "choose new username " newuser
read -p "choose new pass " newpass
while read -u10 HOST ; do ssh ${HOST} "uname -a" ;
sudo -S adduser --disabled-password --gecos "" $newuser
sudo -S chpasswd <<<"$newuser:$newpass"
sudo -S chown $newuser /home/$newuser
#sudo -S groupadd group
echo; echo "New user ${newuser} has been created on ${HOST}"
done 10< config.txt
It's worth to note I have set /etc/ssh/sshd_config PermitRootLogin to yes.
While we're at it, is there a way to minimize the amount of times i have to input my admin password? Right now I have to use it when I first ssh into the machine and when I execute a sudo command - so if I have 17 machines that's a minimum of 17 machines. I'd like to minimize that if possible.
Please do not set /etc/ssh/sshd_config PermitRootLogin to yes. No reason to play with fire unless necessary.
On the remote machine, use visudo to define a group like admin that never needs to enter a password in order to use sudo. Here are two lines from my /etc/sudoers file:
# Members of the admin group may gain root privileges
%admin ALL=(ALL) NOPASSWD: ALL
Then add the user id to that Linux user group and the script will run as root without prompts for sudo passwords:
$ usermod -a -G admin my_user_name
I'm writing script to test unix commands. one of them is the SCP command, where the certs are set up , so it did not ask for the password on normal scenario.
scp /tmp/test.txt $userid#$hostname:$path
But assume scenario like certs were deleted or the user id is wrong then the script is not getting completed as scp asks for password
On the terminal
scp /tmp/test.txt $userid#$hostname:$path
Password:
So how can i force not to prompt for password and just fail in case it can not validate.
The scp command was stored in a variable line and is executed as below
`$line`
Typically:
scp -o BatchMode=yes ..
Taken from the scp man pages:
-B Selects batch mode (prevents asking for passwords or
passphrases).
I am trying to create linux users using a script . The username and corresponding password are stored in a config file.
config file is given below
username="user1"
password="passxxx"
using a shell script i am creating linux users with username and password provided in config file.
if [ -n "$username" ] then
password="$line"
sudo adduser $username
sudo passwd $username
Here i have to give the password in the config file. usually we have to type the required password twice to update the password information . Is there any way to update the password without typing ( use the password provided in config file) .
Use chpasswd instead of passwd to set passwords in batch mode.
Example:
sudo chpasswd <<<"$username:$password"
Or, it you're using other shell (not bash of version >=3.0):
echo "$username:$password" | sudo chpasswd
I am attempting to use sudo check to see if the password for the user is set to a standard password.
I have read that you can use the -S option to receive input from stdin
-S The -S (stdin) option causes sudo to read the password from
the standard input instead of the terminal device.
but when i run echo 'password' | sudo -S -u user command
it returns:
Password:
Sorry, try again.
Password:
sudo: 1 incorrect password attempt
Could somebody tell me what is off with this script?
Note: I have tried other methods to check the password but I am having a hard time finding a solution because I do not have: root access, a c compiler, or the availability to install programs like expect
Does your password end in a newline? Try:
printf password
or
echo -n password
Sudo is not expecting the password of the specified user, rather the password of the user calling "sudo":
http://en.wikipedia.org/wiki/Sudo
I am finding myself with the issue of needing to execute the postgres createuser.exe from a batch script and cannot get it to stop prompting me with the following:
Enter name of role to add:
my batch script looks like this:
echo calling createuser!
createuser username %super_user% -s -U Super_Postgres s -q
Where %super_user% is a command line argument. Any help would be greatly appreciated, this is the documentation that I am referring too: postgres
"username" should go at the end, after the options. You have it as the first parameter.
from the documentation http://www.postgresql.org/docs/8.4/static/app-createuser.html
The following is from the documentation listed above
To create the same user joe using the server on host eden, port 5000, avoiding the prompts and taking a look at the underlying command:
$ createuser -h eden -p 5000 -S -D -R -e joe
CREATE ROLE joe NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;
To create the user joe as a superuser, and assign a password immediately:
$ createuser -P -s -e joe
Enter password for new role: xyzzy
Enter it again: xyzzy
CREATE ROLE joe PASSWORD 'md5b5f5ba1a423792b526f799ae4eb3d59e' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;