2013年7月21日 星期日

[ssh] How to use ssh command without PASSWORD

How to use ssh command without PASSWORD
The picture was copy from ibm.



Quick Command:
ssh-copy-id user@remote_server



這份文件會告訴你
如何把你的 private key 加入系統, 作為認證使用? (ssh-add command)
.ssh/authorized_keys 是作什麼用的? (放 public key which are used to identify the client)
.ssh/known_hosts 的目的是什麼? (for connected hosts)
id_rsa 與 id_rsa.pub 到底可不可以刪除?


用 ssh (secure shell) 機制, 不僅可以省去輸入密碼的不方便, 最重要的功能在於, 中間的傳輸過程全面加密, 使得有心人士將無法攔截你的傳輸訊息. 另外, 在全面自動化機制的建置上, 也有方式可以取代直接輸入password, 你應該不想半夜三點鐘, 還在終端機前面輸入密碼吧! ^_^


若不用輸入密碼, 那一定是有甚麼東西取代了 password. 沒錯! 這次我們就是使用 RSA public key 與 private key 來取代 password.

現在很多網站透過 OAuth 機制, 讓你可以使用 Google 或 Facebook 帳號所發的憑證來登入新的服務, 其中連註冊資訊都可自動從第三方取得. 使用者不用費事再輸入基本資訊. 使用者還可以在 Google 或 FB 直接設定授予憑證的權限. 這種借用第三方認證隨時限縮或擴大權限的機制, 實在是比交出整把鑰匙有彈性的多. 對使用者而言, 只是點選登入方式, 完全不用輸入密碼. 這種方式已經廣泛的應用在各項的雲服務中, 大量用在人與雲服務之間的註冊與認證關係, 其間認證的建立, 則是透過已經授權的第三方(FB or Google) 來給予認證. 使用者完全不需要再輸入 password 以及基本資訊, 大大簡化了流程-- 在輸入不易的行動裝置雲時代, OAuth 可說是必需要了解的東西.

然而, 我們今天討論的主題是: 完全自動化無人的安全(secure)連線上, 使用 RSA public/private key pair 機制取代 password. 討論的是主機與主機之間的認證關係.

概念: 若 想要在 remote server 上執行遠端指令, 必須要把自己的 private key 加入 ssh 認證機制中 (ssh-add [private key]), 而 remote server 則必須要取得 public key, 並把它放到 .ssh/authorized_keys 中. 整個程序透過 ssh 機制保證安全.


Remote server 藉由擁有合法的 public key 來檢驗, 想要連過來的 client 是不是合法的機器. 所以一般來說,  client 會在公開的地方放上它的 public key (如何驗證公開場合的 public key 是正確的/合法的key, 則最好是從可信賴公開場合取 key), remote server 取得 public key 之後, 放到 authorized_keys 檔案中. 這個檔案存放了一堆合法的 public key.  slave 利用 ssh protocol 證明連進來的 server 身分.


若一切都設定好時, 第一次連線時,  你會遇到下面的問題.


Are you sure you want to continue connecting (yes/no)?
這是因為第一次連線建立時, ssh 會先檢查這個 host 之前是否連線過? if no, 則會詢問使用者是否真的要連線 (你需要檢查提供這個 server public key 的單位, 真的是你要連線的機器嗎? ex: 網路上隨便剪貼的台灣銀行 public key, 你真的相信那真的是台灣銀行的 public key 嗎?). 當你檢查完畢, 沒問題這個 host 確實是你要連線的機器, 則按 yes. ssh 系統會幫你把這個新的 host 記在 .ssh/known_hosts




 讓我們繼續看下去.


If you want to execute command on the remote server, you should follow the commands:

You
Step 1: Create Private Key and Public Key
 [You]:~$ ssh-keygen -t rsa -C "The access key for the slave Jing"
(note: Please enter passphrase)
Output:
 private key: id_rsa         # as an identity for the master, keep in master/server
 public key: id_rsa.pub    # public to the net for identify the master self




Check the rsa key pair

.



Step 2: Add the private key identities to the authentication agent.
關鍵: the agent will proves to the slave that it knows the associated private key

master: ~$ ssh-add .ssh/id_rsa




[Remote Server]
Step 3: Setup the public key
關鍵: 想要連進來的人, 就必須要把他的 public key 放到 authorized_keys 檔案中
slave:~$ cat id_rsa.pub >> authorized_keys
slave:~$ chmod 600 authorized_keys    # make sure 600




Q: What’s happen, if you delete the authorized_keys on slave?
When you get access to the remote server, your host's tty will prompt a password input message.
ex:
   ssh slave@172.20.10.2 uname -a



Done!
Now, the remote server is ready to received the certified master’s command.



Test: run a remote task on the remote server
The First Run:
1. System do not known the remote server that you want to connect. It will prompt you to ensure the connection..
2. If yes, ssh protocol will save the host to the .ssh/known_hosts.
3. know_hosts 放在哪裡? 放在要執行連線的那台機器上. 以這個例子來說, 是放在 master




The Second Run
  • 因為 master 已經有 known_hosts 了, 所以不用再問一次. 而且也不需要密碼了.


重要指令與檔案:
  • [private id key] ssh-add [private key]: 任何想要連別人的, 都要利用這個指令, 把自己的 private key id 加進 ssh 機制.
  • [public key] .ssh/authorized_keys: 任何要連進來的人, 都要把他的 public key 附加 (append) 到 remote server 機器的 authorized_keys, 否則一律會問密碼
  • .ssh/known_hosts: 任何連線過的機器, 都會被記錄起來.
  • id_rsa 與 id_rsa.pub: 操作完上面兩個步驟, 基本上沒用了可以刪除. 除非還想要連其他的 remote server, 則要保留 id_rsa.pub 檔. 給未來的 slave 用.

所以整理一下, 重點!! 
Quick Guide:
If you want to execute command that is run on the remote server, you should follow the commands.


Step 1: 建立 RSA Key Pair
ssh-keygen -t rsa -C "The access key for the slave Jing"


Step 2: 把 private key 加入 ssh 機制
ssh-add .ssh/rsa_id


Step 3: 把 public key 加入 Remote server's authorized_keys 檔案中
cat rsa_id.pub >>.ssh/authorized_keys



References:
  1. Roger Hill, "Getting started with SSH security and configuration",http://www.ibm.com/developerworks/aix/library/au-sshsecurity/.
  2. Kam, "Run Commands Remotely via SSH with No Password", http://www.dotkam.com/2009/03/10/run-commands-remotely-via-ssh-with-no-password/.