24 March 2013
1 min read

Shell Script to Add a User in Linux

In this tutorial, we will show you how to write a shell script to add a user in Linux. In the script, we will use useradd command to add user and groupadd command to add user to group.

Add new user using shell script

The following shell script add a new user which can create username, primary group, home directory and shell. We are using useradd command to create user.

#!/bin/bash while [ x$username = "x" ]; do read -p "Please enter the username you wish to create : " username if id -u $username >/dev/null 2>&1; then echo "User already exists" username="" fi done while [ x$group = "x" ]; do read -p "Please enter the primary group. If group not exist, it will be created : " group if id -g $group >/dev/null 2>&1; then echo "Group exist" else groupadd $group fi done read -p "Please enter bash [/bin/bash] : " bash if [ x"$bash" = "x" ]; then bash="/bin/bash" fi read -p "Please enter homedir [/home/$username] : " homedir if [ x"$homedir" = "x" ]; then homedir="/home/$username" fi read -p "Please confirm [y/n]" confirm if [ "$confirm" = "y" ]; then useradd -g $group -s $bash -d $homedir -m $username fi

Sample Result

sudo ./linux_user.sh Please enter the username you wish to create : test Please enter the primary group. If group not exist, it will be created : test Please enter bash [/bin/bash] : Please enter homedir [/home/test] : Please confirm [y/n]y 22:12:58 [test@Desktop] :~ id test uid=1003(test) gid=1003(test) groups=1003(test)

Learn above script line by line

#Write to console ask to enter group and save input to group variable

read -p “Please enter the primary group. If group not exist, it will be created : ” group

#check if group already exist

if id -g $group >/dev/null 2>&1; then

#just warn that group already exist

echo “Group exist”

else

#if group not exist – create one more

groupadd $group

fi

#end of while loop

done

#ask to enter preferred bash

read -p “Please enter bash [/bin/bash] : ” bash

#check if no input

if [ x”$bash” = “x” ]; then

#if no input, use default bash

bash=”/bin/bash”

fi

#ask to enter preferred homedir

read -p “Please enter homedir [/home/$username] : ” homedir

#check if no input

if [ x”$homedir” = “x” ]; then

#if no input , use default homedir

homedir=”/home/$username”

fi

#ask to confirm all inputs

read -p “Please confirm [y/n]” confirm

#if input y

if [ “$confirm” = “y” ]; then

#command to add user with all entered info

useradd -g $group -s $bash -d $homedir -m $username

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is the editor-in-chief of Linoxide and has an experienced team of Linux enthusiastic authors who makes this blog awesome. Linoxide is one of the top 20 Linux Blog by whizlabs.

Leave a Reply

Your email address will not be published.

Previous Story

Detailed Understanding of Linux Inodes with Example

Next Story

How to List and Delete Iptables Rules

Latest from Blog

Top 8 Reasons to Use Garuda Linux

Have you been going back and forth between multiple Linux flavors in search of an exciting experience? Or perhaps you are coming from a Windows or MAC environment and want to try

How to Rename Multiple Files in Linux

In a Linux system, you can easily rename a file using mv command. But, if you have multiple files which you want to rename, in this situation you need some extra tools

How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered
Go toTop