Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create user using Bash [closed]

Tags:

linux

bash

unix

How can I create a user in linux using a bash script already knowing the username and password.

Lets say the username will be variable $username and password will be $password.

I also dont want it to print anything. I simply want to run a .sh file and have it create the user with a static variable I defined.

I am having issues trying to accomplish this. Also I assume I need to encrypt the text version of my password in sha-512

I tried this below no luck

sudo useradd -m -s /bin/bash -p $password $username

Thanks!

like image 647
soniccool Avatar asked Jan 31 '26 01:01

soniccool


1 Answers

You haven't told us what exactly is going wrong but useradd requires a crypted (or otherwise possibly) password. It might be better to just let passwd do the work for you.

sudo useradd -m -s /bin/bash "$username"
echo "$password" | sudo passwd --stdin "$username"
like image 125
Etan Reisner Avatar answered Feb 01 '26 17:02

Etan Reisner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!