update-ssh-keys 453 B

123456789101112131415161718192021
  1. #!/bin/bash
  2. USERNAME=$1
  3. HOME_DIR=$(grep ^$USERNAME /etc/passwd | cut -f6 -d:)
  4. if [ ! -d $HOME_DIR/.ssh ]; then
  5. mkdir -p $HOME_DIR/.ssh
  6. chmod 0700 $HOME_DIR/.ssh
  7. fi
  8. if [ ! -e $HOME_DIR/.ssh/authorized_keys ]; then
  9. touch $HOME_DIR/.ssh/authorized_keys
  10. chmod 0600 $HOME_DIR/.ssh/authorized_keys
  11. fi
  12. if ! grep -q "$2" $HOME_DIR/.ssh/authorized_keys; then
  13. echo "$2" >> $HOME_DIR/.ssh/authorized_keys
  14. fi
  15. chown -R $USERNAME $HOME_DIR/.ssh