Set password of new user in bash script on RedHat - bash

#! /bin/bash
C_PASS="12345"
echo "enter user name which u want to create"
read UNAME
UPASS="987654321"
echo $C_PASS | sudo -S -k useradd $UNAME
echo "$UPASS" | echo "$C_PASS" | sudo -S -k passwd --stdin "$UNAME"
i got error like:
[sudo] password for admin: Changing password for user zzzak.
passwd: Authentication token manipulation error
plzz help me.

Related

sudo -S command does not recognize my password

I am trying to create a python script using sudo with echo, but the terminal always recognize my password as a command.
echo főzőedény|sudo -S su
echo "főzőedény"|sudo -S su
echo főzőedény|sudo -S -k su
[sudo] password for molnar: %
echo "főzőedény"|sudo -S -k su
[sudo] password for molnar:
Every time I got the same error message: zsh: command not found: főzőedény

Execute commands as different user via sudo over SSH in a justfile

I have this justfile:
remote:
#!/usr/bin/env bash
read -p 'Password:' -s password
ssh -tt somewhere 'bash -l -s' << 'ENDSSH'
whoami
echo "$password" | sudo su someone 'bash -l -s' << 'ENDSUDO'
whoami
ENDSUDO
ENDSSH
It should:
Ask me for a password
SSH into somewhere
sudo to change the user
execute some scripts
What it does:
It asks for a password a second time.
It stucks on input (no error message).
How to solve this problem?
Update
As suggested by #xhienne, this does almost work, but it says, I use the wrong password:
remote:
#!/usr/bin/env bash
read -p 'Password:' -s password
ssh -tt somewhere 'bash -l -s' << 'ENDSSH'
sudo -S -i -u someone << ENDSUDO
$password
whoami
ENDSUDO
exit
ENDSSH
But this does work:
remote:
#!/usr/bin/env bash
read -p 'Password:' -s password
ssh -tt somewhere 'bash -l -s' << 'ENDSSH'
sudo -S -i -u someone << ENDSUDO
clear-text-password
whoami
ENDSUDO
exit
ENDSSH
Update 2
The answer of #xhienne does work.
With
echo "$password" | sudo su someone 'bash -l -s' << 'ENDSUDO'
whoami
ENDSUDO
You are redirecting stdin twice:
once with |
a second time with <<
Try this:
sudo -S -i -u someone << ENDSUDO
$password
whoami
ENDSUDO
sudo -S will read the password from stdin. sudo -i is a substitute for the ugly sudo su bash -l (but it needs that sudo be properly configured for -u someone)
Note that I removed the quotes around ENDSUDO. Beware of inadvertent substitutions. If you must keep ENDSUDO quoted, then you can try this instead:
{
echo "$password"
cat << 'ENDSUDO'
whoami
ENDSUDO
} | sudo -S -i -u someone
I believe the following will work, if you only want to run whoami instead of several commands:
#!/usr/bin/env bash
read -s -p 'Password: ' password
ssh somewhere whoami
echo "$password" | ssh somewhere sudo -S -u someone whoami
The -S tells sudo to read the password from stdin.
If you want to run several commands with a here-document, see #xhienne's answer.

passwd: unrecognized option '--stdin' error on Debian when I run my created Bash Script

I am using Debian. I am learning Bash scripting. I am creating a script that creates new user and sets password the problem is I get passwd: unrecognized option '--stdin' error
That is my script:
#!/bin/bash
read -p "Please Enter Your Real Name: " REAL_NAME
read -p "Please Enter Your User Name: " USER_NAME
useradd -c "${COMMENT}" -m ${USER_NAME}
read -p "Please Enter Your Password: " PASSWORD
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
passwd -e ${USER_NAME}
As tested on Debian 10 # Docker image
echo -e "badpass\nbadpass" | passwd urname
This will work in /bin/bash, not in /bin/sh
echo "urname:badpass" | chpasswd
This will work in /bin/sh and /bin/bash
There is no --stdin option, and you need to protect your variable with quotes.
This is a working version:
#!/bin/bash
read -p "Please Enter Your Real Name: " REAL_NAME
read -p "Please Enter Your User Name: " USER_NAME
useradd -c "${COMMENT}" -m ${USER_NAME}
read -p "Please Enter Your Password: " PASSWORD
echo -e "$PASSWORD\n$PASSWORD" |passwd "$USER_NAME"
passwd -e ${USER_NAME}

How to enter sudo password from command line?

I want to enter sudo password from command line .
but i am facing problem in the script .
#!/bin/sh
ssh -tt server_name<<'EOSSH'
sudo su - user
cd /move-to-path/
echo "done"
EOSSH
exit
export password as environment variable
export PASS=yourpassword
echo $PASS
now you can use it in script like:
echo $PASS | sudo -S su
echo $PASS | sudo -S apt-get install update
the password will be passed to sudo su command via -S flag

sudo and chpasswd on one line

I need connect to server via ssh and change users' passwords without typing ssh or sudo password.
I found some example but I cannot complete one command.
I found this command for ssh connection without typing password and running sudo command for delete user:
sshpass -p $admin_password ssh -t $admin#$server "echo $admin_password | sudo -S /usr/sbin/userdel -r $usr"
then I found command for change users password:
echo "$usr:$password" | sudo -S /usr/sbin/chpasswd;
Finaly, I want something like this:
sshpass -p $admin_password ssh -t $admin#$server "echo $admin_password | sudo -S echo "$usr:$password" | sudo -S /usr/sbin/chpasswd;"
Do you have any ideas?
Your question is actually going to require 2 things. The command to change a password remotely for a user:
ssh remoteserver 'echo -e "passwdofuser\npasswdofuser" | passwd username'
In order to do that you probably need to be root, or in the sudo config, have "passwd" NOT require a password for your user to run.
visudo
Edit the file:
Cmnd_Alias MYPASSWD = /usr/bin/passwd
yourusername ALL = NOPASSWD: MYPASSWD
That should allow you to ssh in and not have to use a password to run the passwd command.

Resources