How to Automatically Add User Account and Password with a Bash Script

How to automatically add user account AND password with a Bash script?

You can run the passwd command and send it piped input. So, do something like:

echo thePassword | passwd theUsername --stdin

How to create a new username and password in Bash script?

Nevermind! I got it working!
This is my solution:

useradd $username -d /home/$username -m ;
echo $passwd | passwd $username --stdin;
echo "the account is setup"

How do I get my bash script to assign unique usernames automatically to new users?

Just put another loop around

           i=$(($i+1))
USERNAM="user$i"

to skip over existing users:

          while
i=$(($i+1))
USERNAM="user$i"
id $USERNAM
do :
done

bash shell automatically creating user - adduser

Always quote your variables unless you deliberately want to split the value into separate words.

sudo adduser --gecos "$fullname" --disabled-password --home "$home" "$username"

Also, you have to use double quotes around strings containing variables, not single quotes, if you want the variables to be expanded.

Difference between single and double quotes in Bash

So this line:

echo 'username:$password' | chpasswd

should be:

echo "username:$password" | chpasswd

Automatically insert username and password to run application in shell script

Got it to work by adding -u $UN -p $PWD where $UN and $PWD are the username and password. I added the above text at the and of the line calling the app in the shell script.

How to input automatically when running a shell over SSH?

For general command-line automation, Expect is the classic tool. Or try pexpect if you're more comfortable with Python.

Here's a similar question that suggests using Expect: Use expect in bash script to provide password to SSH command



Related Topics



Leave a reply



Submit