Created
April 1, 2019 17:28
-
-
Save aholbreich/d76f3d698a4c36d211a6937dcf6fe65f to your computer and use it in GitHub Desktop.
Linux: newuser.sh
This file contains hidden or 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/sh | |
# Script to add a user to Linux system | |
if [ $(id -u) -eq 0 ]; then | |
read -p "Enter username : " username | |
read -p "Enter userkey : " userkey | |
egrep "^$username" /etc/passwd >/dev/null | |
if [ $? -eq 0 ]; then | |
echo "$username exists!" | |
exit 1 | |
else | |
useradd -g users -m -d /home/$username -s /bin/bash $username | |
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" | |
mkdir /home/$username/.ssh | |
[ $? -eq 0 ] && echo ".ssh dir Ok" || echo ".ssh dir FAIL" | |
chmod 700 /home/$username/.ssh | |
chmod 755 /home/$username | |
touch /home/$username/.ssh/authorized_keys | |
chmod 600 /home/$username/.ssh/authorized_keys | |
chown -R $username: /home/$username | |
echo $userkey >> /home/$username/.ssh/authorized_keys | |
[ $? -eq 0 ] && echo "User Key copied" || echo "User key could not be copied" | |
fi | |
else | |
echo "Only root may add a user to the system" | |
exit 2 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment