Last active
January 2, 2022 00:57
-
-
Save khustochka/33f8a1c0df142eae654a08c140319dae to your computer and use it in GitHub Desktop.
Create you own user on AWS Ubuntu or Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
username=$1 | |
new_uid=$2 | |
new_groups=$3 | |
while [ -z "$username" ] | |
do | |
read -p "Enter username []: " username | |
done | |
if id "$username" &>/dev/null; then | |
echo "User \`$username\` exists." | |
else | |
if [ -z "$new_uid" ]; then | |
read -p "Enter UID []: " new_uid | |
fi | |
if [ ! -z "$new_uid" ]; then | |
uid_str="--uid $new_uid" | |
fi | |
useradd --create-home $uid_str --shell /usr/bin/bash "$username" || exit 1 | |
fi | |
if [[ $(passwd --status "$username" | awk '{print $2}') = P ]] | |
then | |
read -p "Do you want to change your password? [y|N]" change_pass | |
else | |
change_pass=y | |
fi | |
if [ "$change_pass" == 'y' ]; then | |
passwd "$username" | |
fi | |
default_groups="sudo,adm" | |
echo "Current groups:" | |
groups "$username" | |
while :; do | |
if [ -z "$new_groups" ]; then | |
read -p "Enter groups to add the user to [$default_groups]: " new_groups | |
fi | |
new_groups=${new_groups:-"$default_groups"} | |
usermod -aG "$new_groups" "$username" || exit 1 | |
# Check if the user belongs to sudo group | |
id --name --groups --zero "$username" | grep --quiet --null-data --line-regexp --fixed-strings "sudo" && break | |
echo "User needs to belong to sudo group!" | |
new_groups="" | |
done | |
HMDIR="/home/$username" | |
SSHDIR="$HMDIR/.ssh" | |
mkdir -p "$HMDIR/.ssh" | |
chown $username:$username "$SSHDIR" | |
chmod 700 "$SSHDIR" | |
KEYSFILE="$SSHDIR/authorized_keys" | |
touch $KEYSFILE | |
chown $username:$username "$KEYSFILE" | |
chmod 600 "$KEYSFILE" | |
read -p "Enter public key. Enter to skip (if key is already set): " pubkey | |
if [ -n "$pubkey" ]; then | |
echo $pubkey >> $KEYSFILE | |
else | |
if [ ! -s $KEYSFILE ]; then | |
echo "Authorized keys file is empty. Please provide your pub key." | |
exit 1 | |
fi | |
fi | |
read -p "IMPORTANT! Check if you can ssh as your user without password [y|N]: " can_login | |
if [ "$can_login" != 'y' ]; then | |
echo "Restart the script and enter a proper public key" | |
exit 1 | |
fi | |
echo "Disabling passwordless sudo!" | |
if [ -f /etc/sudoers.d/90-cloud-init-users ]; then | |
mv /etc/sudoers.d/90-cloud-init-users /etc/sudoers.d/90-cloud-init-users~ | |
else | |
: # TODO: for Raspberri Pi | |
fi | |
current=$SUDO_USER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is supposed to be used on AWS Ubuntu or Raspberry Pi to create your own user and disable the stock one.
HOW TO RUN:
or
ARGUMENTS (OPTIONAL):
WHAT IT DOES:
adm,sudo
.sudo
group.authorized_keys
. This can be skipped, but only ifauthorized_keys
file is not empty.ubuntu
user.TODO:
ubuntu
/pi
user (optional).root
login via ssh.pi
user password (?)RECOMMENDATION: