服务器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\]#

相关推荐
qinyia33 分钟前
通过 Wisdom SSH AI 助手部署和配置 Nginx Web 服务器
人工智能·nginx·ssh
Trouvaille ~33 分钟前
【LInux】进程程序替换与shell实现:从fork到exec的完整闭环
linux·运维·c语言·c++·ssh·进程替换·基础入门
秦时明月天明1 天前
GitLab SSH Key 过期:git pull failed : remote your ssh key has expired
git·ssh·gitlab
白里透白的小白2 天前
复盘 Git+GitHub SSH 配置:从权限报错到免密推送的全流程解决方案
git·ssh·github·版本控制
西京刀客2 天前
Mac下ssh终端之iTerm2 (Oh My Zsh + powerlevel10k)
运维·macos·ssh·iterm2
jimy12 天前
在一台电脑上生成多个ssh公钥并添加到不同GitHub账号
运维·ssh·github
ℳ₯㎕ddzོꦿ࿐3 天前
先立后破:Linux 下“新建管理员 → 验证 → 禁用 root 远程 SSH”的零翻车笔记
linux·笔记·ssh
jiafuLi20183 天前
TortoiseGit 和本地 Git 共享 SSH 密钥和用户配置
运维·git·ssh
一点晖光3 天前
ssh连接慢的原因调查及解决方案
运维·ssh
阿巴阿巴boer4 天前
用wsl搭建远程linux服务器
linux·运维·服务器·ssh