使用key登陆管理linux服务器/免密码登陆linux服务器

简介:

ssh除了密码验证外,还有一种比较常用的验证方式:key;他的好处除了安全外,还可以实现linux之间的免密码登陆 ,方便管理或者批量维护。

本文介绍一下windows/linux下如何配置key免密码登陆linux服务器

01 [root@A ~]# ssh-keygen -t rsa
02  
03 Generating public/private rsa key pair.
04  
05 Enter file in which to save the key (/root/.ssh/id_rsa):
06 //key保存的路径和文件名
07  
08 Enter passphrase (empty for no passphrase):
09  
10 //key的密码,免密码登陆的话留空
11  
12 Enter same passphrase again:
13  
14 Your identification has been saved in /root/.ssh/id_rsa
15  
16 //生成了私钥/root.ssh/id_rsa
17  
18 Your public key has been saved in /root/.ssh/id_rsa.pub
19  
20 //生成了公钥 /root/.ssh/id_rsa.pub
21  
22 The key fingerprint is:
23  
24 82:8c:2c:52:06:c6:f3:c4:1c:1c:35:b7:cd:5b:42:a2 root@A
25  
26 [root@A ~]# ls /root/.ssh/ //查看一下目录,已经有了这两个文件
27  
28 authorized_keys id_rsa id_rsa.pub known_hosts
29  
30 [root@A ~]#

查看一下服务器ssh服务公钥存放的路径

1 [root@A ~]# cat /etc/ssh/sshd_config |grep AuthorizedKeysFile
2  
3 #AuthorizedKeysFile .ssh/authorized_keys

默认服务器保存公钥的位置是.ssh/AuthorizedKeysFile

所以需要将生成的公钥文件通过scp或者任何方法拷贝到你需要远程登录的服务器的这个文件中

01 [root@B ~]# cd .ssh/
02  
03 [root@B .ssh]# cat id_rsa.pub >authorized_keys
04  
05 //服务器本地ssh客户端默认读取的私钥路径已经包含了/.ssh/id_rsa
06  
07 [root@A .ssh]# cat /etc/ssh/ssh_config |grep IdentityFile
08  
09 # IdentityFile ~/.ssh/identity
10  
11 # IdentityFile ~/.ssh/id_rsa
12  
13 # IdentityFile ~/.ssh/id_dsa

至此你可以直接从A 执行命令ssh ipB

登录到服务器B了,就这么简单。

有一种简单的方法:使用命令ssh-copy-id 直接将本机的key加入到对方服务器的验证文件中

01 [root@A .ssh]# ssh-copy-id
02  
03 Usage: /usr/bin/ssh-copy-id [-i [identity_file]] [user@]machine
04  
05 [root@A .ssh]# ssh-copy-id -i /root/.ssh/id_rsa root@ipB
06  
07 The authenticity of host 'ipB (ipB)' can't be established.
08  
09 RSA key fingerprint is 17:21:32:02:da:3b:a1:d1:a2:69:12:36:a2:d7:59:ad.
10  
11 Are you sure you want to continue connecting (yes/no)? yes
12  
13 root@ipB's password:
14  
15 Now try logging into the machine, with "ssh 'root@ipB'", and check in:
16  
17 .ssh/authorized_keys
18  
19 [root@A ~]# ssh ipB
20  
21 Last login: Sun Dec 15 19:33:07 2013 from ipA
22  
23 [root@B ~]#

这样执行之后,就可以直接登陆服务器了。

上面都是通过linux的ssh客户端来连接,如果通过本地来使用key登陆的话方法大同小异;

将私钥id_rsa下载到本地,然后导入到客户端的密钥认证配置的地方即可;

以SecureCRT为例,配置位置如下图选择下载下来的私钥文件即可。

crt-key

目录
相关文章
|
6天前
|
消息中间件 安全 Unix
SSH配置多台服务器之间的免密登陆以及登陆别名
SSH配置多台服务器之间的免密登陆以及登陆别名
18 1
|
9天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
12天前
|
Linux
linux下搭建tftp服务器教程
在Linux中搭建TFTP服务器,需安装`tftp-server`(如`tftpd-hpa`)。步骤包括:更新软件包列表,安装`tftpd-hpa`,启动并设置开机自启,配置服务器(编辑`/etc/default/tftpd-hpa`),添加选项,然后重启服务。完成后,可用`tftp`命令进行文件传输。例如,从IP`192.168.1.100`下载`file.txt`: ``` tftp 192.168.1.100 <<EOF binary put file.txt quit EOF ```
27 4
|
17天前
|
Linux 数据安全/隐私保护 Windows
aes加密在linux下会生成随机key的解决办法
aes加密在linux下会生成随机key的解决办法
11 2
|
9天前
|
网络协议 Linux
在Linux中,管理和配置网络接口
在Linux中管理网络接口涉及多个命令,如`ifconfig`(在新版本中被`ip`取代)、`ip`(用于网络设备配置)、`nmcli`(NetworkManager的CLI工具)、`nmtui`(文本界面配置)、`route/ip route`(处理路由表)、`netstat/ss`(显示网络状态)和`hostnamectl/systemctl`(主机名和服务管理)。这些命令帮助用户启动接口、设置IP地址、查看连接和路由信息。不同发行版可能有差异,建议参考相应文档。
19 4
|
5天前
|
Linux 数据安全/隐私保护
Linux基础与服务器架构综合小实践
【4月更文挑战第9天】Linux基础与服务器架构综合小实践
952 6
|
17天前
|
Ubuntu Linux 虚拟化
【Linux】ubuntu安装samba服务器
【Linux】ubuntu安装samba服务器
|
17天前
|
存储 固态存储 Unix
Linux中磁盘分区和文件系统管理
在Linux系统中,磁盘是存储数据的物理设备,如HDD或SSD,以文件形式存在于`/dev`目录下,如`sda`、`sdb`等。文件系统定义了如何在磁盘上组织和访问数据,常见的Linux文件系统有ext2、ext3、ext4、xfs等。通过虚拟机软件如VMware,用户可以向Linux虚拟机添加新的硬盘。
|
17天前
|
Linux
Linux安装bind9搭建自己的域名服务器
Linux安装bind9搭建自己的域名服务器
11 0
|
20天前
|
网络协议 Linux 网络安全
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
17 0