Bạn có thể đăng nhập vào server Linux từ xa không cần nhập mật khẩu chỉ trong 3 bước đơn giản dùng ssh-keygen và ssh-copy-id như bài viết này giải thích.
ssh-keygen tạo cặp khóa công khai và riêng tư. ssh-copy-id copy khóa công khai của local-host vào file authorized_keys của remote-host. ssh-copy-id cũng gán quyền phù hợp cho home, ~/.ssh, và ~/.ssh/authorized_keys của remote-host.
Bài này cũng giải thích 3 phiền toái nhỏ của ssh-copy-id và cách dùng ssh-copy-id cùng ssh-agent.
Bước 1: Tạo Khóa Công Khai Và Riêng Tư Bằng ssh-keygen Trên Local-Host
jsmith@local-host$ [Lưu ý: Bạn đang ở local-host] jsmith@local-host$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Nhấn Enter] Enter passphrase (empty for no passphrase): [Nhấn Enter] Enter same passphrase again: [Nhấn Enter] Your identification has been saved in /home/jsmith/.ssh/id_rsa. Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. The key fingerprint is: 33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host
Bước 2: Copy Khóa Công Khai Sang Remote-Host Bằng ssh-copy-id
jsmith@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host jsmith@remote-host's password: Now try logging into the machine, with "ssh 'remote-host'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
Lưu ý: ssh-copy-id thêm vào (append) khóa vào file .ssh/authorized_key của remote-host.
Bước 3: Đăng Nhập Remote-Host Không Nhập Mật Khẩu
jsmith@local-host$ ssh remote-host Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 [Lưu ý: SSH không hỏi mật khẩu.] jsmith@remote-host$ [Lưu ý: Bây giờ bạn ở remote-host]
3 bước đơn giản trên sẽ giải quyết vấn đề trong hầu hết trường hợp.
Chúng ta cũng đã thảo luận chi tiết trước về thực hiện SSH và SCP từ openSSH sang openSSH không nhập mật khẩu.
Nếu bạn dùng SSH2, chúng ta đã thảo luận trước về SSH và SCP không mật khẩu từ SSH2 sang SSH2, từ OpenSSH sang SSH2 và từ SSH2 sang OpenSSH.
Dùng ssh-copy-id Cùng ssh-add/ssh-agent
Khi không truyền giá trị cho tùy chọn -i VÀ ~/.ssh/identity.pub không có, ssh-copy-id sẽ báo lỗi sau.
jsmith@local-host$ ssh-copy-id -i remote-host /usr/bin/ssh-copy-id: ERROR: No identities found
Nếu bạn đã nạp khóa vào ssh-agent dùng ssh-add, thì ssh-copy-id sẽ lấy khóa từ ssh-agent để copy sang remote-host. Tức là, nó copy khóa từ lệnh ssh-add -L sang remote-host, khi bạn không truyền tùy chọn -i cho ssh-copy-id.
jsmith@local-host$ ssh-agent $SHELL jsmith@local-host$ ssh-add -L The agent has no identities. jsmith@local-host$ ssh-add Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa) jsmith@local-host$ ssh-add -L ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79DyBV aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa jsmith@local-host$ ssh-copy-id -i remote-host jsmith@remote-host's password: Now try logging into the machine, with "ssh 'remote-host'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [Lưu ý: Đã thêm khóa hiển thị bởi ssh-add -L]
Ba Phiền Toái Nhỏ Của ssh-copy-id
Dưới đây là một vài phiền toái nhỏ của ssh-copy-id.
- Khóa công khai mặc định: ssh-copy-id dùng ~/.ssh/identity.pub làm khóa công khai mặc định (khi không truyền giá trị cho tùy chọn -i). Tác giả mong nó dùng id_dsa.pub, hoặc id_rsa.pub, hoặc identity.pub làm mặc định. Tức là nếu một trong ba tồn tại, nó copy cái đó sang remote-host. Nếu hai hoặc ba tồn tại, nó copy identity.pub làm mặc định.
- The agent has no identities: Khi ssh-agent đang chạy và ssh-add -L trả về "The agent has no identities" (tức không khóa nào thêm vào ssh-agent), ssh-copy-id vẫn copy thông báo "The agent has no identities" vào entry authorized_keys của remote-host.
- Entry trùng lặp trong authorized_keys: Tác giả mong ssh-copy-id kiểm tra trùng lặp trên authorized_keys của remote-host. Nếu chạy ssh-copy-id nhiều lần trên local-host, nó sẽ giữ thêm cùng khóa vào authorized_keys của remote-host mà không kiểm tra trùng. Dù entry trùng mọi thứ vẫn chạy ngon. Nhưng tác giả muốn file authorized_keys sạch sẽ.
Nếu bạn thích bài này, hãy bookmark nó trên Delicious và Stumble it.