Cisco交换机SSH使用RSA公钥免密登录(IOS与Nexus,服务器以RHEL8为例)

news2025/2/23 22:17:00

目录

  • 需求
  • 实验步骤
    • 0. 实验环境
    • 1. Linux
    • 2. CiscoIOS
      • 基础设置
      • 保存密钥
      • 登陆测试
    • 3. CiscoNexus
      • 基础配置
      • 保存密钥
      • 登陆测试

需求

在实际工作中,常会遇到自动化的需求,那么在自动采集、配置等对网络设备的自动化需求中,不可避免的会遇到需要登录->采集->录入的流程。
而第一步登录,一般采用ssh进行安全的远程连接。不过每次需要输入密码的远程登陆不便于自动化的应用,因此需要采用免密登录

一般针对服务器如Linux的ssh免密登录,仅需要将需要免密登录进来的其他服务器的rsa公钥放入自己的authorized_keys文件中即可。
详细可以见我之前的笔记:linux下的openssh简介(centos 8)

那么,针对网络设备没有这个文件,应该如何进行rsa公钥的存放和ssh免密登录?
网络上对于华为设备如何配置ssh免密登录的文章很多(同时现网中很多华为真实设备有自动保存登录设备的RSA公钥的功能),但对于Cisco尤其存在IOS和Nexus两种软件版本的交换机反而没有直接可以使用的文章,因此本文将对此进行分享。

Cisco官网上有RSA和X.509两种配置方式,分别是公钥和CA两种方式,这里介绍的是RSA公钥免密登陆方式。
可以参考的官方文档:Cisco Nexus 9000 Series NX-OS Security Configuration Guide, Release 10.5(x)

实验步骤

  1. 服务器创建rsa密钥对
  2. Cisco交换机保存服务器创建的rsa公钥
  3. 服务器ssh登录Cisco设备进行测试

0. 实验环境

在这里插入图片描述

设备型号IP
LinuxRHEL8192.168.100.141
SwitchCisco IOS192.168.100.100
NXOSCisco Nexus192.168.100.101
NetCLoud0(桥接VMnet8)N/A

1. Linux

Linux上仅需要能ping通两台交换机,申请rsa密钥对并将公钥记录下来即可

# 查看ip
[root@linux ~]# ip a s ens33 | awk 'NR==4 {print $2}'
192.168.100.141/24

# 生成一个2048长度的密钥
# 交换机可能因为版本限制rsa的长度,2048是一个非常稳妥的长度
[root@linux ~]# ssh-keygen -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WwIJVzyTw89Rsp2JVfUbUwjRr0g+LCUqXoXPT4NMC14 root@docker01
The key's randomart image is:
+---[RSA 2048]----+
|    . .+....==.oo|
|     o .B .* oo o|
|      o  Bo.+  +.|
|       .o E o   =|
|       .SX.O . o |
|      . ++B B .  |
|     . o.  + o   |
|      .     .    |
|                 |
+----[SHA256]-----+

# 查看公钥,记录下来,可以用
# 以64字符为一行,因为Cisco IOS的公钥录入有行长度限制,需要多行录入,提前设置好方便录入
# 仅需要记录从ssh-rsa开始到主机名空格前结束
[root@linux ~]# fold -w 64 ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoVHfQEHOq50u7kl5ukfPwwoYn
RHGCaYEHht4Fy8O3pGM3hk9GyT/IsBBquiBR1cxPjvZFlIGUd9gc2v4Xk8JHPIsH
f3IaS/5lhL257N4CZcL+aZh/PWCaY3DSmZqJ3ywFlX1YLUDlUvelcG2fmc/p0brM
LCxawgePkzl/MQq++aiEW/cqfXHR134InlV9nhBYyADGQff7Mmg6ysq+EK+KBMqG
h6dSquXo3i8PnQSI0RwIf8W9oUOWFIFJAzaaauqmMQhwxFbsc6vL+OdctHc9Ndgy
z04O5bmoI7qT0Tgh1yuynHWmkfuUnC+Ci/S83BaFOyOKxn4ymEVA3mJCcA1t roo
t@linux

2. CiscoIOS

基础设置

# 查看版本
Switch#show version
Cisco IOS Software, vios_l2 Software (vios_l2-ADVENTERPRISEK9-M), Experimental Version 15.2(20200924:215240) [sweickge-sep24-2020-l2iol-release 135]

# 修改主机名
test>en
test#configure terminal
test(config)#hostname Switch

# 设置svi1 ip
Switch(config-if)#int vlan 1
Switch(config-if)#ip add 192.168.100.100 255.255.255.0

# ping测试
Switch#ping 192.168.100.141
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.100.141, timeout is 2 seconds:
.!!!!
Success rate is 80 percent (4/5), round-trip min/avg/max = 4/4/5 ms

保存密钥

# 均在配置模式下配置

# 设置域名,不设置不能启用ssh和rsa
Switch(config)#ip domain-name test

# 创建rsa密钥,用于开始ssh
Switch(config)#crypto key generate rsa
% You already have RSA keys defined named Switch.test.
Choose the size of the key modulus in the range of 360 to 4096 for your
  General Purpose Keys. Choosing a key modulus greater than 512 may take
  a few minutes.
How many bits in the modulus [512]: 2048
% Generating 2048 bit RSA keys, keys will be non-exportable...
[OK] (elapsed time was 3 seconds)

# 启用sshv2
Switch(config)#ip ssh version 2

# 启用ssh登录
Switch(config)#line vty 0 4
Switch(config-line)#transport input ssh
Switch(config-line)#login local
Switch(config-line)#exit

# 设置无密码账号linux
Switch(config)#username linux privilege 15

# 导入linux的公钥
Switch(config)#ip ssh pubkey-chain
Switch(conf-ssh-pubkey)#username linux
Switch(conf-ssh-pubkey-user)#key-string
# 以下是录入密钥,将前面录入的复制下来
Switch(conf-ssh-pubkey-data)#$2EAAAADAQABAAABAQCoVHfQEHOq50u7kl5ukfPwwoYn
Switch(conf-ssh-pubkey-data)#$k9GyT/IsBBquiBR1cxPjvZFlIGUd9gc2v4Xk8JHPIsH
Switch(conf-ssh-pubkey-data)#$Zh/PWCaY3DSmZqJ3ywFlX1YLUDlUvelcG2fmc/p0brM
Switch(conf-ssh-pubkey-data)#$/cqfXHR134InlV9nhBYyADGQff7Mmg6ysq+EK+KBMqG
Switch(conf-ssh-pubkey-data)#$8W9oUOWFIFJAzaaauqmMQhwxFbsc6vL+OdctHc9Ndgy
Switch(conf-ssh-pubkey-data)#$HWmkfuUnC+Ci/S83BaFOyOKxn4ymEVA3mJCcA1t
#录入完后退出,即完成录入
Switch(conf-ssh-pubkey-data)#exit
Switch(conf-ssh-pubkey-user)#exit
Switch(conf-ssh-pubkey)#exit

登陆测试

在linux上ssh Cisco交换机

# 登录后,可以看到回显有“永久将主机(RSA)添加入已知列表“
[root@docker01 ~]# ssh linux@192.168.100.100
The authenticity of host '192.168.100.100 (192.168.100.100)' can't be established.
RSA key fingerprint is SHA256:1DfhYAi7UO9ZocSjUhqnF6zCSYrAhXKrSI21J9+b+HE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.100.100' (RSA) to the list of known hosts.

IOSv - Cisco Systems Confidential -

Supplemental End User License Restrictions

This IOSv software is provided AS-IS without warranty of any kind. Under no circumstances may this software be used separate from the Cisco Modeling Labs Software that this software was provided with, or deployed or used as part of a production environment.

By using the software, you agree to abide by the terms and conditions of the Cisco End User License Agreement at http://www.cisco.com/go/eula. Unauthorized use or distribution of this software is expressly prohibited.

IOSv - Cisco Systems Confidential -

Supplemental End User License Restrictions

This IOSv software is provided AS-IS without warranty of any kind. Under no circumstances may this software be used separate from the Cisco Modeling Labs Software that this software was provided with, or deployed or used as part of a production environment.

By using the software, you agree to abide by the terms and conditions of the Cisco End User License Agreement at http://www.cisco.com/go/eula. Unauthorized use or distribution of this software is expressly prohibited.

Switch#en
Switch#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Switch(config)#
# 作为权限15的账号,可以进入配置模式进行配置

3. CiscoNexus

基础配置

# 查看版本,是9.3.8的Nexus OS
switch# show version
Cisco Nexus Operating System (NX-OS) Software
Software
  BIOS: version
 NXOS: version 9.3(8)
  BIOS compile time:
  NXOS image file is: bootflash:///nxos.9.3.8.bin
  NXOS compile time:  8/4/2021 13:00:00 [08/04/2021 22:25:26]

