Tuesday, November 12, 2019

copy ssh public key to remote server with one command

cat ~/.ssh/id_rsa.pub | ssh $server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

just substitute the $server above with user@remote-server

or I have created a bash script here

!/usr/bin/env bash

usage(){
  echo usage:: $0 -s [user@]\
}

if [ "$#" -ne 1 ]; then
    usage
    exit 1
fi

server=$1

if [ -z $server ]; then
  echo variable \$server is not set
  usage
  exit 1
fi

cat ~/.ssh/id_rsa.pub | ssh $server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

if [ $? -ne 0 ]; then
  echo "Command failed"
  exit 1
else
  echo "Command successful"
fi

No comments:

Post a Comment

Followers

About Me

Torrance, CA, United States