日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ssh 使用密匙登录服务器

發布時間:2023/12/31 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ssh 使用密匙登录服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

ssh 有2種認證方式:一種是密碼認證,一種是密匙認證。

然而密碼認證的方式是比較不安全的。首先如果密碼強度不是很高的話很容易就被別人猜出來了(當然不是用人腦猜),其次是密碼在傳輸的過程中容易被別人劫持(專業術語叫 中間人攻擊)。詳細的ssh認證過程可以看這個帖子,介紹的比較全。(傳送門)

對于運維人員來說管理大量的服務器人工輸入密碼是不現實的,將密碼寫在登錄腳本里也是不可取的。那么使用密匙登錄服務器是比較好的做法。

?

場景: server1(127.0.0.1), server2(192.168.0.6), server3(192.168.0.7)

1. 在server1服務器上面使用命令 ssh-keygen 生成公鑰私鑰。

bingaos-MacBook-Pro:~ bingao$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/Users/bingao/.ssh/id_rsa): /Users/bingao/.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 /Users/bingao/.ssh/id_rsa. Your public key has been saved in /Users/bingao/.ssh/id_rsa.pub. The key fingerprint is: SHA256:tm55gD9g9rv3X3+LOnkn4FP95SCJb0Wl1Gu2zt3wm0Y bingao@bingaos-MacBook-Pro.local The key's randomart image is: +---[RSA 2048]----+ | . | | . o| | . o.| | o+ | | .S . o+ .| | =.....o.+E.| | o +.o..+o+=*| | .* o=oo.*X| | .+* +*o=+=| +----[SHA256]-----+ bingaos-MacBook-Pro:~ bingao$

-t rsa 表示 使用rsa方式加密,其他加密方式還有 dsa, ecdsa, ed25519

由于我這里已經生成過,所以提示 overwrite (y/n)

Enter passphrase 輸入口令,最好輸一下,我這里沒有輸入。

?

2. 生成完之后可以在用戶目錄的 .ssh 文件夾下面看到2個文件: id_rsa, id_rsa.pub

bingaos-MacBook-Pro:~ bingao$ ll ~/.ssh/ total 96 -rw------- 1 bingao staff 1679 Dec 7 15:53 id_rsa -rw-r--r-- 1 bingao staff 414 Dec 7 15:53 id_rsa.pub -rw-r--r-- 1 bingao staff 22799 Dec 7 15:52 known_hosts bingaos-MacBook-Pro:~ bingao$

id_rsa 是私鑰,內容類似

-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEArdh52+aPhalePduymkPWVSI6odvf/DVfxkVEpm4Kl9TuB7o2 ... 640rfbph0AflsPhjIhDu/a/TdVtLYBgkR4ENTvnCNC7YLRA+GHXGsaVnooWvQQjW KDB//FarJuEf9386hW/Xp9/0+FnQxSY+WHD0ULsDdLS2PeHkd44dBw== -----END RSA PRIVATE KEY-----

id_rsa.pub 是公鑰,內容類似

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCt2Hnb5o+FqV4927KaQ9ZVIjqh2...P4TfovAyHfafboYz2JopyoUU3L12Q2xDB4wjAA7R+Eccbx/fwxwOIL0hcru/OMSmpaxMY5G2Z4+d bingao@bingaos-MacBook-Pro.local

?

3.將公鑰(id_rsa.pub)上傳到目標服務器(server2)上面

bingaos-MacBook-Pro:~ bingao$ scp ~/.ssh/id_rsa.pub root@192.168.0.6:~ root@192.168.0.6's password: id_rsa.pub 100% 414 73.4KB/s 00:00 bingaos-MacBook-Pro:~ bingao$

?

4.在目標服務器上講公鑰寫入用戶目錄下 .ssh 文件夾下面的?authorized_keys 文件內(如沒有則創建)

[root@localhost ~]# cat ~/id_rsa.pub >> ~/.ssh/authorized_keys -bash: /root/.ssh/authorized_keys: No such file or directory [root@localhost ~]# mkdir ~/.ssh [root@localhost ~]# cat ~/id_rsa.pub >> ~/.ssh/authorized_keys [root@localhost ~]# ll ~/.ssh/ total 4 -rw-r--r--. 1 root root 414 Dec 7 16:21 authorized_keys [root@localhost ~]#

?

5.將 authorized_keys 文件權限設置為 600

[root@localhost ~]# chmod 600 ~/.ssh/authorized_keys [root@localhost ~]# ll ~/.ssh/ total 4 -rw-------. 1 root root 414 Dec 7 16:21 authorized_keys [root@localhost ~]#

?

配置完成,接下來就可以直接登錄了

bingaos-MacBook-Pro:~ bingao$ ssh root@192.168.0.6 Last login: Thu Dec 7 16:26:06 2017 from 192.168.1.164 [root@localhost ~]#

因為server2已經持有server1的公鑰,所以直接通過密匙認證成功。從認證原理來看?用戶將自己的公鑰儲存在遠程主機上。登錄的時候,遠程主機會向用戶發送一段隨機字符串,用戶用自己的私鑰加密后,再發回來。遠程主機用事先儲存的公鑰進行解密,如果成功,就證明用戶是可信的,直接允許登錄shell,不再要求密碼。也就是說凡是持有這個私鑰的用戶都能夠直接登錄遠程服務器。

將server1的私鑰發送到server3上面

bingaos-MacBook-Pro:~ bingao$ scp ~/.ssh/id_rsa john@192.168.0.7:~ john@192.168.0.7's password: id_rsa 100% 1679 364.5KB/s 00:00 bingaos-MacBook-Pro:~ bingao$

?

在server3上面使用命名?ssh -i ~/id_rsa root@192.168.0.6

[john@localhost ~]$ ssh -i ~/id_rsa root@192.168.0.6 The authenticity of host '192.168.0.6 (192.168.0.6)' can't be established. ECDSA key fingerprint is 23:c5:0e:6b:3c:26:0c:e9:31:21:12:1f:d8:b5:60:a2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.6' (ECDSA) to the list of known hosts. Last login: Thu Dec 7 16:28:02 2017 from 192.168.1.164 [root@localhost ~]#

由于是第一次登錄,提示是否信任遠程主機的簽名,輸入 yes,無需輸入密碼即可登錄。

第二次登錄直接進入服務器

[john@localhost ~]$ ssh -i ~/id_rsa root@192.168.0.6 Last login: Thu Dec 7 16:39:15 2017 from 10.2.0.130 [root@localhost ~]#

?

所以運維人員有義務保證私鑰的安全,避免在開發人員之間隨意傳播。

?

轉載于:https://my.oschina.net/u/232911/blog/1586450

總結

以上是生活随笔為你收集整理的ssh 使用密匙登录服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。