Debian 6 的 SSH 公匙免密碼登入小記


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
01. 在自機上生成 RSA 的金鑰

ssh-keygen -t rsa -f ~/.ssh/the_name_of_your_generated_rsa_file

02. 在自機上複制公匙中的內容

cat ~/.ssh/the_name_of_your_generated_rsa_file.pub

03. 以 root 登入主機增加用戶和設置密碼

useradd Your_New_User
passwd Your_New_User

04. 修改主機上的 ssh 端口 和 禁用 root 登入

vim /etc/ssh/sshd_config

Port 22 => Port Your_Port
PermitRootLogin yes => PermitRootLogin no
PasswordAuthentication yes => PasswordAuthentication no
ChallengeResponseAuthentication yes => ChallengeResponseAuthentication no
UsePAM yes

05. 重啟主機的 sshd

/etc/init.d/ssh restart 或 service sshd restart

06. 再開新的連接,用新的用戶 Your_New_User 和密碼登入主機
切到主機上的 Your_New_User 的 ~/.ssh 目錄 (如不存在就自建)

mkdir ~/.ssh && chmod 700 ~/.ssh

07. 在主機貼上剛才複製了的公匙內容到 ~/.ssh/authorized_keys 檔案
(每行記錄都獨立一行開始)

chmod 600 ~/.ssh/authorized_keys
vim ~/.ssh/authorized_keys

08. 改變主機上,剛才的 authorized_keys 權限成唯讀

chmod 400 ~/.ssh/authorized_keys

09. 修改本機的 ~/.ssh/config 檔案,加入以下內容

Host Your_Server_IP_Address
IdentityFile ~/.ssh/id_rsa.the_name_of_your_generated_rsa_file
User Your_New_User
Port Your_Port

10. 再開新的連接,於本機直接嘗試,是否可以免密碼

ssh Your_Server_IP_Address

免密碼登入成功即完成.再測試是否可以再轉為 root

得知目前的身份輸入: id (應可以看見自己的名字)
切換為 root 的方法: su -
得知切換後的身份再輸入: id (應可以看見 root 名字)

=========================================

問題: sshd: unrecognized service
解決: /etc/init.d/ssh restart

問題: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
解決: locale-gen en_US en_US.UTF-8