Created
March 8, 2020 16:25
-
-
Save varunchandak/ea71fa2f025248aabc7893279dc12bec to your computer and use it in GitHub Desktop.
Create minimal user on Ubuntu Machine
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 | |
# The below is run as root user (or sudo user) and tested on Ubuntu Machine | |
export USER_NAME="<ENTER USER NAME HERE>" | |
# Create a User with restricted bash shell | |
useradd -s /bin/rbash -m "$USER_NAME" | |
mkdir /home/"$USER_NAME"/.ssh/ | |
cd /home/"$USER_NAME"/ | |
# Copy the pubkeys here and change permissions | |
cat "<AUTH_KEYS>" >> .ssh/authorized_keys | |
chmod 0700 .ssh/ | |
chown -R "$USER_NAME":"$USER_NAME" .ssh | |
# Add command symlinks to the user private bin dir | |
cd /home/"$USER_NAME" | |
mkdir bin | |
ln -s /bin/ls /home/"$USER_NAME"/bin/ | |
ln -s /usr/bin/top /home/"$USER_NAME"/bin/ | |
ln -s /usr/bin/du /home/"$USER_NAME"/bin/ | |
ln -s /bin/date /home/"$USER_NAME"/bin/ | |
ln -s /bin/uname /home/"$USER_NAME"/bin/ | |
ln -s /usr/bin/free /home/"$USER_NAME"/bin/ | |
ln -s /usr/bin/head /home/"$USER_NAME"/bin/ | |
ln -s /usr/bin/tail /home/"$USER_NAME"/bin/ | |
ln -s /usr/bin/less /home/"$USER_NAME"/bin/ | |
ln -s /bin/more /home/"$USER_NAME"/bin/ | |
ln -s /bin/cat /home/"$USER_NAME"/bin/ | |
ln -s /bin/ping /home/"$USER_NAME"/bin/ | |
ln -s /usr/bin/telnet /home/"$USER_NAME"/bin/ | |
# Update path and deny path editing for "$USER_NAME" | |
cd /home/"$USER_NAME" | |
sed -i '/^PATH/s/PATH.*/PATH=\$HOME\/bin/g' .profile | |
sed -i '/^PATH/s/PATH.*/PATH=\$HOME\/bin/g' .bashrc | |
chattr +i .bashrc .profile | |
# Login via user and test: | |
echo $PATH # Output: /home/"$USER_NAME"/bin | |
ls -l | |
ping -c5 google.com | |
cat .profile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment