Using the passwd command from within a shell script
from "man 1 passwd
":
--stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.
So in your case
adduser "$1"
echo "$2" | passwd "$1" --stdin
[Update] a few issues were brought up in the comments:
Your passwd
command may not have a --stdin
option: use the chpasswd
utility instead, as suggested by ashawley.
If you use a shell other than bash, "echo" might not be a builtin command,
and the shell will call /bin/echo
. This is insecure because the password
will show up in the process table and can be seen with tools like ps
.
In this case, you should use another scripting language. Here is an example in Perl:
#!/usr/bin/perl -w
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe
passwd command in shell script
Try expect(1)
.
How to provide password to a command that prompts for one in bash?
Take a look at autoexpect
(decent tutorial HERE). It's about as quick-and-dirty as you can get without resorting to trickery.
How to get a password from a shell script without echoing
Here is another way to do it:
#!/bin/bash
# Read Password
echo -n Password:
read -s password
echo
# Run Command
echo $password
The read -s
will turn off echo for you. Just replace the echo
on the last line with the command you want to run.
how to pass a user password ,stored in config file to passwd command
Use chpasswd instead of passwd to set passwords in batch mode.
Example:
sudo chpasswd <<<"$username:$password"
Or, it you're using other shell (not bash of version >=3.0):
echo "$username:$password" | sudo chpasswd
How do I write a password using a bash script?
Assuming there's still no "useradd" on Pi, I'd suggest you start looking into adduser's command line parameters?
adduser <username> -p <password> ...
Useradd script in linux without using passwd
If you prefer to pipe the user's password from STDIN, use chpasswd
utility which is quick and simple.
as suggested by @Ardit.
This script should work for your purpose, assuming you meet the following conditions-
- You are interacting as the root user
- You have an existing group created for the purpose of your new user
#!/bin/bash
echo "Name:"
read name
echo "Password:"
read password
echo "Group:" # group must exist
read group
# add new user, set group, create new home directory
useradd -G $group -m $name
# update new user password by piping from STDIN
echo ""$name":"$password"" | chpasswd
# change the default user shell to bash
chsh -s /bin/bash $name
- First we execute
useradd
command to create the new user and assign it to an existing group. - Then we pipe the name and password into
chpasswd
. If you're wondering why wrap those variable expansions with double quotes, check this answer out . - Finally
chsh
utility is used to update the user shell.
Why not execute everything in a single statement?
I prefer subdividing a problem into smaller tasks for easier understanding.
Related Topics
Asynchronous Io Io_Submit Latency in Ubuntu Linux
Performing Http Requests With Curl (Using Proxy)
Appending a Line to a File Only If It Does Not Already Exist
Sorting Multiple Keys With Unix Sort
Linux Default Behavior of Executable .Data Section Changed Between 5.4 and 5.9
How to Ensure Only One Instance of a Bash Script Is Running
How to Compile a 32-Bit Binary on a 64-Bit Linux Machine With Gcc/Cmake
How to Remove ^[, and All of the Escape Sequences in a File Using Linux Shell Scripting
Bash Script Process Substitution Syntax Error: "(" Unexpected
Recursively Look For Files With a Specific Extension
Kernel Stack and User Space Stack
Convert Xlsx to CSV in Linux With Command Line