Send passwort to passwd automatically - linux

I have a script, which is installing a guest machine on a xen server.
It installs automatically.
But in one step, I'll be asked for a password.
The following happens:
Enter new UNIX password: Retype new UNIX password: passwd: Authentication token manipulation error
How can I send the password "1234" at this time?
Maybe with expect and send?
Here is the command, which executes passwd:
chroot /mnt/vms/install /usr/bin/passwd root
It's an internal server, so it must not be very secure.

I have used "chpasswd" command in script, after useradd, to do that. For example
useradd -m -s /bin/bash -u 1001 -g 1003 -G sudo newuser || exit 1
echo newuser:newuserpassword | chpasswd || exit 1
Refer to debianadmin or man page.

Related

How to add Multiple user acccount with check and SSH key using Bash script

Based on my requirement in my dev server, I have Created a Linux function that will create a dev user,(Take the user's first name and create a Linux user )
we have a devs group in our developer server, and now want to create a Linux function for 10 plus developers (user)
as a requirement If any of the '$1' directories exist, it would throw a notice to the executor. our developer will have their own key my plan was not to copy the key. we could pass the key in when this is executed, or have this generate the key and output it on completion.
I have tried this code:
#!/bin/bash
CREATE_USER_NAME="pi"
# Create account
echo "============= now create an account ============="
sudo useradd -s /usr/bin/fish -m $CREATE_USER_NAME
sudo usermod -aG devs $CREATE_USER_NAME
sudo passwd $CREATE_USER_NAME
if getent passwd "$1" > /dev/null 2>&1; then
echo "yes the user exists"
else
echo "No, the user does not exist"
fi
mkdir -p /home/santosh/.ssh
chmod 0700 /home/santosh/.ssh
touch /home/santosh/.ssh/authorized_keys
chmod 600 /home/santosh/.ssh/authorized_keys
sudo mkdir -p /var/www/html/santosh-dev.jove.com/
sudo chown -R santosh /var/www/html/santosh-dev.jove.com/
sudo mkdir -p /var/log/httpd/santosh-dev.jove.com/
sudo chown -R santosh /var/log/httpd/santosh-dev.jove.com/
#
Output I Received:
[santosh#skb Desktop]$ ./user1.sh
============= now create an account =============
[sudo] password for santosh:
useradd: user 'pi' already exists
Changing password for user pi.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
No, the user does not exist
what is the best advice here! I'm unable to figure it what is wrong here? can anyone guide me on it!

How to automatically pass my password in multiple password prompts in command line?

I'm using ubuntu and i want to pass my password in multiple password prompts in single command only. I know how to deal with single password prompt.
For example:
echo password | sudo -S MyCommand;
But when i'm runing this command then it will ask for new password and confirm password:
echo password | sudo -S htpasswd -m /home/user1/svn.passwd UserName
Q. How to respond to multiple password prompts in single command just like we respond to single sudo password prompt?

Remote execute bash scripts that needs input

I am using SSH command to execute the bash scripts remotely:
ssh user#server 'bash -s' < $script_dir/script.sh
And inside the script.sh, I will have the command like below:
ssh-keygen -t rsa
ssh-copy-id postgres#$sqlserver
ssh postgres#$sqlserver -C true
And also
printf "Creating user in postgresql server...\n"
createuser -s -P username
Which need user's input, but I found when I execute the command from the remote server, it will skip getting the users' input and failed.
Another one is:
printf "Please enter your barman server name: \n" ; read -s barmanserver
Which cannot read user's input neither
I know that the script seems cannot read the other terminal's input, but can anyone help me find a solution if I need the user input to continue?
Thanks a lot!!
Eva
I have used something like this in the past. I am not quite sure why I installed sshpass though.
apt-get install sshpass -y
echo "Adding users to new VMs"
adduser adminuser
echo "changing user password"
echo "adminuser:password" | chpasswd
adduser adminuser sudo
It does work, but it gives you some warning.

Linux shell script - How to switch user and run a script?

I'm currently writing a .sh script to deploy different applications on 4 different machines. Right now I'm having trouble with running a script as another user. I need to log in with myUser with my credentials and then sudo su to user2 to run a specific script.
Normally, I would manually do the following:
ssh myUser#remotehost
[Type in password]
sudo su - user2
cd /path/only/accessible/to/user2
./someScript.sh
when I tried
ssh -t myUser#$remotehost "sudo su - user2 && /path/only/accessible/to/user2 && ./someScript.sh"
I was asked my password, then stayed logged as user2, without any feedback from the script, which would normally give me some informations.
What am I doing wrong?
Try
ssh -t myUser#$remotehost "sudo -u user2 /path/only/accessible/to/user2/someScript.sh"
If you need shell access after that you can use
ssh -t myUser#$remotehost "sudo -u user2 /path/only/accessible/to/user2/someScript.sh && /bin/bash -l"
An update if anyone wonders about this.
What I finally did was to log in with an ssh key. My sysadmin had to get involved in order to set it up, but at least it is a viable option.
ssh -i /path/to/sshKey user2#$remoteHost "/path/only/accessible/to/user2/someScript.sh"

Switch user with sudo su command without asking password or sending password as argument

I need to switch to another user with sudo su -user command but I have problems with that because it was asking me password so I type something like
echo mypass | sudo -S su user and it doesn't work also.
How can I switch user without asking me password or can I send somehow password like su user -p password?

Resources