腳本一鍵配置ssh免密訪問遠程服務器

腳本一鍵配置ssh免密訪問遠程服務器

免密訪問ubuntu 服務器

  1. 在ubuntu 創建腳本secret-free_login.sh,腳本內容參見下問。
  2. 修改腳本中的ip爲要訪問的服務器ip
  3. 執行腳本:./secret-free_login.sh 要訪問的服務器用戶名
  4. 終端輸入svr訪問服務器

在ubuntu 上執行腳本secret-free_login.sh

#!/bin/bash
#
# Filename: secret-free_login.sh
#
# Copyright (C) 2018-2023 Eric MA  <[email protected]>. All Rights Reserved.
#
# History:
#    2020/06/13 - [Eric MA] Created file
#
# Maintainer: Eric MA <[email protected]>
#    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"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章