Share This
//Hướng dẫn cài đặt SSH keys

Hướng dẫn cài đặt SSH keys

Cách thức hoạt động của SSH Key

SSH Keys hiểu đơn giản là một phương thức chứng thực người dùng truy cập bằng cách đối chiếu giữa một key cá nhân (private key) và key công khai (public key).
Private key và public key luôn có liên hệ chặt chẽ với nhau để nó có thể nhận diện lẫn nhau. Khi tạo một SSH Key thì sẽ có cả 2 loại key này. Sau đó public key sẽ được bỏ lên server, còn private key sẽ lưu ở máy client và khi đăng nhập vào server, client sẽ gửi yêu cầu đăng nhập kèm theo private key này để gửi tín hiệu đến server, server sẽ kiểm tra xem cái private key có khớp với public key có trên server hay không, nếu có thì sẽ đăng nhập được.
Nội dung giữa private key và public key hoàn toàn khác nhau, nhưng nó vẫn sẽ nhận diện được với nhau thông qua một thuật toán riêng của nó.

ssh-key-auth-flow

Bước 1—Tạo cặp RSA Key

Bước đầu tiên là tạo một cặp SSH keys trên máy client:

ssh-keygen -t rsa

Bước 2—Lưu keys và passphrase

Khi nhập lệnh tạo keys, bạn sẽ phải trả lời một số câu hỏi:

Enter file in which to save the key (/home/demo/.ssh/id_rsa):

Bạn có thể ấn Enter để bỏ qua, các file sẽ được lưu mặc định trong thư mục user (trong bài này user của tôi sẽ là “demo”).

Enter passphrase (empty for no passphrase):

Tùy bạn có muốn sử dụng passphrase hay không. Nhập passphrase sẽ giúp bạn bảo vệ private key. Nếu private key bị mất hoặc đánh cắp bởi một ai đó, người đó cũng không thể dùng nó để đăng nhập vào server được trừ khi họ biết cả passphrase. Nhược điểm duy nhất của passphrase là bạn phải nhập passphrase mỗi lần đăng nhập vào server với SSH keys.

Toàn bộ quá trình tạo keys sẽ diễn ra như sau:

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:
+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+-----------------+

Public key sẽ được lưu tại /home/demo/.ssh/id_rsa.pub và private key sẽ được lưu tại /home/demo/.ssh/id_rsa

Bước 3—Copy public key

Khi SSH key được tạo, bạn sẽ phải đặt public key lên server mà bạn muốn sử dụng.

Bạn có thể copy public key vào file authorized_keys của server với lệnh ssh-copy-id:

ssh-copy-id user@123.45.56.78

Ngoài ra bạn có thể dùng lệnh bên dưới cũng được:

cat ~/.ssh/id_rsa.pub | ssh user@123.45.56.78 "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Bạn sẽ thấy kết quả như bên dưới:

The authenticity of host '12.34.56.78 (12.34.56.78)' can't be established.
RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts.
user@12.34.56.78's password:
Now try logging into the machine, with "ssh 'user@12.34.56.78'", and check in:

 ~/.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

Bây giờ bạn đã có thể login vào server user@12.34.56.78 mà không cần phải cung cấp password. Tuy nhiên nếu bạn có cài đặt passphrase bạn sẽ cần phải nhập passphrase để có thể đăng nhập vào server (ở những lần đăng nhập sau cũng tương tự)

Bước 4—Vô hiệu hóa password khi đăng nhập bằng tài khoản root qua SSH

Khi bạn đã copied SSH keys lên server của bạn và chắc rằng bạn đã có thể đăng nhập với SSH keys rồi, bạn có thể tiếp tục cài đặt để ngăn chặn việc đăng nhập bằng tài khoản root qua password, chỉ cho phép đang nhập tài khoản root qua SSH keys.

Mở file SSH config:

sudo nano /etc/ssh/sshd_config

Thay đổi giá trị của dòng PermitRootLogin:

PermitRootLogin without-password

Khởi động lại SSH.

Chú ý—Cần kiểm tra phân quyền tại thư mục .ssh của server

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

 Hoàn thành.

Theo Digital Ocean.


APPLY NOW






    Benefits

    SALARY & BONUS POLICY

    RiverCrane Vietnam sympathizes staffs' innermost feelings and desires and set up termly salary review policy. Performance evaluation is conducted in June and December and salary change is conducted in January and July every year. Besides, outstanding staffs receive bonus for their achievements periodically (monthly, yearly).

    TRAINING IN JAPAN

    In order to broaden staffs' view about technologies over the world, RiverCrane Vietnam set up policy to send staffs to Japan for study. Moreover, the engineers can develop their career paths in technical or management fields.

    ANNUAL COMPANY TRIP

    Not only bringing chances to the staffs for their challenging, Rivercrane Vietnam also excites them with interesting annual trips. Exciting Gala Dinner with team building games will make the members of Rivercrane connected closer.

    COMPANY'S EVENTS

    Activities such as Team Building, Company Building, Family Building, Summer Holiday, Mid-Autum Festival, etc. will be the moments worthy of remembrance for each individual in the project or the pride when one introduces the company to his or her family, and shares the message "We are One".

    INSURANCE

    Rivercrane Vietnam ensures social insurance, medical insurance and unemployment insurance for staffs. The company commits to support staffs for any procedures regarding these insurances. In addition, other insurance policies are taken into consideration and under review.

    OTHER BENEFITS

    Support budget for activities related to education, entertainment and sports. Support fee for purchasing technical books. Support fee for getting engineering or language certificates. Support fee for joining courses regarding technical management. Other supports following company's policy, etc.

    RELATED JOBS

    © 2012 RiverCrane Vietnam. All rights reserved.

    Close