Skip to content

Instantly share code, notes, and snippets.

@zhanghai
Created August 8, 2021 01:36
Show Gist options
  • Save zhanghai/335f50fc91989869cd9e366f6eef9473 to your computer and use it in GitHub Desktop.
Save zhanghai/335f50fc91989869cd9e366f6eef9473 to your computer and use it in GitHub Desktop.
#!/bin/bash
config_directory="$(dirname "$(realpath "$0")")"
port=2222
while [[ "$#" -gt 0 ]]; do
case "$1" in
-c|--config-directory)
config_directory="$2"
shift
shift
;;
-p|--port)
port="$2"
shift
shift
;;
esac
done
if [[ ! -f "${config_directory}/host_key" ]]; then
ssh-keygen -t rsa -b 4096 -f "${config_directory}/host_key" -N ''
fi
cat <<EOF >"${config_directory}/sshd_config"
Port ${port}
HostKey ${config_directory}/host_key
AuthorizedKeysFile ${config_directory}/authorized_keys
ChallengeResponseAuthentication no
UsePAM yes
AllowAgentForwarding no
AllowTcpForwarding no
PrintMotd no
PidFile ${config_directory}/sshd.pid
Subsystem sftp internal-sftp
ForceCommand internal-sftp
EOF
echo "Note: You may want to manually kill the remaining sshd processes for active sessions after you exit the main sshd process."
"$(type -p sshd)" -Def "${config_directory}/sshd_config"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment