How to create a user by using Shell Script on Oracle Linux 8.5

To Create A User By Using Shell Script On Oracle Linux 8.5

Introduction:

Shell scripts are text files that contain commands for UNIX-based operating systems.

In Linux, shell scripting is an essential part of process automation. A script is a way to write a sequence of commands and then execute them.

Introduction Procedure:

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

[root@linuxhelp ~]$ cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.5"

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

[root@linuxhelp ~]$ vim /tmp/userlist
user1
user2

Step 3: Creating a script file to write the script by using the below command.

[root@linuxhelp ]# 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.

[root@linuxhelp ~]# chmod 775 /usr/sbin/createuser.sh

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

[root@linuxhelp ~]# 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@linuxhelp ~]$ cat /etc/passwd
user1:x:1001:1001::/home/user1:/bin/bash

user2:x:1002:1002::/home/user2:/bin/bash

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

[root@linuxhelp ~exit

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

[linux@linuxhelp ~]$ su user1
Password: 
[user1@linuxhelp linux]$

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

[linux@linuxhelp ~]$ su user2
Password: 
[user2@linuxhelp linux]$ 

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

[user2@linuxhelp linux]$ cd /home
[user2@linuxhelp home]$ ls
linux  user1  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 oracle linux 8.5. 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