-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateadmin.sh
More file actions
executable file
·30 lines (20 loc) · 871 Bytes
/
createadmin.sh
File metadata and controls
executable file
·30 lines (20 loc) · 871 Bytes
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
#!/bin/bash
# Prompt for the username
read -p "Enter the username: " username
# Prompt for the public key file path
read -p "Enter the public key file path: " public_key_path
# create a new user
useradd -m -s /bin/bash "$username"
# create .ssh directory in the user's home directory
mkdir -p /home/"$username"/.ssh
# copy the public key to the authorized_keys file
cp "$public_key_path" /home/"$username"/.ssh/authorized_keys
# set the permissions for .ssh directory and authorized_keys file
chmod 700 /home/"$username"/.ssh
chmod 600 /home/"$username"/.ssh/authorized_keys
# change the owner of .ssh directory to the new user
chown -R "$username":"$username" /home/"$username"/.ssh
# grant sudo privileges
echo "$username ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/"$username"
# make sure the file is only writable by root
chmod 0440 /etc/sudoers.d/"$username"