I need to create a shell script that will create a user then put it into a group. How do I do it?
Asked
Active
Viewed 292 times
2 Answers
2
create a file and put smth like
#!/bin/bash
useradd "$1" && gpasswd -a "$1" group
in there.
Frederick Nord
- 589
2
Your question is a bit vague because you haven't really said what you need this for.
I'm assuming you need arguments as well so first create the script, so in the terminal type nano scriptname.sh and past in the script below.
#!/bin/bash
#$1 username
#$2 home directory
useradd $1 -U -m -d $2
If you type 'man useradd' you will see that -U creates a group with the same name as the user, -m creates the home directory and -d specifics the path to the home directory.
To use this script type chmod +x scriptname.sh to make the script executable and then type ./scriptname.sh
So for example to create a user called bob with the users home being /home/bob type:
./scriptname.sh bob /home/bob
FortuneCookie101
- 325
-
It says permission denied how do i gain permission? – Helen Schofield Jan 30 '15 at 09:33
-
@HelenSchofield you have to run as sudo so "sudo ./scriptname.sh bob /home/bob" – FortuneCookie101 Jan 30 '15 at 13:22
-
How do I edit it so that the user has a password and gets put in a group? – Helen Schofield Feb 09 '15 at 15:06