เมื่อเข้าถึง Linux server คุณมี several options หากเป็น server with a user interface ใช้ VNC ได้ แต่วิธี common กว่าคือ log into a headless server with Secure Shell หรือ SSH เป็น encrypted, secure protocol ให้ access ไป remote system ผ่าน SSH client แต่คุณอาจแค่อยากส่งบางไฟล์ไป server สำหรับนั้นใช้ program ที่เรียกว่า Secure Copy หรือ SCP ที่รันบน SSH protocol เพื่อ transfer files quickly over your network ไป remote system นี่คือวิธีถ่ายโอนไฟล์อย่างปลอดภัยด้วย SCP ใน Linux
การตั้งค่า SSH (Configuring SSH)
บน server (หรือ remote system ใดๆ ที่ต้องการเข้าถึง) ต้อง install SSH server ที่ common บน Linux คือ OpenSSH server ติดตั้งด้วยคำสั่งใดคำสั่งหนึ่ง:
# Debian/Ubuntu-based server sudo apt install openssh-server # Fedora/Centos sudo dnf install openssh-server
ขึ้นอยู่กับ distro อาจต้อง allow SSH ผ่าน software firewalls บน Ubuntu ปัญหานี้ไม่มี แต่บน CentOS ต้องรันคำสั่งต่อไปนี้:
sudo firewall-cmd --add-service=ssh --permanent sudo firewall-cmd --reload
ที่จุดนี้ต้องการ SSH client บน most distros มี OpenSSH client installed แตถ้าไม่มี ติดตั้งด้วยคำสั่ง:
# Debian/Ubuntu-based distro sudo apt install openssh-client # Fedora sudo dnf install openssh-client
การเชื่อมต่อกับระบบของคุณผ่าน SSH
ตรวจสอบ SSH works ก่อน mess around กับ SCP ก่อน connect via SSH ต้อง discover IP address ของ server บน graphical servers IP address แสดงใน Network applet ใน System Settings บน most servers ควรใช้คำสั่ง ip บน terminal
ip a s
ใน output หา line เริ่มด้วย inet ภายใต้ eth0 หรือ enp1s0 ขึ้นอยู่กับว่า network interface เชื่อมต่อกับ system อย่างไร ในกรณีผมคือ 192.168.122.201
ทดสอบ SSH connection ย้ายไป Linux client machine พิมพ์:
ssh user@192.168.122.201
เปลี่ยน "user" เป็น actual username ใน server
ใส่ password ของ account นั้น และคุณ in business หากได้คำถามเกี่ยวกับ "authenticity of host can't be established" แค่ตอบ "yes" เป็น security check ให้แน่ใจว่าคุณ connect ไป actual server ไม่ใช่ impostor ควรเห็น same prompt ที่เห็นตอน login เข้า server โดยตรง และหมายถึง connection successful ควร configure SSH connections สำหรับ maximum security ก่อนดำเนินต่อ
การใช้ SCP ถ่ายโอนไฟล์
ตอนนี้ทดสอบ SSH connection แล้ว เริ่ม copy files ระหว่างสองเครื่อง Secure copying ทำได้ด้วยคำสั่ง scp Basic format ของคำสั่ง scp คือ:
scp /PATH/TO/FILE USER@IP-ADDRESS:PATH/TO/DESIRED/DESTINATION
ตัวอย่าง copy file "backup.txz" จาก local machine ไป folder "backups" ใน home directory ของ user "maketecheasier" บน remote server IP 192.168.1.101 ใช้:
scp backup.txz maketecheasier@192.168.122.201:~/backups/
คล้ายกับตอน connect ด้วย ssh จะ prompt for password ไม่ prompt for username เพราะ specified ในคำสั่งแล้ว
ใช้ wild cards ได้เช่น:
scp *.txz maketecheasier@192.168.122.201:~/backups/
Copy file จาก remote server ไป local machine แค่ reverse parameters:
scp maketecheasier@192.168.122.201:~/backups/backup.txz .
สังเกต dot ท้ายคำสั่งหมายถึง "current directory" เหมือน standard cp หรือ mv commands ระบุ directory อื่นได้เช่นกัน
scp -r maketecheasier@192.168.122.201:~/backups/ backups-from-server/
Wild cards เหมือนกัน:
scp maketecheasier@192.168.122.201:~/backups/*.txz .
Recursively copy directory ไป remote server ใช้ option -r:
scp -r backups/ maketecheasier@192.168.122.201:~/backups/
Copy recursively copy of directory จาก remote server ไป local machine ใช้:
scp -r scp -r maketecheasier@192.168.122.201:~/backups/ .
Other SCP Commands to Try
เพื่อ progress further ลอง experiment with option -C ซึ่ง enables compression during copy หรือ option -l ซึ่ง limits bandwidth during copy
เพื่อ transfer file without having to type in a password everytime สร้าง private SSH key เพื่อ connect ไป server ได้