Skip to content

Instantly share code, notes, and snippets.

@robmsmt
Last active April 11, 2024 04:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save robmsmt/b8300e7a0d711a7616e948a8232289a5 to your computer and use it in GitHub Desktop.
Save robmsmt/b8300e7a0d711a7616e948a8232289a5 to your computer and use it in GitHub Desktop.
make_user_gen_ssh.sh
#!/bin/bash
NEW_USER=$1
set -e
if [ -z "$NEW_USER" ];then
echo "Please provide user as arg... exiting."
exit 1
fi
sudo adduser $NEW_USER
mkdir -p user_keys
ssh-keygen -f user_keys/$NEW_USER
sudo -u $NEW_USER mkdir -p /home/$NEW_USER/.ssh/
sudo cp user_keys/$NEW_USER* /home/$NEW_USER/.ssh/
sudo chown -R $NEW_USER /home/$NEW_USER/.ssh
sudo su $NEW_USER <<'EOF'
cd ~/.ssh
cat *.pub >> ./authorized_keys
EOF
chmod 600 user_keys/$NEW_USER*
zip -re user_keys/$NEW_USER.zip user_keys/$NEW_USER user_keys/$NEW_USER.pub
echo "User setup complete... send them private+pub key:"
echo "$(readlink -f user_keys/$NEW_USER.zip)"
~
@gitcoleman
Copy link

Hi Rob,

This is what I was talking about when I said it creates a key for the logged in user, not the new user.

image

I’ve underlined the part in red that I was concerned about.

image

Regards,
Wes

@robmsmt
Copy link
Author

robmsmt commented Oct 5, 2021

I think that part is just a comment: https://serverfault.com/a/743551/598820

@gitcoleman
Copy link

gitcoleman commented Oct 5, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment