Bảo mật 2 lớp SSH với Authentication trên Server/Orange Pi

Bài này sẽ thiết lập xác thực hai lớp SSH bằng Google Authenticator hoặc FreeOTP. Nó giúp tăng tính bảo mật của dịch vụ SSH trên Server.

 

Cài, cấu hình Google Authenticator

Đăng nhập SSH vào Server chạy lệnh sau để cài đặt Google Authenticator.

$ sudo apt install -y libpam-google-authenticator

 Sau đó, chạy google-authenticator để tạo khóa bí mật mới.

$ google-authenticator

 

authenticator 1

Khi được hỏi “Do you want authentication tokens to be time-based (y/n)” chọn  y .

 

authenticator 2

Trong terminal sẽ xuất hiện mã QR có thể quét bằng ứng dụng TOTP trên điện thoại.

Tếp theo sẽ thấy secret key, verification code, emergency scratch code. Bạn nên lưu vào một nơi khác phòng sử dụng sau này. Nếu muốn thay đổi khóa bí mật, chỉ cần đăng nhập vào Server của mình và chạy google-authenticator lại lệnh để cập nhật ~/.google_authenticator file.

Nhập ” y ” để trả lời tất cả câu hỏi còn lại, thao tác này sẽ cập nhật tệp cấu hình Google Authenticator, vô hiệu hóa sử dụng cùng một mã để xác thực, tăng thời gian bật terminal, giới hạn, bảo vệ khỏi các đăng nhập.

 

Cấu hình SSH daemon để sử dụng Google Authenticator

Nếu sử dụng mật khẩu cho SSH.

$ sudo nano /etc/ssh/sshd_config

Tìm hai tham số sau và sửa thành yes .

UsePAM yes

ChallengeResponseAuthentication yes

Nếu muốn dùng cho tài khoản root, tìm PermitRootLogin thay thành yes. Ngược lại PermitRootLogin no hoặc PermitRootLogin prohibit-password.

PermitRootLogin yes

 

Mở file SSH daemon.

$ sudo nano /etc/pam.d/sshd

Tiếp tục thêm hai dòng sau vào.

# two-factor authentication via Google Authenticator

auth   required   pam_google_authenticator.so

Lưu và restart lại SSH daemon để thay đổi có hiệu lực.

$ sudo systemctl restart ssh

authenticator 7

 

Nếu sử dụng SSH Key để đăng nhập.

Mở file cấu hình SSH.

$ sudo nano /etc/ssh/sshd_config

Tìm hai tham số sau và đổi cả hai thành yes.

UsePAM yes

ChallengeResponseAuthentication yes

Nếu bạn muốn cho phép người dùng root sử dụng.

PermitRootLogin yes

Thêm dòng sau vào cuối file.

AuthenticationMethods publickey,keyboard-interactive

 

Chỉnh tiếp file SSH daemon.

$ sudo nano /etc/pam.d/sshd

Thêm hai dòng sau.

# two-factor authentication via Google Authenticator

auth   required   pam_google_authenticator.so

Lưu và restart lại SSH daemon để thay đổi có hiệu lực.

$ sudo systemctl restart ssh

Từ bây giờ cần sử dụng khóa SSH và mã xác minh Google Authenticator để đăng nhập.

 

Cách tắt xác thực hai yếu tố SSH.

$ sudo nano /etc/pam.d/sshd

Nhập dòng sau dòng sau.

auth required pam_google_authenticator.so

Lưu vào thoát file. Nếu đã thêm dòng sau vào file “/etc/ssh/sshd_config”

AuthenticationMethods publickey,keyboard-interactive

Xóa xác thực keyboard-interactive.

AuthenticationMethods publickey

Lưu và khởi động lại SSH daemon.

$ sudo systemctl restart ssh

chúc các bạn ngon miệng.

 

0 0 đánh giá
Article Rating
Theo dõi
Thông báo của
guest
0 Comments
Phản hồi nội tuyến
Xem tất cả bình luận
0
Rất thích suy nghĩ của bạn, hãy bình luận.x