脚本一键配置ssh免密访问远程服务器
免密访问ubuntu 服务器
- 在ubuntu 创建脚本secret-free_login.sh,脚本内容参见下问。
- 修改脚本中的ip为要访问的服务器ip
- 执行脚本:
./secret-free_login.sh 要访问的服务器用户名
- 终端输入
svr
访问服务器
在ubuntu 上执行脚本secret-free_login.sh
#!/bin/bash
#
# Filename: secret-free_login.sh
#
# Copyright (C) 2018-2023 Eric MA <eric@company.com>. All Rights Reserved.
#
# History:
# 2020/06/13 - [Eric MA] Created file
#
# Maintainer: Eric MA <eric@email.com>
# Created: 2020-06-13
# LastChange: 2020-06-13
# Version: v0.0.01
#
svr_user=
svr_ip=172.11.0.11
script_name=/usr/local/bin/svr
if [[ -z $1 ]]; then
echo "error: command format - ./`basename $0` your_server_user_name"
exit
fi
svr_user=$1
# create command for accessing server
sudo touch ${script_name}
sudo chmod a+x ${script_name}
sudo sh -c "echo '#!/bin/bash' > ${script_name}"
sudo sh -c "echo 'ssh ${svr_user}@${svr_ip}' >> ${script_name}"
# secret-free
if [[ ! -f ~/.ssh/id_rsa.pub ]]; then
echo "generate ssh-key ..."
ssh-keygen -t rsa
fi
ssh-copy-id -i ~/.ssh/id_rsa.pub ${svr_user}@${svr_ip}
echo "how to access remote server?"
echo "$ svr"