Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create minimal user on Ubuntu Machine
# The below is run as root user (or sudo user) and tested on Ubuntu Machine
# 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
cat .profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment