CẤU HÌNH SSH KEY-BASED AUTHENTICATION TRÊN CENTOS 7
Link hướng dẫn:
/>
SSH Server details:
OS : CentOS 7 64-bit
IP address : 192.168.28.149/24
SSH client details:
OS : Ubuntu 16.04 LTS 64-bit
IP address : 192.168.28.139/24
Lưu ý quan trọng: Không tạo ra cặp khóa với quyền root, vì chỉ có root mới có thể sử dụng các
khóa này. Tạo cặp khóa với quyền user bình thường.
TẠO KHÓA TẠI CLIENT
Tại máy client:
tuananh@ubuntu:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tuananh/.ssh/id_rsa):
Created directory '/home/tuananh/.ssh'.
Enter passphrase (empty for no passphrase): 123456
Enter same passphrase again: 123456
Your identification has been saved in /home/tuananh/.ssh/id_rsa.
Your public key has been saved in /home/tuananh/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cwCpDV8guJqd3kEXAEhPK+7Nn8ckKwrBGwSN3STGCQs tuananh@ubuntu
The key's randomart image is:
+---[RSA 2048]----+
|EO+B+.oo
|
|=oO.+.o..
|
|.o + = o.
|
|+ o o + .
|
|.B o . S .
|
|+.B .. . o
|
|.+ + .=
|
|. ..o..o
|
| .. .o.
|
+----[SHA256]-----+
Trong trường hợp bạn đã tạo cặp khóa, bạn sẽ thấy thông báo sau. Chỉ cần gõ "y" để tạo ghi đè
lên khoá hiện tại.
/home/username/.ssh/id_rsa already exists.
Overwrite (y/n)?
Khóa đã tạo thành công ở client, copy public key cho SSH bằng lệnh:
tuananh@ubuntu:~$ ssh-copy-id
The authenticity of host '192.168.28.149(192.168.28.149)' can't be
established.
ECDSA key fingerprint is
SHA256:0UYB6aehcNKmgtBWj6KMpW2sF8582kam9M4PvqT7NhI.
Are you sure you want to continue connecting (yes/no)? yes
/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
's password: mật khẩu user root của Server
Number of key(s) added: 1
Now try logging into the machine, with:
"ssh
''"
and check to make sure that only the key(s) you wanted were added.
hoặc:
cat ~/.ssh/id_rsa.pub | ssh root@Server_IP_Address "cat >>
~/.ssh/authorized_keys"
CẤU HÌNH TẠI SERVER
Vì đang cấu hình SSH Key-base Authentication nên chúng ta sẽ bỏ chế độ Password-based
Authentication. Sửa file cấu hình SSH tại Server:
vi /etc/ssh/sshd_config
Sửa dòng sau:
PasswordAuthentication no
PubkeyAuthentication yes
Khởi động lại dịch vụ SSH tại server:
service sshd restart
Kết nối tới SSH Server từ client
ssh
123456 (mật khẩu khi ta phát sinh khóa)
Thử SSH vào SSH Server với user3.
ssh
Lý do không SSH vào user3 được: SSH Server đang ở chế độ Key-base Authentication, không cho
phép đăng nhập bằng Password. Và user3 (trên server) chưa được cấu hình để chấp nhận public
key từ tuananh@ubuntu.
Tạo thêm khóa để có thể đăng nhập Server bằng các user khác
Trên server:
Add The New User if not exist
useradd user3
passwd user3
Create Directories and Files
mkdir -p /home/user3/.ssh
touch /home/user3/.ssh/authorized_keys
Set Proper Permissions
chown user3:user3 /home/user3/.ssh
chown user3:user3 /home/user3/.ssh/authorized_keys
chmod 700 /home/user3/.ssh
chmod 600 /home/user3/.ssh/authorized_keys
Copy public key từ client :
Setup SSH Keys for myuser
echo "ssh-rsa ...." >> /home/user3/.ssh/authorized_keys
service sshd restart
Tại client thành công: