Skip to content

Commit

Permalink
login improved & some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
BalliAsghar authored Sep 18, 2022
1 parent dd6f6e3 commit 0b195fd
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions createuser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ if [ $(id -u) -ne 0 ]; then
exit 1
fi

echo "Enter User Information"

# ask for username
read -p "Enter username: " username
read -p "🙍 username: " username

# check if username is empty
if [ -z "$username" ]; then
Expand All @@ -25,42 +27,53 @@ if id "$username" >/dev/null 2>&1; then
fi

# ask for password
read -s -p "Enter password: " password
password=`systemd-ask-password "Enter password:"`

# check if password is empty
if [ -z "$password" ]; then
echo "Password cannot be empty."
exit 1
fi

# Ask for public key
read -p "🔑 Enter public key (from you local machine): " publickey

# check if public key is empty
if [ -z "$publickey" ]; then
echo "Public key cannot be empty."
exit 1
fi

# validate public key
if ! echo "$publickey" | grep -q "ssh-rsa"; then
echo "Invalid public key"
exit 1
fi

# create user
useradd -m -s /bin/bash $username

# set password
# # set password
echo "$username:$password" | chpasswd

# add user to sudo group
# # add user to sudo group
usermod -aG sudo $username

# create .ssh directory
# # create .ssh directory
mkdir /home/$username/.ssh

# change ownership of .ssh directory
# # change ownership of .ssh directory
chown $username:$username /home/$username/.ssh

# Ask for public key
read -p "Enter public key: " publickey

# check if public key is empty
if [ -z "$publickey" ]; then
echo "Public key cannot be empty."
exit 1
fi

# add public key to authorized_keys
# # add public key to authorized_keys
echo $publickey >> /home/$username/.ssh/authorized_keys

# change ownership of authorized_keys
# # change ownership of authorized_keys
chown $username:$username /home/$username/.ssh/authorized_keys

echo "User created successfully."
echo -e "\e[32mUser created successfully.\e[0m"

# ip address of the server
ipaddress=`hostname -I | awk '{print $1}'`

echo -e "\e[33mLogin with: ssh $username@$ipaddress\e[0m"

0 comments on commit 0b195fd

Please sign in to comment.