快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Linux上实现ssh免密码登陆远程服务器-创新互联

0.说明

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了龙岗免费建站欢迎大家使用!

    平常使用ssh登陆远程服务器时,都需要使用输入密码,希望可以实现通过密钥登陆而免除输入密码,从而可以为以后实现批量自动部署主机做好准备。

    环境如下:


IP地址操作系统
服务器端10.0.0.128/24CentOS 6.5 x86
客户端10.0.0.129/24Ubuntu 16.04 x86


1.客户端生成密钥对

    生成密钥对:

xpleaf@leaf:~$ ssh-keygen -t rsa -b 2048 Generating public/private rsa key pair. Enter file in which to save the key (/home/xpleaf/.ssh/id_rsa):  Created directory '/home/xpleaf/.ssh'. Enter passphrase (empty for no passphrase):  Enter same passphrase again:  Your identification has been saved in /home/xpleaf/.ssh/id_rsa. Your public key has been saved in /home/xpleaf/.ssh/id_rsa.pub. The key fingerprint is: SHA256:eLssyXJLzUCfSN5mu6nqNH9dB/gOyXSvWBwQdNssIYE xpleaf@leaf The key's randomart p_w_picpath is: +---[RSA 2048]----+ |         o=oo    | |        E .o =   | |      o    oo o  | |     + = .o +.   | |      = So = +   | |       B o+ = o  | |    o...=. * o   | |   ..+=..+o o    | |   .o++==        | +----[SHA256]-----+

    查看生成的密钥对:

xpleaf@leaf:~$ ls .ssh id_rsa  id_rsa.pub # id_rsa为私钥,这个一般需要保密;id_rsa.pub为公钥,这个可以公开。


2.上传公钥到服务器端

  使用scp命令操作:

xpleaf@leaf:~$ scp .ssh/id_rsa.pub root@10.0.0.128:/root The authenticity of host '10.0.0.128 (10.0.0.128)' can't be established. RSA key fingerprint is SHA256:0Tpm11wruaQXyvOfEB1maIkEwxmjT2AklWb198Vrln0. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.0.0.128' (RSA) to the list of known hosts. root@10.0.0.128's password:  id_rsa.pub                                                    100%  393     0.4KB/s   00:00

 


3.服务器端操作

    把从客户端传来的公钥添加到.ssh/authorized_keys中:

[root@leaf ~]# cat id_rsa.pub >> .ssh/authorized_keys [root@leaf ~]# chmod 600 .ssh/authorized_keys # authorized_keys的权限需要为600

    修改ssh配置文件/etc/ssh/sshd_config,找到下面一行:

PubkeyAuthentication no

    修改为:

PubkeyAuthentication yes


4.测试

    在客户端上使用密钥登陆到服务器上:

xpleaf@leaf:~$ ssh -i .ssh/id_rsa root@10.0.0.128 Last login: Tue May  9 15:14:01 2017 from 10.0.0.129 [root@leaf ~]#


5.注意事项

  • 在服务器端需要把selinux关闭,否则最后无法使用密钥进行远程登陆;

  • 客户端使用scp命令时,在服务器端也需要安装ssh客户端,否则无法把公钥上传到服务器端,另外也可以使用ssh-copy-id root@10.0.0.128来代替scp操作(这样在服务器端也不需要执行创建.ssh目录等这些操作,即相当于该命令可以一步帮我们完成密钥的上传与配置工作);

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:Linux上实现ssh免密码登陆远程服务器-创新互联
本文URL:http://6mz.cn/article/dehohd.html

其他资讯