Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 332  / 1 Year ago, sat, january 21, 2023, 11:25:29

I need to create a shell script that will create a user then put it into a group. How do I do it?


More From » bash

 Answers
7

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

[#21556] Monday, January 23, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
humbire

Total Points: 55
Total Questions: 93
Total Answers: 113

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
humbire questions
Thu, May 13, 21, 02:28, 3 Years ago
Sun, Oct 24, 21, 14:23, 3 Years ago
Wed, May 17, 23, 22:16, 1 Year ago
;