How to Create a User by using shell script on rocky Linux 8.6

To Create a User by using shell script on rocky Linux 8.6

Introduction:

A Shell Script is a text file containing UNIX-based commands. In Linux, shell scripting plays a significant role in process automation. Scripting allows you to write a sequence of commands and then execute them.

Installation Procedure:

Step 1: Check the Oracle Linux Version by using the below command

[root@localhost ~]# cat /etc/os-release 
NAME="Rocky Linux"
VERSION="8.6 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.6"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.6 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

Step 2: Create a text file userlist with all the usernames in it by using the below command

[root@localhost ~]# vim /tmp/userlist

Step 3: creating a .sh file to write the script in it by using the below command

[root@localhost ~]# vim /usr/sbin/createuser.sh
#execute the Script using a bash shell
#!/bin/bash
#//location of the txt file of usernames
userfile=/tmp/userlist

#extracting usernames from the file one-by-one
username=$(cat /tmp/userlist | tr 'A-Z'  'a-z')

#defining the default password
password=Admin@123

#running loop  to add users
for user in $username
do
       #//adding users '$user' is a variable that changes
      #// usernames accordingly in txt file.
       useradd $user
       echo $password | passwd --stdin $user
done

echo "$(wc -l /tmp/userlist) users have been created"
tail -n$(wc -l /tmp userlist) /etc/passwd
~

Step 4: Give permissions to the script file by using the below command

ot@localhost ~]# chmod -R 775 /usr/sbin/createuser.sh

Step 5: Run the Script file by using the below command

[root@localhost ~]# sh /usr/sbin/createuser.sh

Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
2 /tmp/userlist users have been created
wc: /tmp: Is a directory
wc: userlist: No such file or directory
/usr/sbin/createuser.sh: line 24: /root: Is a directory

Step 6: Check whether the user have been created by using the below command

[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin 
tcpdump:x:72:72::/:/sbin/nologin
linuxhelp:x:1000:1000:linuxhelp:/home/linuxhelp:/bin/bash
mongooseim:x:1001:1001::/home/mongooseim:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
user2:x:1003:1003::/home/user2:/bin/bash

Step 7: Exit from root user by using the below command

[root@localhost ~]# exit
Exit

Step 8: Try to Switch user1 by using the below command

[linuxhelp@localhost sbin]$ su user1
Password: 
[user1@localhost sbin]$

Step 9: Try to Switch user2 by using the below command

[user1@localhost sbin]$ su user2
Password: 
[user2@localhost sbin]$

Step 10: Go to home location and list by using the below command

[user2@localhost sbin]$ cd /home/
[user2@localhost home]$ ls -la%%
drwx------. 15 linuxhelp  linuxhelp  4096 Sep 18 20:20 linuxhelp
drwx------.  4 mongooseim mongooseim  114 Sep 12 02:12 mongooseim
drwx------.  3 user1      user1        98 Sep 18 20:22 user1
drwx------.  3 user2      user2        98 Sep 18 20:22 user2

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create user by using shell script on rocky linux 8.6. Your feedback is much welcome.

FAQ
Q
What is the difference between shell and Bash scripting?
A
Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash.
Q
What is the first line of the shell script?
A
This is the standard location of the Bourne shell on just about every Unix system. Adding #!/bin/bash as the first line of your script tells the OS to invoke the specified shell to execute the commands that follow in the script. #! is often referred to as a “hash-bang”, “she-bang” or “sha-bang”
Q
What is the role of the shell in Linux?
A
It provides an interface between the user and the kernel and executes programs called commands
Q
Which language is used in a shell script?
A
Common scripting languages include Shell scripts - sh, bash, csh, tcsh. Other scripting languages - TCL, Perl, Python.
Q
How shell scripting is useful?
A
Using a shell script is most useful for repetitive tasks that may be time-consuming to execute by typing one line at a time