shell 生成public key,实现免密码SSH登录

本文介绍了一个简单的bash脚本,用于自动化SSH密钥的生成及远程服务器上的部署过程。用户只需通过命令行调用该脚本并提供远程服务器的信息,即可轻松完成SSH免密码登录的设置。
#!/bin/bash

if [ ! $1 ]; then
  echo "usage: copyssh.sh user@remoteserver "
  exit
fi

PORT=22

if [ $2 ]; then
   PORT=$2
fi

echo $1 $PORT

# Uploads your id_rsa.pub to the specified host, wrapped for readability

if [ ! -r ${HOME}/.ssh/id_rsa.pub ]; then
  ssh-keygen -b 2048 -t rsa
fi

# Make sure auth file exists and chmod to 600

ssh $1 -p $PORT 0> echo "mkdir ~/.ssh; touch ~/.ssh/authorized_keys; 
chmod u+rw .ssh/authorized_keys"

# Append to the copy on the remote server

cat ~/.ssh/id_rsa.pub | ssh $1 -p $PORT "cat - >> .ssh/authorized_keys"

if [ $? -eq 0 ]; then
  echo "Success"
fi


使用方法:
1.保存成文件 copyssh.sh
2.然后在shell >. ./copyssh.sh username@remoteserver
3.输入密码两次,出现 'success!'那说明设置成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值