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

相关推荐
如若1233 天前
Ubuntu 无 sudo 安装花生壳并实现 SSH 内网穿透:Conda 环境部署、冲突排查与自动启动
linux·运维·ubuntu·ssh·内网穿透
芳草萋萋鹦鹉洲哦4 天前
【git】重新生成并添加 SSH Key(Mac)
git·macos·ssh
午安~婉4 天前
git中http与ssh连接
git·http·ssh
炸膛坦客5 天前
Git 和 GitHub:(七)将本地新建仓库与 GitHub 远程仓库关联起来(SSH)
git·ssh·github
小莫分享6 天前
sshw:用交互搜索和 Web 配置高效管理 SSH Server
linux·运维·golang·开源·ssh
Jerry小杰7 天前
运维神器|单端口搞定多台内网SSH!frp端口复用终极教程
运维·ssh
BD_Marathon7 天前
xshell无法连接上centos7
ssh
吳所畏惧9 天前
宝塔面板Redis密码修改指南:SSH命令修改 vs 面板UI界面修改,哪个更靠谱?
运维·服务器·数据库·redis·缓存·ssh
xuefeiniao9 天前
CentOS + 宝塔服务器偶发 SSH、面板、网站全部无法访问排查记录(PHP-FPM 内存耗尽案例)
服务器·centos·ssh·php
weixin_457340219 天前
ssh 本地链接服务器地址和服务器查看本地地址
运维·服务器·ssh