# 配置设备名
switch# conf t
Enter configuration commands, one per line. End with CNTL/Z.
switch(config)# hostn NXOS
NXOS(config)#

# 设置svi1 ip
NXOS(config-if)# ip add 192.168.100.101/24
NXOS(config-if)# do show ip int bri

IP Interface Status for VRF "default"(1)
Interface            IP Address      Interface Status
Vlan1                192.168.100.101 protocol-up/link-up/admin-up


# ping测试
NXOS# ping 192.168.100.141
PING 192.168.100.141 (192.168.100.141): 56 data bytes
64 bytes from 192.168.100.141: icmp_seq=0 ttl=63 time=9.11 ms
64 bytes from 192.168.100.141: icmp_seq=1 ttl=63 time=8.318 ms
64 bytes from 192.168.100.141: icmp_seq=2 ttl=63 time=19.181 ms
64 bytes from 192.168.100.141: icmp_seq=3 ttl=63 time=7.7 ms
64 bytes from 192.168.100.141: icmp_seq=4 ttl=63 time=5.08 ms

--- 192.168.100.141 ping statistics ---
5 packets transmitted, 5 packets received, 0.00% packet loss
round-trip min/avg/max = 5.08/9.877/19.181 ms


保存密钥

# 生成密钥,启用ssh
NXOS(config)# ssh key rsa
NXOS(config)# feature ssh

# 查看ssh server key
NXOS(config)# show ssh key
**************************************
rsa Keys generated:Wed Aug 14 02:22:31 2024

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC2ag54FDSbAT3Z3uVxHJ5LVEIedz6ximnx1lJr2gC6
r96XcUw2l+3vx704V6nMiFrdjsuMuP+k9cVmuHvdUy09/Q6pPiUD8I0/t+SdMz+PANoAsURLa06J/Gqo
v6RJVPtqKum1DsMR91d8UYXrNFKq62SvCDaNa486bAd8+/qMRw==

bitcount:1024
fingerprint:
SHA256:RGZdz0/waQniT3HN+S+5haHBVst0N7DPHTc1WLadUyc
**************************************
could not retrieve dsa key information
**************************************
could not retrieve ecdsa key information
**************************************

# 创建登陆方式为公钥登录的用户,输入linux的公钥
# 因为为一行输入,所以cat linux的公钥直接复制即可,不要切断换行
NXOS(config)# username linux sshkey ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoVHfQEHOq50u7kl5ukfPwwoYnRHGCaYEHht4Fy8O3pGM3hk9GyT/IsBBquiBR1cxPjvZFlIGUd9gc2v4Xk8JHPIsHf3IaS/5lhL257N4CZcL+aZh/PWCaY3DSmZqJ3ywFlX1YLUDlUvelcG2fmc/p0brMLCxawgePkzl/MQq++aiEW/cqfXHR134InlV9nhBYyADGQff7Mmg6ysq+EK+KBMqGh6dSquXo3i8PnQSI0RwIf8W9oUOWFIFJAzaaauqmMQhwxFbsc6vL+OdctHc9Ndgyz04O5bmoI7qT0Tgh1yuynHWmkfuUnC+Ci/S83BaFOyOKxn4ymEVA3mJCcA1t

登陆测试

在linux上ssh Cisco交换机

# 使用创建的用户在linux上ssh登录Cisco交换机
# 登录后,可以看到回显有“永久将主机(RSA)添加入已知列表“
[root@docker01 ~]# ssh linux@192.168.100.101
The authenticity of host '192.168.100.101 (192.168.100.101)' can't be established.
RSA key fingerprint is SHA256:RGZdz0/waQniT3HN+S+5haHBVst0N7DPHTc1WLadUyc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.100.101' (RSA) to the list of known hosts.
User Access Verification

Cisco NX-OS Software
Copyright (c) 2002-2021, Cisco Systems, Inc. All rights reserved.
Nexus 9000v software ("Nexus 9000v Software") and related documentation,
files or other reference materials ("Documentation") are
the proprietary property and confidential information of Cisco
Systems, Inc. ("Cisco") and are protected, without limitation,
pursuant to United States and International copyright and trademark
laws in the applicable jurisdiction which provide civil and criminal
penalties for copying or distribution without Cisco's authorization.

