普通使用者SSH無密碼互信建立 出現的問題以及解決辦法
配置前提醒:.ssh目錄不需要手動建立!命令會自動完成這一切。
當你看見這篇文章時,假設你已經安裝好了ssh相關服務。
作為普通使用者,用sudo許可權去修改配置檔案/etc/ssh/sshd_config
$ sudo vim /etc/ssh/sshd_config
...
RSAAuthentication yes # 啟用 RSA 認證
PubkeyAuthentication yes # 啟用公鑰私鑰配對認證方式
AuthorizedKeysFile %h/.ssh/authorized_keys # 公鑰檔案路徑
...
重啟SSH服務(不同版本有不同的重啟方式):
Debian / Ubuntu
$ sudo service ssh restart
or
$ /etc/init.d/ssh restart
CentOS / RHEL / Fedora / Redhat
$ /etc/init.d/sshd restart
or
$ service sshd restart
建立私鑰和公鑰
建立金鑰對時,一定不要在命令前加sudo
建立金鑰對時,一定不要在命令前加sudo
建立金鑰對時,一定不要在命令前加sudo
# -t 指定型別,-f指定檔案生成路徑,-P指定檔案密碼(''表示無密碼)
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
Generating public/private rsa key pair.
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:sRWB6v9sTYvBAJVc40QFZs49JQuQVkPjB6fRc7xETDM user@192.168.1.212
The key's randomart image is:
+---[RSA 2048]----+
| ooX^=+=E |
| . *O %o+=o |
| +. B =+ . |
| . .+ . .. |
| . So |
| . o . |
| . = . |
| ..o o |
| oo |
+----[SHA256]-----+
此時生成了一對金鑰
公鑰:id_rsa.pub
金鑰:id_rsa
將生成的公鑰(id_ras.pub)拷貝至另外一臺伺服器
$ ssh-copy-id -i .ssh/id_rsa.pub user@192.168.1.213
...
Are you sure you want to continue connecting (yes/no)? yes
...
user@192.168.1.213's password: #這裡提示輸入密碼
...
Now try logging into the machine, with: "ssh 'user@192.168.1.213'"
and check to make sure that only the key(s) you wanted were added.
拷貝成功,測試連線
$ ssh 192.168.1.213 'ifconfig'
ens33 Link encap:Ethernet HWaddr 00:0c:29:c6:7e:6b
inet addr:192.168.1.213 Bcast:192.168.1.255 Mask:255.255.255.0
...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
...
連線成功,則對另外一臺伺服器主機進行相同的操作,完成互信無密碼連線。
注意:如果建立公鑰私鑰的時候,加上了sudo
,比如像這樣:
$ sudo ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
系統會給以下提示:
Generating public/private rsa key pair.
Saving key "/home/****/.ssh/id_rsa" failed: No such file or directory
提示沒有這個檔案或目錄
網上一些解答說,建立一個目錄就好了,然後就去這樣做
$ mkdir ~/.ssh
然後再來一次生成公鑰私鑰:
$ sudo ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
Generating public/private rsa key pair.
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/OxKIyjs01Z9yWae/Bnke1wQF1WMDSLmKH7XFQK3qyA root@192.168.1.212
The key's randomart image is:
+---[RSA 2048]----+
| +.+ o**|
| + o +o.+|
| . . . . .o |
| . o . o. |
| E.S...+ . |
| . ..o.=*+ .|
| o.... o*+.o. . |
| ...o o o+ +o |
| .o ....+. |
+----[SHA256]-----+
一看結果,成功!
但是。。。 使用者此時會把公鑰遠端傳送給另外一臺伺服器上,像這樣:
$ ssh-copy-id -i .ssh/id_rsa.pub user@192.168.1.213
/usr/bin/ssh-copy-id: ERROR: failed to open ID file '.ssh/id_rsa': Permission denied
(to install the contents of '.ssh/id_rsa.pub' anyway, look at the -f option)
許可權不夠?加上sudo吧。。。
$ sudo ssh-copy-id -i .ssh/id_rsa.pub user@192.168.1.213
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
user@ip's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'user@192.168.1.213'"
and check to make sure that only the key(s) you wanted were added.
心中默唸:完美!公鑰已經成功拷貝到伺服器,可以免密遠端登入了!接著就開心的去連線…
$ ssh 192.168.1.213
Load key "/home/user/.ssh/id_rsa": Permission denied
user@192.168.1.213's password:
什麼?居然還要我輸入密碼!!!不能忍。
接著各種查資料,然而毫無進展。。。
接著檢視連線過程是不是出了錯:
$ ssh -vvv 192.168.1.213
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "192.168.1.213" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 192.168.1.213 [192.168.1.213] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/user/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/user/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/user/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/user/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
...
...
去google搜尋半天也沒任何結果。
就當準備放棄的時候,用ll -a ~/.ssh/
檢視了一下.ssh目錄下的檔案們….
$ ll -a ~./ssh/
-rw------- 1 root root 1.7K Oct 4 00:45 id_rsa
-rw-r--r-- 1 root root 406 Oct 4 00:45 id_rsa.pub
!!!居然我這個user使用者建立的檔案變成了root所有!好吧,把屬於我的奪回來:
$ sudo chown user /home/user/.ssh/id_rsa
$ sudo chgrp user /home/user/.ssh/id_rsa
再試一次連線
$ ssh 192.168.1.213
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-87-generic x86_64)
...
...
$
這下終於成功了。
雖說成功了,但是繞了好大的一個圈子,哎,人生苦短,千萬不要因為一些小細節,浪費掉自己大把時間。
寫這篇部落格的目的在於,希望能夠幫助一些遇到類似問題的朋友,雖然說到最後問題看似簡單,但往往bug就出現在一些很簡單的細節問題上,難以察覺。細節決定成敗。。。
總結:多注意細節上的東西,時間就是生命,多總結,多積累。
相關文章
- discuz資料庫搬家,改密碼後無法訪問解決辦法資料庫密碼
- 雲伺服器解決“MySQL忘記密碼”問題的辦法伺服器MySql密碼
- Linux 作業系統配置互信認證後,登入仍然需要輸入使用者密碼的解決辦法Linux作業系統密碼
- Authentication failure 以及xxx is not in the sudoers file 問題的解決辦法AI
- 【Salesforce問題解決】Dataloader匯出的檔案亂碼或者問號的解決辦法Salesforce
- 在IDEA下使用JUnit出現的問題與解決辦法Idea
- LoadRunner中文出現亂碼的解決辦法
- 你遇到過的相容性問題以及解決辦法
- iPhone忘記訪問限制密碼的解決辦法 密碼忘記了怎麼辦?iPhone密碼
- wordress小程式圖片訪問出現404問題解決辦法
- OpenWrt 無法透過 ssh 免密碼方式訪問 git 伺服器的原因及解決方案密碼Git伺服器
- 訊息通道路由出現 403 問題的最好解決辦法路由
- RCmongodb出現id重複問題的簡單解決辦法jztMongoDB
- 一個ssh無法遠端登入的問題跟蹤解決
- Macbook Pro Big Sur出問題解決辦法Mac
- 解決安裝LR後提示內部出現錯誤,無法建立資料夾的問題
- Windows無法配置此無線連線這個問題的解決辦法Windows
- 關於網路硬體配置出現問題,無法上網問題的解決
- WSL中配置EDA環境:遇到的問題以及解決辦法
- 10G密碼版本使用者無法登陸問題密碼
- 常見的HTTP介面超時問題出現原因及解決辦法HTTP
- 密碼過期引起的ssh無法登陸密碼
- 解決無法使用VI的問題
- Laradock 下建立 storage:link 軟連線資源無法訪問的解決辦法
- Ansible批量更新遠端主機使用者密碼 (包括Ansible批量做ssh互信)密碼
- MySQL組複製的幾個常見問題以及解決辦法MySql
- Android開發過程中遇到的問題以及解決辦法 how toAndroid
- 解決 Python 指令碼無法生成結果的問題Python指令碼
- 批量實現多臺伺服器之間ssh無密碼登入的相互信任關係伺服器密碼
- 跨域問題解決辦法跨域
- K8Sdashboard登入問題(chrome無法訪問以及使用使用者名稱和密碼登入)K8SChrome密碼
- uniapp的h5專案更版出現白屏的問題解決辦法APPH5
- java多執行緒程式設計問題以及解決辦法Java執行緒程式設計
- 生產內網ssh登陸變慢問題原因及解決辦法內網
- 解決Mysql資料庫插入資料出現問號(?)的解決辦法MySql資料庫
- Python專案,VS Code控制檯輸出亂碼問題解決辦法Python
- flashfxp 亂碼,2種辦法解決flashfxp 亂碼問題
- git出現Permission denied的解決辦法Git
- 關於SSH免密登陸普通使用者