Created
August 24, 2022 06:50
-
-
Save Lucchetto/b9f2d999546304fe9889e5941a978f08 to your computer and use it in GitHub Desktop.
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 | |
USER_DISK_PATH=/home/user-disks | |
USER_DISK_SIZE_IN_BYTES=1099511627776 | |
USER_PASSWORD_LENGTH=24 | |
USER_DEFAULT_SHELL=/bin/bash | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit 1 | |
fi | |
read -p "Insert new user's username: " new_user | |
if [ -z $new_user ]; then | |
echo "Please insert an username !" | |
exit 1 | |
elif getent passwd $new_user >/dev/null; then | |
echo "User already exists !" | |
exit 1 | |
fi | |
if [ ! -d $USER_DISK_PATH ]; then | |
echo "Creating folder for user disk at" $USER_DISK_PATH | |
mkdir -p $USER_DISK_PATH | |
fi | |
USER_DISK_FILE=$USER_DISK_PATH/$new_user.img | |
echo "Creating user disk image" $USER_DISK_FILE | |
fallocate -l $USER_DISK_SIZE_IN_BYTES $USER_DISK_FILE | |
sudo mkfs.ext4 -Enodiscard $USER_DISK_FILE | |
USER_MOUNT_POINT=/home/$new_user | |
echo "Create user mountpoint" $USER_MOUNT_POINT | |
mkdir -p $USER_MOUNT_POINT | |
echo "Writing entry to fstab" | |
echo $USER_DISK_FILE $USER_MOUNT_POINT ext4 defaults 0 0 >> /etc/fstab | |
mount $USER_MOUNT_POINT | |
#while true ; do | |
# read -s -p "Insert a password for user ${new_user}: " new_user_psw | |
# if [ ! -z $new_user_psw ]; then | |
# break | |
# fi | |
# echo "Please insert a password !" | |
#done | |
echo "Creating user" $new_user | |
useradd $new_user --no-create-home --home $USER_MOUNT_POINT -s $USER_DEFAULT_SHELL | |
new_user_psw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $USER_PASSWORD_LENGTH | head -n 1) | |
echo "$new_user:$new_user_psw" | chpasswd | |
cp -rT /etc/skel $USER_MOUNT_POINT | |
chown -R $new_user:$new_user $USER_MOUNT_POINT | |
echo "User" $new_user "create successfully with password" $new_user_psw | |
echo "Make sure to change it" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment