Secure copy protocol (SCP) là công cụ mà developer thường sử dụng để transfer files giữa máy client và server, nó hoạt động trên nền giao thức SSH (Secure Shell). Server thường là máy chạy Linux, còn client có thể là Windows hoặc Linux. Ví dụ →
1 2 3 |
$ scp tuanpm@192.168.1.10:./aosp/out/target/product/generic_x86_64/system-qemu.img system-qemu.img tuanpm@192.168.1.10's: system-qemu.img 100% 2562MB 73.3MB/s 00:34 |
Câu lệnh trên sẽ copy file system-qemu.img ở trên server về máy client nhưng default thì khi sử dụng lệnh SCP để copy file về từ server (hoặc copy file từ client lên server) thì bạn cần phải nhập SSH password của server (như trên). Việc phải nhập password sau mỗi câu lệnh scp khá là phiền phức, để loại bỏ phiền phức đó thì các bạn có thể làm theo hướng dẫn sau.
Client chạy Windows, server chạy Linux
- Trên client (Windows) sử dụng ssh-keygen để tạo ra ssh key (private + public). Khi dùng ssh-keygen các bạn có thể cứ để trống và Enter hết cho nhanh →
123456789101112131415161718192021> ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (C:\Users\tuanp/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in C:\Users\tuanp/.ssh/id_rsa.Your public key has been saved in C:\Users\tuanp/.ssh/id_rsa.pub.The key fingerprint is:SHA256:Dh4RqBstbyyX22SO2yGZmbySOVM05G3qX0xL0XndVLw tuanp@LAPTOP-8Q1LUVIGThe key's randomart image is:+---[RSA 2048]----+| .. .+|| o . . . . o.|| = .. . o . . o|| + = o. . . E || B =o S || o.O*oB . || OBBo = || * ++oo || +ooo |+----[SHA256]-----+
- Lệnh bên trên tạo ra cặp key là id_rsa (private key) và id_rsa pub (public key) và lưu vào đường dẫn C:\Users\tuanp\.ssh. Tiếp theo ta cần đăng ký public key đã tạo ra với server để server verify và lưu trên server sử dụng cho việc tự động authenticate về sau.
- Sử dụng Notepad++ mở file C:\Users\tuanp/.ssh/id_rsa.pub lên ta sẽ thấy một chuỗi ký tự rất dài, đó chính là public key
1ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDauvLrl56dhASGiS+C+O89M0lgaskSpdJOhxKyIeStCc8ScV8L5GAjF/FY93oppm2TB+KlSl7N+qBq/xiweNK11iZ/Vg3tmbMBcE55BGQ1vbZ6Zujhg9Krw/bYYvV64ALurpDQZsqjI7KaiaEcLRD+f+/YJJNrNJGgjffUJXjU9Nxo2C63OeYrNHTfiB2oE6od+nJ7oW0zO/2dYQmRC1k3SlRW1imgKj036TQJIBevKwf8iQoHfCLkxGiLDP/vEbSjS8A0CoY8R7/UkYz95+3/GCUfmX8IkOPjUZ1NJ+sKKt9EndB5poJVNU4M81Usr0WRKliUBQOaSBQJKqtPidtr tuanp@LAPTOP-8Q1LUVIG
- Tiếp theo trên Server (Linux): Kiểm tra xem ở đường dẫn /home/<user>/.ssh/ đã có file authorized_keys chưa, nếu chưa có thì tự tạo file này, nếu có rồi thì mở lên và paste nội dung file id_rsa.pub trên client vào một dòng mới và save lại.
- Sử dụng Notepad++ mở file C:\Users\tuanp/.ssh/id_rsa.pub lên ta sẽ thấy một chuỗi ký tự rất dài, đó chính là public key
- Done: từ nay về sau khi bạn có thể dùng scp trên máy client này để truy cập vào files trên server thì sẽ không cần nhập password nữa.
Client và server đều chạy Linux
- Trên client (Linux) ta cũng sử dụng ssh-keygen tương tự như trên để tạo ra ssh key (private + public) →
12345678910111213141516171819202122$ ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/home/tuanpm/.ssh/id_rsa):Created directory '/home/tuanpm/.ssh'.Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /home/tuanpm/.ssh/id_rsa.Your public key has been saved in /home/tuanpm/.ssh/id_rsa.pub.The key fingerprint is:SHA256:F3mmuxHitLjJoHrw2E1LUdN6Z5egwbjilRJYrhGFJLQ tuanpm@osboxesThe key's randomart image is:+---[RSA 2048]----+|ooo=o + || .+o. + + .. || E. .o = oo.o. || o+ = o o=o || .. = .So+. ||. + + + o || = +... o o ||. +.oo o o ||.o. + . |+----[SHA256]-----+
- Lệnh bên trên tạo ra cặp key là id_rsa (private key) và id_rsa pub (public key) và lưu vào đường dẫn ~/.ssh/. Bây giờ ta cần phải gửi public này cho server để server verify, ta sẽ sử dụng lệnh ssh-copy-id, ví dụ như sau →
1$ ssh-copy-id -i ~/.ssh/id_rsa tuanpm@192.168.1.10
- Done nhưng sau khi lệnh được thực thi thì từ nay về sau khi bạn có thể dùng scp để copy file lên hoặc lấy file về từ server mà không cần nhập password.
— Phạm Minh Tuấn (Shun) —