服务器A到服务器B免密登录

#!/bin/bash

变量定义

source_host="192.168.42.250" # 源主机 IP

target_host="192.168.24.43" # 目标主机 IP

target_user="nvidia" # 目标主机的用户名

ssh_port="6666" # SSH 端口号

生成 SSH 密钥对(如果没有密钥对的话)

echo "生成 SSH 密钥对..."

if ! -f "$HOME/.ssh/id_rsa" ; then

ssh-keygen -t rsa -b 2048 -f "$HOME/.ssh/id_rsa" -N ""

echo "SSH 密钥对生成成功!"

else

echo "SSH 密钥对已存在,跳过生成。"

fi

将公钥复制到目标主机(通过 SSH 登录目标主机)

echo "将公钥复制到目标主机 $target_host..."

ssh-copy-id -i "HOME/.ssh/id_rsa.pub" -p ssh_port target_user@target_host

测试免密登录

echo "测试免密登录..."

ssh -p ssh_port target_user@$target_host "echo '免密登录成功!'"

root@bogon ziyong#

相关推荐
开发者联盟league4 天前
安装pnpm
ssh
2601_961875244 天前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant
小小小花儿5 天前
SSH密钥配置(免密连接远程服务器)
服务器·ssh
m0_526119405 天前
ssh key生成,gitee配置ssh
运维·gitee·ssh
qq_338432375 天前
VSCode Remote-SSH 远程 Windows Server 卡死的排查与解决
windows·vscode·ssh
着迷不白5 天前
十、网络客户端工具curl, wget, ssh, scp, sftp, rsync
运维·网络·ssh
江畔柳前堤6 天前
github实战指南01-账号配置与 SSH 密钥
运维·人工智能·深度学习·ssh·github·pyqt·信号处理
相醉为友6 天前
Trae IDE WSL2/SSH 环境网络故障排查笔记
ide·笔记·ssh
c238567 天前
git常见错误和ssh验证推送
运维·git·ssh
zh路西法7 天前
【tmux入门】终端分屏、SSH远程守护与一键启动脚本
linux·运维·ssh·bash