JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Bash Script to Add New User in Linux

Author:JIYIK Last Updated:2025/03/22 Views:

This short article is about creating a Bash script that automates adding users and assigning passwords for Linux operating system. In Linux operating system, useradd command is used to add new users and provide passwords to them.


Bash script useradd command

A simple utility for adding users is useradd. Debian administrators should usually use adduser instead.

Invoking the useradd command without the -D option creates a new user account based on the values ​​entered on the command line and the system defaults. The useradd command updates system files and may perform other tasks depending on the command line options.

Also, create a home directory for the new user and copy the initial files.

Bash script for adding users

In Linux operating system, we need to be the root user to add new users to the system. Therefore, in the script, we first need to verify whether the user running the script is the root user.

After that, we can get the input username and password from the user and then run useraddthe command. Let's take a look at the following script.

Bash script:

#!/bin/bash

if [ $(id -u) -eq 0 ]; then
    read -p "Enter your  username : " user_name
    read -s -p "Enter your password : " pass
    egrep "^$user_name" /etc/passwd >/dev/null
    if [ $? -eq 0 ]; then
        echo "$user_name exists!"
        exit 1
    else
        epass=$(perl -e 'print crypt($ARGV[0], "pass")' $pass)
        useradd -m -p "$epass" "$user_name"
        [ $? -eq 0 ] && echo "Successfully added User!" || echo "Sorry! User not added"
    fi
else
    echo "Sorry! You are not authorized to add users."
    exit 2
fi

After reading the username and password from the user, we first egrepsearch if the user with the given username already exists using the command. If the user entry is found in the /etc/passwd file, it means that the user already exists.

Otherwise, we will encrypt the password using perl command. The perl command will display the screen with the encrypted password.

The function in Perl crypt()uses a one-way encryption algorithm. Once the password is encrypted, it cannot be decrypted. Get the user's password string, encrypt it with salt, and then display it on the computer screen.

After running the useradd command, we will use the $? value and display an appropriate message to the user.

Output:

Output of the bash script for the useradd command

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Sort data based on the second column of a file in Bash

Publish Date:2025/03/22 Views:87 Category:OPERATING SYSTEM

This article explains how to sort data based on the second column of a file in bash. Overview of the Sort Command in Bash Sort files using the sort command, which places records in a specific order. By default, the sort command sorts files

Check if a command exists in Bash

Publish Date:2025/03/22 Views:50 Category:OPERATING SYSTEM

You may need to verify the existence of a command, program, or file using Bash scripting or programming. This article will take a look at several methods to meet this need. We can use different built-in commands in Bash to check if a comman

Including a script file in another Bash script

Publish Date:2025/03/22 Views:104 Category:OPERATING SYSTEM

This article discusses different ways to include a Bash script file into another script file. Including files in Bash scripts Including or reusing scripts in Bash is very simple. The source keyword is similar to that in C/C++ #include . To

eval command in bash script

Publish Date:2025/03/21 Views:88 Category:OPERATING SYSTEM

This article is about using strings as commands in Bash scripts. For this purpose, the eval command is used. Eval Command in Bash Scripts In some Bash scripts, you have to create a string using variables or input values ​​(for example)

Exit Bash Script

Publish Date:2025/03/21 Views:99 Category:OPERATING SYSTEM

This article provides a brief introduction to Bash scripting and discusses exiting a Bash script when an error occurs. It further discusses the limitations and benefits of Bash scripting. What is Bash Scripting A computer script/program tel

Echo Tab Character in Bash Script

Publish Date:2025/03/21 Views:66 Category:OPERATING SYSTEM

This article explains how to echo one or more tab characters when using Bash scripts. Echo Tab Character in Bash Script echo The command is simple: it prints whatever is passed to the terminal. Normally, if you save a variable as: example=

Bash 脚本中的 eval 命令

Publish Date:2023/06/11 Views:392 Category:操作系统

本文是关于在 Bash 脚本中使用字符串作为命令的。 为此,使用了 eval 命令。Bash 脚本中的 Eval 命令 在某些 Bash 脚本中,您必须使用变量或输入值(例如)创建一个字符串,并在最后将其作为命

退出 Bash 脚本

Publish Date:2023/06/11 Views:269 Category:操作系统

本文简要介绍 Bash 脚本,并讨论在出现错误时退出 Bash 脚本。 它进一步讨论了 Bash 脚本的局限性和好处。什么是 Bash 脚本 计算机脚本/程序告诉计算机做什么和说什么。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial