Linux Users
sudo adduser username | Create a new user |
sudo userdel username | Delete a user |
sudo usermod -aG groupname username | Add a user to group |
sudo deluser username groupname | Remove a user from a group |
Create multiple users ( it requires a separate list for user names and keys )
create_user.sh
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
version=$(cat /etc/os-release | head -1)
number=0
for user in $(cat ${1} | awk '{print $1}'); do
((number+=1))
key=$(cat ${1} | sed $number'q;d' | awk '{print $2 " " $3 " " $4}')
# This is for Linux-instances
if [ "$version" = 'NAME="Amazon Linux"' ];then
adduser $user
mkdir /home/$user/.ssh
echo $key > /home/$user/.ssh/authorized_keys
chown -R $user:$user /home/$user/.ssh
chmod 700 /home/$user/.ssh
chmod 600 /home/$user/.ssh/authorized_keys
fi
# This is for ubuntu-instances
if [ "$version" = 'NAME="Ubuntu"' ];then
adduser --disabled-password --gecos "" $user
mkdir /home/$user/.ssh
echo $key > /home/$user/.ssh/authorized_keys
chown -R $user:$user /home/$user/.ssh
chmod 700 /home/$user/.ssh
chmod 600 /home/$user/.ssh/authorized_keys
fi
echo "Do you want to $user become sudo?"
echo "Type yes or no"
read sudo
if [ "$sudo" = 'yes' ] || [ "$sudo" = 'no' ];then
echo "You selected $sudo."
else
echo "You type incorrectly. Abort"
exit 1
fi
if [ "$sudo" = 'yes' ];then
echo
# This is for Linux-instances
if [ "$version" = 'NAME="Amazon Linux"' ];then
usermod -aG wheel $user
sudoers=$(tail -1 /etc/sudoers)
if [ "$sudoers" = '#includedir /etc/sudoers.d' ] || [ "$sudoers" = 'Defaults env_keep += "SSH_CLIENT"' ];then
sudo sh -c "echo \"%wheel ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers"
fi
fi
# This is for ubuntu-instances
if [ "$version" = 'NAME="Ubuntu"' ];then
usermod -aG sudo $user
sudoers=$(tail -1 /etc/sudoers)
if [ "$sudoers" = '#includedir /etc/sudoers.d' ] || [ "$sudoers" = 'Defaults env_keep += "SSH_CLIENT"' ];then
sudo sh -c "echo \"%sudo ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers"
fi
fi
echo "$user is now sudo"
echo
else
echo "Skipping"
echo
fi
done
user_list
1
This post is licensed under CC BY 4.0 by the author.
Comments powered by Disqus.