CÁCH SSH VÀO SERVER LINUX KHÔNG CẦN PASSWORD

SSH (Secure Socket Shell) là một giao thức được sử dụng để đăng nhập một máy tính từ xa.
Thông thường, người dùng hay sử dụng password để chứng thực khi SSH. Việc này rất có lợi cho hacker dễ dàng quét mật khẩu để truy cập vào hệ thống nếu người dùng đặt mật khẩu quá đơn giản.
Vì thế hôm nay TEL4VN sẽ hướng dẫn mọi người cách ssh vào server linux dùng key, không cần password.

1. Cài đặt SSH server:

Trên CentOS:

OpenSSH đã được cài đặt mặc định khi cài đặt hệ điều hành CentOS lên server. Vì thế không cần cài đặt thêm package mới.

Trên Debian:

root@tel4vn:~# aptitude -y install ssh
root@tel4vn:~# vi /etc/ssh/sshd_config

# line 28: change to no (prohibit root login
# default setting “without-password” means that root login is permited but require key-pair authentication)

PermitRootLogin no

root@tel4vn:~# systemctl restart ssh

2. Tạo Key trên Server:

root@tel4vn:~# ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/root/.ssh/id_rsa): # Enter
Enter passphrase (empty for no passphrase): # set passphrase (set no passphrase to Enter with empty)
Enter same passphrase again:
Your identification has been saved in /home/root/.ssh/id_rsa.
Your public key has been saved in /home/root/.ssh/id_rsa.pub.
The key fingerprint is:
30:ee:f0:47:38:9d:8a:24:5e:01:c8:13:df:15:40:b4 root@tel4vn
The key’s randomart image is:

root@tel4vn:~# mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys 
root@tel4vn:~# chmod 600 ~/.ssh/authorized_keys 
root@tel4vn:~# cat /root/.ssh/id_rsa

Lưu lại nội dung của file về máy với đuôi .ppk. Ví dụ: key.ppk

3. Tắt đăng nhập SSH bằng password:

Trên CentOS:

root@tel4vn:~# vi /etc/ssh/sshd_config

# line 65: turn to [no]

PasswordAuthentication no

# line 69: make sure the value is [no]

ChallengeResponseAuthentication no

# line 96: make sure the value is [yes]

UsePAM yes




root@tel4vn:~# systemctl restart sshd

 

Trên Debian:

root@tel4vn:~# vi /etc/ssh/sshd_config




# line 52: turn to “no”

PasswordAuthentication no




root@tel4vn:~# systemctl restart ssh

 

4. SSH dùng key từ máy window bằng phần mềm Putty hoặc Mobaxterm:

Putty:

Nhập IP của server cần remote:

ssh vào server linux dùng key, không cần password

Thêm privatekey vào Options controlling SSH authentication:

ssh vào server linux dùng key, không cần password

Sau đó ấn Open để kết nối.

Mobaxterm:

Ấn tổ hộp phím Ctrl + Shift + N để tạo session mới, sau đó chọn tab SSH

ssh vào server linux dùng key, không cần password

Điền thông tin IP, port của server cần kết nối và chọn đường dẫn lưu privatekey sau đó ấn OK để lưu lại và kết nối.

ssh vào server linux dùng key, không cần password

 

Chúc các bạn thành công!

Các khóa học tại TEL4VN: https://tel4vn.edu.vn


Mọi thắc mắc vui lòng liên hệ:
🔗 Fanpage: https://www.facebook.com/tel4vn
🏠 Địa chỉ: 82/2/9 Đinh Bộ Lĩnh, F.26, Q.Bình Thạnh, TP.HCM
☎️ SĐT: 028 3622 0868
📩 Email: tuyensinh@tel4vn.com

Để lại bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *