您好,登录后才能下订单哦!
Linux系统中如何设置SSH免密登录,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
我们先使用usera 登录 serverA 服务器
[root@serverA ~]# su - usera [usera@serverA ~]$ pwd /home/usera
然后在serverA上生成密钥对
[usera@serverA ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/usera/.ssh/id_rsa): Created directory '/home/usera/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/usera/.ssh/id_rsa. Your public key has been saved in /home/usera/.ssh/id_rsa.pub. The key fingerprint is: 39:f2:fc:70:ef:e9:bd:05:40:6e:64:b0:99:56:6e:01 usera@serverA The key's randomart image is: +--[ RSA 2048]----+ | Eo* | | @ . | | = * | | o o . | | . S . | | + . . | | + . .| | + . o . | | .o= o. | +-----------------+
此时会在/home/usera/.ssh目录下生成密钥对
[usera@serverA ~]$ ls -la .ssh 总用量 16 drwx------ 2 usera usera 4096 8月 24 09:22 . drwxrwx--- 12 usera usera 4096 8月 24 09:22 .. -rw------- 1 usera usera 1675 8月 24 09:22 id_rsa -rw-r--r-- 1 usera usera 399 8月 24 09:22 id_rsa.pub
然后将公钥上传到serverB 服务器的,并以userb用户登录
[usera@portalweb1 ~]$ ssh-copy-id userb@10.124.84.20 The authenticity of host '10.124.84.20 (10.124.84.20)' can't be established. RSA key fingerprint is f0:1c:05:40:d3:71:31:61:b6:ad:7c:c2:f0:85:3c:cf. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.124.84.20' (RSA) to the list of known hosts. userb@10.124.84.29's password: Now try logging into the machine, with "ssh 'userb@10.124.84.20'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
这个时候usera的公钥文件内容会追加写入到userb的 .ssh/authorized_keys 文件中
[usera@serverA ~]$ cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA2dpxfvifkpswsbusPCUWReD/mfTWpDEErHLWAxnixGiXLvHuS9QNavepZoCvpbZWHade88KLPkr5XEv6M5RscHXxmxJ1IE5vBLrrS0NDJf8AjCLQpTDguyerpLybONRFFTqGXAc/ximMbyHeCtI0vnuJlvET0pprj7bqmMXr/2lNlhIfxkZCxgZZQHgqyBQqk/RQweuYAiuMvuiM8Ssk/rdG8hL/n0eXjh9JV8H17od4htNfKv5+zRfbKi5vfsetfFN49Q4xa7SB9o7z6sCvrHjCMW3gbzZGYUPsj0WKQDTW2uN0nH4UgQo7JfyILRVZtwIm7P6YgsI7vma/vRP0aw== usera@serverA
查看serverB服务器userb用户下的 ~/.ssh/authorized_keys文件,内容是一样的,此处我就不粘贴图片了。
[userb@serverB ~]$ cat .ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA2dpxfvifkpswsbusPCUWReD/mfTWpDEErHLWAxnixGiXLvHuS9QNavepZoCvpbZWHade88KLPkr5XEv6M5RscHXxmxJ1IE5vBLrrS0NDJf8AjCLQpTDguyerpLybONRFFTqGXAc/ximMbyHeCtI0vnuJlvET0pprj7bqmMXr/2lNlhIfxkZCxgZZQHgqyBQqk/RQweuYAiuMvuiM8Ssk/rdG8hL/n0eXjh9JV8H17od4htNfKv5+zRfbKi5vfsetfFN49Q4xa7SB9o7z6sCvrHjCMW3gbzZGYUPsj0WKQDTW2uN0nH4UgQo7JfyILRVZtwIm7P6YgsI7vma/vRP0aw== usera@serverA
另外我们要注意,.ssh目录的权限为700,其下文件authorized_keys和私钥的权限为600。否则会因为权限问题导致无法免密码登录。我们可以看到登陆后会有known_hosts文件生成。
[useb@serverB ~]$ ls -la .ssh total 24 drwx------. 2 useb useb 4096 Jul 27 16:13 . drwx------. 35 useb useb 4096 Aug 24 09:18 .. -rw------- 1 useb useb 796 Aug 24 09:24 authorized_keys -rw------- 1 useb useb 1675 Jul 27 16:09 id_rsa -rw-r--r-- 1 useb useb 397 Jul 27 16:09 id_rsa.pub -rw-r--r-- 1 useb useb 1183 Aug 11 13:57 known_hosts
这样做完之后我们就可以免密码登录了
[usera@serverA ~]$ ssh userb@10.124.84.20
另外,将公钥拷贝到服务器的~/.ssh/authorized_keys文件中方法有如下几种: 1、将公钥通过scp拷贝到服务器上,然后追加到~/.ssh/authorized_keys文件中,这种方式比较麻烦。scp -P 22 ~/.ssh/id_rsa.pub user@host:~/。 2、通过ssh-copy-id程序,就是我演示的方法,ssh-copyid user@host即可 3、可以通过cat ~/.ssh/id_rsa.pub | ssh -p 22 user@host ‘cat >> ~/.ssh/authorized_keys’,这个也是比较常用的方法,因为可以更改端口号。
Linux是一种免费使用和自由传播的类UNIX操作系统,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统,使用Linux能运行主要的Unix工具软件、应用程序和网络协议。
看完上述内容,你们掌握Linux系统中如何设置SSH免密登录的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。