• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

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

  • 00:37 cat /etc/os-release
  • 01:01 vim /tmp/userlist
  • 02:02 vim /usr/sbin/createuser.sh
  • 08:24 chmod -R 775 /usr/sbin/createuser.sh
  • 08:38 sh /usr/sbin/createuser.sh
  • 08:46 cat /etc/passwd
  • 09:14 su user1
  • 09:32 su user2
  • 09:55 cd /home/
7544

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.

Tags:
matthew
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

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

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

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

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 difference between shell and Bash scripting?

A

Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash.

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

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

How to install Xrdp Server (Remote Desktop) on Oracle Linux 8.5
How to install Xrdp Server (Remote Desktop) on Oracle Linux 8.5
Oct 17, 2022
How to install and update OpenSSL on Debian 11.3
How to install and update OpenSSL on Debian 11.3
Oct 21, 2022
How to Install and Configure Mega in Linux
How to Install and Configure Mega in Linux
Jul 19, 2016
How to use Aureport command on Linux
How to use Aureport command on Linux
Nov 28, 2017
How to install Development tools on Linux
How to install Development tools on Linux
Jun 12, 2018
How to Install mod_ssl and SSL certificate on Oracle Linux
How to Install mod_ssl and SSL certificate on Oracle Linux
Dec 30, 2021
How to install Nextcloud on Ubuntu 22.04 version
How to install Nextcloud on Ubuntu 22.04 version
Jun 23, 2023
How to install ClipGrab in Linux
How to install ClipGrab in Linux
Jul 16, 2016

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

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

Linux
jayce class=
shasum command not found
May 5, 2017
Linux
stephan class=
How to list all samba users
Jan 12, 2018
pv command
muhammad class=
pvcreate command not found error
May 9, 2017
Linux
henry class=
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
Apr 25, 2017
ifconfig command
jackbrookes class=
what is the location of the ifconfig program on your machine?
Jan 4, 2018
Linux
baseer class=
single command to apply setfacl for multiple user at a time
Jan 23, 2018
Linux
beulah class=
What does mean by 0 0 value in fstab file
Jan 2, 2018
CentOS
mason class=
Error getting authority: Error initializing authority: Could not connect: No such file or directory (g-io-error-quark, 1)
Nov 20, 2018

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

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

Anbox, the Android-to-Linux tool the developers have been waiting for
Anbox, the Android-to-Linux tool the developers have been waiting for
Apr 17, 2017
Linus Torvalds stops signing Linux kernel RC tarballs
Linus Torvalds stops signing Linux kernel RC tarballs
May 17, 2017
Capsule8 Launches Linux-Based Container Security Platform
Capsule8 Launches Linux-Based Container Security Platform
Feb 14, 2017
Symantec updates Management console product
Symantec updates Management console product
Nov 22, 2017
Latest Linux driver release feature seven AMD Vega
Latest Linux driver release feature seven AMD Vega
Mar 23, 2017
A Newer and a Faster Window Manager for Tina (Linux Mint 19.2)
A Newer and a Faster Window Manager for Tina (Linux Mint 19.2)
Apr 9, 2019
Microsoft makes its Azure App service now available on Linux Systems
Microsoft makes its Azure App service now available on Linux Systems
Sep 7, 2017
Docker friendly Alpine Linux gets hardened Node.js
Docker friendly Alpine Linux gets hardened Node.js
Apr 19, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Isaac ?
How to run windows application in linux

I need to run the windows application in my Linux machine, instead of installing from yum repo or any other repos. How to do that..??

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.