Bash_Scripts/SSH/Create-Remote-User.bash

44 lines
1.1 KiB
Bash
Raw Permalink Normal View History

2021-07-28 09:32:08 -05:00
#!/bin/bash --posix
set -e
# Remote System
REMOTE_SSH_SERVER=Kansas-Xeon
REMOTE_HOST_NAME=kansas-xeon.orion-technologies.io
2021-09-28 02:34:51 -05:00
REMOTE_USER_TO_CREATE=Sam
2021-07-28 09:32:08 -05:00
# Local System
SSH_FILE_DIRECTORY=~/.ssh
2021-08-04 21:17:20 -05:00
SSH_FILE_NAME=${REMOTE_USER_TO_CREATE}-id_ed25519
SSH_FILE="${SSH_FILE_DIRECTORY}"/"${SSH_FILE_NAME}"
SSH_HOST=Kansas-Xeon
2021-07-28 09:32:08 -05:00
ssh-keygen -b 512 -t ed25519 -f ${SSH_FILE} -N ""
2021-07-28 09:32:08 -05:00
chmod 600 ${SSH_FILE}
PUB_KEY_CONTENTS=$(cat ${SSH_FILE}.pub)
ssh root@${REMOTE_SSH_SERVER} /bin/bash << EOF
2021-07-28 09:32:08 -05:00
useradd ${REMOTE_USER_TO_CREATE}
mkdir /home/${REMOTE_USER_TO_CREATE}/.ssh
touch /home/${REMOTE_USER_TO_CREATE}/.ssh/authorized_keys
echo ${PUB_KEY_CONTENTS} >> /home/${REMOTE_USER_TO_CREATE}/.ssh/authorized_keys
chown -R ${REMOTE_USER_TO_CREATE}:${REMOTE_USER_TO_CREATE} /home/${REMOTE_USER_TO_CREATE}/.ssh
EOF
cat << EOF >> ~/.ssh/config
2021-08-17 20:21:33 -05:00
2021-07-28 09:32:08 -05:00
Host ${SSH_HOST}
HostName ${REMOTE_HOST_NAME}
Match user ${REMOTE_USER_TO_CREATE} host ${REMOTE_HOST_NAME}
2021-07-28 09:32:08 -05:00
Identityfile ${SSH_FILE}
EOF
ssh ${REMOTE_USER_TO_CREATE}@${SSH_HOST} /bin/bash << EOF
mkdir ~/.bin
2021-09-28 02:34:51 -05:00
echo export PATH=$PATH:~/.bin >> ~/.bash_profile
2021-07-28 09:32:08 -05:00
EOF
echo
2021-08-17 20:21:33 -05:00
echo "Finished, login via ssh with \"${REMOTE_USER_TO_CREATE}@${SSH_HOST}\""