Last active
April 6, 2022 03:03
-
-
Save mamemomonga/bf56e004b6f2077d1c4eb3a77d34048b to your computer and use it in GitHub Desktop.
GitHubに登録されたSSH公開鍵でログイン可能な同名のUNIXユーザを作成する。
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 | |
set -eu | |
# デフォルトのパスワードが作成され ~/passwords.txt で保存されます。 | |
if [ "$(id -u)" != "0" ]; then | |
echo "please run as root" | |
exit 1 | |
fi | |
GITHUB_USERNAME="${1:-}" | |
if [ -z "$GITHUB_USERNAME" ]; then | |
echo "USAGE: $0 GITHUB_USERNAME" | |
exit 1 | |
fi | |
USERNAME="$GITHUB_USERNAME" | |
DATETIME="$( date +"%Y/%m/%d %H:%M:%S" )" | |
PUBLIC_KEYS="$( echo "# $GITHUB_USERNAME ($DATETIME)"; \ | |
curl -s "https://api.github.com/users/$GITHUB_USERNAME/keys" | \ | |
jq -r '.[]|.key+" '$GITHUB_USERNAME'@github/"+(.id|tostring)' )" | |
echo "$PUBLIC_KEYS" | |
PASSWORD=$(openssl rand -base64 64 | perl -E 'local $/; $_=<>; s#\n#_#g;s#/#.#g;s#/#!#g; for my $c(1..16) { print substr($_,$c,1_) }; say ""') | |
useradd -m -s /bin/bash $USERNAME | |
echo "$USERNAME:$PASSWORD" | chpasswd | |
echo "仮パスワード: $PASSWORD" > /home/$USERNAME/password.txt | |
chown $USERNAME /home/$USERNAME/password.txt | |
chmod 600 /home/$USERNAME/password.txt | |
mkdir -m 0700 /home/$USERNAME/.ssh | |
chown $USERNAME:$USERNAME /home/$USERNAME/.ssh | |
touch /home/$USERNAME/.ssh/authorized_keys | |
chmod 0600 /home/$USERNAME/.ssh/authorized_keys | |
chown $USERNAME:$USERNAME /home/$USERNAME/.ssh/authorized_keys | |
echo "$PUBLIC_KEYS" > /home/$USERNAME/.ssh/authorized_keys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment