Write script to create multiple users with pre-defined passwords
You have to supply the password on stdin. Replace:
passwd $iuser
with:
passwd "$iuser" <<<"$ipasswd
$ipasswd"
or, as suggested by mklement0:
passwd "$iuser" <<<"$ipasswd"$'\n'"$ipasswd"
The incantation <<<
creates a here-string. The string that follows the <<<
is provided as standard in to the command which precedes the <<<
. In this case we provide the two copies of the password that the passwd
command wants.
(The script reads these passwords from a plain text file. I will assume that your situation is some special case for which this is not as dangerous as it normally would be.)
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
Provide password for running a script inside another script as different user
Yes you have option for that. But that password you have to provide at the time of executing the script or hard coded it in the script itself. Try with the below example:-
echo 'passwordofB' | sudo -u B -S ./script2.sh
Also you can do it like:-
sudo -u A ./script.sh passwordofB #as a command line parameter
now inside script.sh
:-
echo $1 | sudo -u B -S ./script2.sh
You are executing another script sudo -u B ./script2.sh
from script ./script.sh
right? So change that line with echo $1 | sudo -u B -S ./script2.sh
and run your first script as sudo -u A ./script.sh passwordofB
where passwordofB
is the password for user 'B'
How to set a password for all users (Bash Linux)
You could manually change all user accounts in question with the following, it will prompt you for the new password
$ sudo passwd <username>
You could automate this with a script. Or you could use a convoluted command at the command line, which is what I would do. The below example will pull all users from the passwd file, filter out the users that cannot login, and then run a loop to set their password
using cat piped to grep you can get a list of all users and filter out the users with "nologin" or "false" in their config. If you get users that you do not want, change the filter items or add the username to the grep statement to filter them out, separate each filter item with \|
$ cat /etc/passwd | grep -Ev nologin\|false
using awk you can get just the username to print out
$ cat /etc/passwd | grep -Ev nologin\|false | awk '{split($0,a,":");print a[1]}'
running this command in a for loop will let us run a command on each user, to test just echo the username
$ for user in `cat /etc/passwd | grep -Ev nologin\|false | awk '{split($0,a,":");print a[1]}'`; do echo $user; done
the tricky part is passing a password to the passwd command. switch to the root user and then echo the password to the passwd command. Here is an example
$ sudo -i
# (echo 'newpassword'; echo 'newpassword') | passwd <username>however you do not want the password in your command line history. put the password in a tempfile and then cat it to xargs. As an example, just echo the password using xargs
$ sudo -i
# vi tempfile
enter only one line with the new password
# cat tempfile | xargs -i echo {}now you'll use xargs to echo to passwd. this is tricky again because you need to run two echo commands correctly, just tell xargs to run the command in a sub shell
$ sudo -i
# cat tempfile | xargs -i /bin/bash -c "(echo '{}'; echo '{}') | sudo passwd <username>"now add the xargs command in the for loop
$ sudo -i
# for user in `cat /etc/passwd | grep -Ev nologin\|false | awk '{split($0,a,":");print a[1]}'`; do cat tempfile | xargs -i /bin/bash -c "(echo '{}'; echo '{}') | sudo passwd $user"; done
That should be it, let me know if you have questions
I tested this example on ubuntu 20.04 using GNU bash 5.0.17
Related Topics
Laravel-Mix No Build Notification
Cvs Tab Completion for Modules Under Linux
Is There Compatible Odbc Driver with Mariadb 10 on Linux
How to Convert a Text/Plain to Text/X.Shellscript
How to Dynamic Load The Library with Same Name But in Different Directory in Linux
Graphics Card Memory Usage in Linux
How to Send Integer with Message Queue with Posix API in Linux
Libcurl with Libssh2 - One or More Libs Available at Link-Time Are Not Available Run-Time
Why Is Capeff All Zeros in /Proc/$Pid/Status
Overview/Reference Manual for Open Firmware Device Trees
Installing PHPsh on Linux, Python Error
How to Detect Whether Tomcat and Ant Are Installed on Linux Machine
Find Is Returning "Find: .: Permission Denied", But I Am Not Searching In
How to Get Started with Libsandbox