How to create a user by using Shell Script on Oracle Linux 8.8
To Create A User By Using Shell Script On Oracle Linux 8.8
Introduction:
A shell script is a text file that contains commands for UNIX-based operating systems. Shell scripting is an essential component of Linux process automation. Scripts are ways to write and execute a sequence of commands.
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.8"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:8:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.8
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.8
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 root]$ su user1
Password:
[user1@linuxhelp root]$
Step 9: Try to Switch user2 by using the below command
[linux@linuxhelp root]$ su user2
Password:
[user2@linuxhelp root]$
Step 10: Go to home location and list by using the below command
[user2@linuxhelp root]$ cd /home
[user2@linuxhelp home]$ ll
total 0
drwx------. 3 linux linux 78 Jul 26 02:17 linux
drwx------. 3 user1 user1 99 Jul 26 02:17 user1
drwx------. 3 user2 user2 99 Jul 26 02:17 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.8. Your feedback is much welcome.