[VMware] 使用 ssh-copy-id 免密碼登入 ESXi server
偶爾會需要用 ssh 直接登入 ESXi server,
這時需要打密碼,打久了覺得麻煩,
就想說可以 用 ssh-copy-id 設定免密碼登入 SSH server:
testuser@localhost ~ $ ssh-copy-id root@172.1.2.3 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/testuser/.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 Password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@172.1.2.3'" and check to make sure that only the key(s) you wanted were added.
看起來 ssh-copy-id 運作正常,
有把我在 ~/.ssh/id_rsa.pub 的 public key 帶過去。
但是再次登入 ESXi server,卻還是需要密碼:
testuser@localhost ~ $ ssh root@172.1.2.3 Password:
這真是有點奇怪…
檢查一下 ESXi server 上的 /etc/ssh/sshd_config,
發現了這一行:
AuthorizedKeysFile /etc/ssh/keys-%u/authorized_keys
%u 帶的看起來是使用者的帳號,
所以當使用者是 root 的話,
要認證的 ssh public key 應該是存在 /etc/ssh/keys-root/authorized_keys。
去看了一下 /etc/ssh/keys-root/authorized_keys,
裡面空空如也,而我剛剛用 ssh-copy-id 複製過去的 public key,
則是存在一般的 ~/.ssh/authorized_keys 位置上。
直接把這個檔案複製到 /etc/ssh/keys-root 目錄下:
mv ~/.ssh/authorized_keys /etc/ssh/keys-root/
再重新 ssh,
噹噹~這次就不需要密碼了:
testuser@localhost ~ $ ssh root@172.1.2.3 The time and date of this login have been sent to the system logs. VMware offers supported, powerful system administration tools. Please see www.vmware.com/go/sysadmintools for details. The ESXi Shell can be disabled by an administrative user. See the vSphere Security documentation for more information. ~ #
不知道為什麼 ESXi server 本身的 authorized_keys,
檔案位置和一般Linux 不同,
不過現階段至少是可以用以上方法,解決免密碼登入的問題囉~
(本頁面已被瀏覽過 1,983 次)