Any use or disclosure, in whole or in part, of the Nexus 9000v Software
or Documentation to any third party for any purposes is expressly
prohibited except as otherwise authorized by Cisco in writing.
The copyrights to certain works contained herein are owned by other
third parties and are used and distributed under license. Some parts
of this software may be covered under the GNU Public License or the
GNU Lesser General Public License. A copy of each such license is
available at
http://www.gnu.org/licenses/gpl.html and
http://www.gnu.org/licenses/lgpl.html
***************************************************************************
*  Nexus 9000v is strictly limited to use for evaluation, demonstration   *
*  and NX-OS education. Any use or disclosure, in whole or in part of     *
*  the Nexus 9000v Software or Documentation to any third party for any   *
*  purposes is expressly prohibited except as otherwise authorized by     *
*  Cisco in writing.                                                      *
***************************************************************************
NXOS# conf t
Enter configuration commands, one per line. End with CNTL/Z.
# 该用户在创建时没有设置权限,因此没有权限进入接口视图,可以后续根据需求自行设置
NXOS(config)# int mgmt0
% Permission denied for the role
NXOS(config)# vlan 2
% Permission denied for the role

实验完毕。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2049677.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

tensorboard显示一片空白解决方案

OK艾瑞巴蒂 不知道看这个视频几个小土堆过来的,今天已经发了一篇博文探讨快速下载tensorboard了 下面用的时候叒出现问题了 from torch.utils.tensorboard import SummaryWriter writer SummaryWriter("logs")# writer.add_image() # Yx for i in range…

实时手势识别(1)- 基于手部检测+手部分类

目录 前言 1.实现效果 2.非端到端实现的原因 3.分类网络与数据准备 4.训练结果 5.测试结果 6.训练代码 7.训练日志 7.1ResNet18训练日志 7.2ShuffleNet_v2训练日志 前言 利用YOLOv8获取手部区域,然后对手部区域进行分类,实现手势识别。 本文使…

powershell 终端 执行 pnpm -v报错

1.问题描述: 明明全局已安装 pnpm ,但在vscode默认终端 powershell 执行 pnpm -v 却报错: 2.问题根因: 原因是 PowerShell 执行策略问题。 3.解决方案: 以管理员身份运行 PowerShell 查看 PowerShell 的执行策略…

初探systemⅡ·慢思考

本篇笔记记录于 May 30th, 2023 oai联合创始人Andrej曾在微软大会上的报告中有提到LLMs对于人类快、慢思考两种认知推理模式的当下探索与未来展望,这里曾经得到的启示是:未来在模型的训练与推理侧是否会出现一种新的长链认知范式?如在RLHF过程…

秋招突击——8/13——并查集——复习{有塔一面}——新作{亲戚关系}

文章目录 引言复习并查集模板复习——有塔一面 新作亲戚关系 总结 引言 这两天准备腾讯的第二面,看了很多人的面经,发现考并查集的题目蛮多的,这里整理学习一下! 复习 并查集模板 这里学习了B站的麦克老师的课程,对…

MySQL与SQLserver

与MySQL的差别 SQL Server和MySQL都是广泛使用的关系数据库管理系统(RDBMS),它们的SQL语法有很多相似之处,但也存在一些差异。以下是一些主要的语法区别: 1. 数据库和表的创建 SQL Server CREATE DATABASE databas…

Ma Spaghet!

目录 一、题目 二、思路 三、payload 3.1 方案一 3.2 方案二(官方) 四、思考与总结 一、题目 <!-- Challenge --> <h2 id"spaghet"></h2> <script>spaghet.innerHTML (new URL(location).searchParams.get(somebody) || "Somebody…

产品经理-​你做产品经理有什么优势?(39)

你做产品经理有什么优势&#xff1f; 这是一个关于自我认知、个人优势的问题 人贵有自知之明&#xff0c;求职者应该对自己的优缺点有一个客观、深入的认识 大公司往往更加看重你的基本素质&#xff08;逻辑分析、学习能力、潜力等&#xff09; 因为大公司有相对成熟的培养体系…

OpenDDS的Rtps_Udp传输协议可靠性QoS收发基本流程

OpenDDS中,实现了Rtps_Udp传输协议(非纯udp)的可靠性传输。传输的线程包括: 1)发送方线程主要线程和定时器 《1》应用线程 《2》网络异步发送线程 《3》Heartbeat定时器 《4》Nak_response定时器 2)接收方主要线程和定时器 《1》网络异步接收线程 《2》heartbeat_respons…

