-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinux_User_Sudo_Setup.sh
More file actions
41 lines (34 loc) · 1.15 KB
/
Linux_User_Sudo_Setup.sh
File metadata and controls
41 lines (34 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
# Author: Mukund
# A script to create a new user, set a password, configure password expiration, and add the user to the sudoers file for sudo privileges
# Set username and password
username=user1
passwd=user@123
#### Check if user is already in sudoers file ####
if sudo cat /etc/sudoers | grep $username; then
echo "useradd: user '$username' already exists"
exit 1
fi
#### Create user, set password, and set expiration ####
sudo useradd $username
echo "$username:$passwd" | sudo chpasswd
sudo chage -M 1 $username
echo "User $username created with password expiry in 1 day."
#### Check if username is empty ####
if [ -z "$username" ]; then
echo "usage: $0 <$username>"
exit 1
fi
#### Verify user exists ####
if ! id "$username" &>/dev/null; then
echo "User $username does not exist. Please create the user first."
exit 1
fi
#### Add user to sudoers file ####
echo "$username ALL=(ALL) ALL" | sudo tee -a /etc/sudoers
#### Verify if user was added to sudoers ####
if sudo cat /etc/sudoers | grep -q $username; then
echo "User $username successfully added to sudoers."
else
echo "Failed to add user $username to sudoers."
fi