Java | Leetcode Java题解之第344题反转字符串

题目&#xff1a; 题解&#xff1a; class Solution {public void reverseString(char[] s) {int n s.length;for (int left 0, right n - 1; left < right; left, --right) {char tmp s[left];s[left] s[right];s[right] tmp;}} }

【C++】智能指针详解

一、从new和delete谈起 在C中&#xff0c;可以使用new和delete关键字进行对象的创建和销毁&#xff0c;new一个对象实际上是在堆上分配内存&#xff0c;而new出来的对象也要自己用delete释放&#xff0c;从而回收内存&#xff0c;否则会造成内存的泄露。由程序员自己new来分配…

[手机Linux PostmarketOS]五, docker安装和使用

docker容器 一&#xff0c;docker安装和配置 安装 docker 和 docker-compose&#xff1a; sudo apk add docker docker-cli-compose #安装docker sudo service docker start #启动docker服务 sudo rc-update add docker default #设置docker为自启动可选关…

【PostgreSQL003】PostgreSQL数据表空间膨胀,磁盘爆满,应用宕机(经验总结,已更新)

1.一直以来想写下基于PostgreSQL的系列文章&#xff0c;作为较火的数据ETL工具&#xff0c;也是日常项目开发中常用的一款工具&#xff0c;最近刚好挤时间梳理、总结下这块儿的知识体系。 2.熟悉、梳理、总结下PostgreSQL数据库相关知识体系。空间膨胀&#xff08;主键、外键、…

汇编语言:call、call far ptr、call word ptr、call dword ptr、call 寄存器

引言 call指令是转移指令&#xff0c;CPU执行call指令&#xff0c;进行两步操作&#xff1a; &#xff08;1&#xff09;将当前IP或当前CS和IP压入栈中 &#xff08;2&#xff09;转移。call指令不能短转移&#xff0c;除此之外&#xff0c;call指令转移的方法跟jmp指令的原理…

柔性超级电容器咋储能?生物聚合物在其中起啥作用?有啥挑战?

*本文只作阅读笔记分享* 一、引言 随着对化石燃料影响的日益关注&#xff0c;开发用于先进电化学能量存储设备的绿色和可再生材料变得至关重要。超级电容器因其出色的寿命、安全性和宽温度操作范围等优势而成为有前途的储能候选者。柔性超级电容器特别适合为轻质可穿戴电子设…

xss GAME (xss漏洞攻击1-8)

目录 xss网页链接 第一关 第二关 第三关 ​编辑第四关 ​编辑第五关 ​编辑第六关 第七关 第一种 Function构建函数 第二种 tostring parseInt 第三种 silce() ​编辑第八关&#xff08;安全过滤框架 dom破坏&#xff09; xss网页链接 XSS Game - Learning XSS Ma…

linux之网络子系统-GSO/TSO 源码分析

一、GSO/TSO GSO 目前在内核5.10.* 版本时&#xff0c;已经是合入主线&#xff0c;就是对TCP/UDP都支持并且在网络协议栈GSO功能是默认打开的。虽然可以通过ethtool -K 网卡名 gso off 关闭&#xff0c;但是在L3/L4还是走GSO逻辑&#xff0c;关不掉。我目前是没有找到内核源码…

NextJs - 服务端/客户端组件之架构多样性设计

NextJs - 服务端/客户端组件之架构多样性设计 前言一. 架构设计1.1 SSR流式渲染常见错误设计之 - 根页面同步阻塞1.2 架构设计之 - 客户端组件依赖于服务端组件数据① 使用 Redux 完成数据共享 1.3 架构设计之 - 单页内的分步骤跳转① 如何做到服务端组件和客户端组件之间的切换…

libevent之android与鸿蒙编译过程

背景 最近基于libevent开发了一个端侧的缓存代理库&#xff0c;先是基于macOS编译开发的&#xff0c;基本0问题&#xff0c;后来移植到鸿蒙与android时遇到一些编译链接问题。 libevent版本如下&#xff1a; 软件版本号libevent-2.1.8 android编译 编译环境 android studio…

EmguCV学习笔记

版权声明&#xff1a;本文为博主原创文章&#xff0c;转载请在显著位置标明本文出处以及作者网名&#xff0c;未经作者允许不得用于商业目的。 笔者的博客网址&#xff1a;https://blog.csdn.net/uruseibest 本教程将分为VB.Net和C#两个版本分别进行发布。 教程VB.net版本请…