SSH ពីកុំព្យូទ័រ, SSH Keys និង scp
ភ្ជាប់ Terminal របស់ Pi ពីកុំព្យូទ័រតាម SSH, ប្ដូរពី Password ទៅ SSH Key និងចម្លងឯកសារទៅវិញទៅមក។
- ភ្ជាប់ Pi ពីចម្ងាយដោយ SSH, បង្កើត SSH Key (ed25519) និងចម្លងឯកសារដោយ scp
- សរសេរ និងរត់កូដលើ Pi ដោយ VS Code Remote-SSH
- បង្កើត Python venv ដោយ --system-site-packages និងយល់ពីកំហុស externally-managed-environment
SSH (Secure Shell) បើក Terminal របស់ Pi ពីចម្ងាយតាមបណ្ដាញ ដោយ Encrypt ទិន្នន័យទាំងអស់ (TCP Port 22)។ Windows 10/11 (PowerShell), macOS និង Linux សុទ្ធតែមាន ssh Client ស្រាប់។ ភ្ជាប់ដោយ ssh student@stunity-pi.local។ ពេលភ្ជាប់លើកដំបូង SSH សួរឱ្យបញ្ជាក់ Host Fingerprint (វាយ yes) ហើយរក្សាទុកក្នុង ~/.ssh/known_hosts។ បើក្រោយ Flash SD Card ថ្មី SSH ព្រមាន REMOTE HOST IDENTIFICATION HAS CHANGED សូមលុប Key ចាស់ដោយ ssh-keygen -R stunity-pi.local។
ឈ្មោះ .local ដំណើរការតាម mDNS (Avahi លើ Pi)។ បណ្ដាញសាលាខ្លះបិទ mDNS — ពេលនោះប្រើ IP ពី hostname -I ជំនួស ឧ. ssh student@192.168.1.42។ កុំព្យូទ័រ និង Pi ត្រូវនៅលើបណ្ដាញតែមួយ។ បើភ្លេចបើក SSH ក្នុង Imager អាចបើកពីអេក្រង់ Pi ដោយ sudo raspi-config → Interface Options → SSH ឬ sudo systemctl enable --now ssh។
SSH Key មានសុវត្ថិភាព និងងាយស្រួលជាង Password៖ ssh-keygen -t ed25519 បង្កើត Private Key ~/.ssh/id_ed25519 (ហាមចែករំលែកដាច់ខាត) និង Public Key ~/.ssh/id_ed25519.pub (ចែករំលែកបាន)។ ចម្លង Public Key ទៅ Pi ដោយ ssh-copy-id student@stunity-pi.local (macOS/Linux) ដែលបន្ថែមវាទៅ ~/.ssh/authorized_keys លើ Pi។ លើ Windows ដែលគ្មាន ssh-copy-id អាចបិទភ្ជាប់ខ្លឹមសារ .pub ក្នុង Imager (Public-key Authentication) ឬបន្ថែមទៅ authorized_keys ដោយដៃ។
ចម្លងឯកសារដោយ scp៖ scp blink.py student@stunity-pi.local:~/projects/ (កុំព្យូទ័រ → Pi) និង scp student@stunity-pi.local:~/data.csv . (Pi → កុំព្យូទ័រ)។ វាយ exit ដើម្បីចាកចេញ ហើយ sudo poweroff បិទ Pi ពីចម្ងាយ។ ចំណាំ៖ កម្មវិធីដែលរត់ក្នុង SSH Session នឹងឈប់ពេល Session ដាច់ — ក្នុងវគ្គកម្រិតមធ្យម យើងនឹងប្រើ systemd ដើម្បីរត់ 24/7។
- ssh user@hostname.local ភ្ជាប់ Terminal របស់ Pi ពីចម្ងាយ (Port 22, Encrypted)
- Private Key (id_ed25519) ហាមចែករំលែក; Public Key (.pub) ដាក់ក្នុង authorized_keys លើ Pi
- បើ .local មិនដំណើរការ ប្រើ IP ពី hostname -I
- scp ចម្លងឯកសាររវាងកុំព្យូទ័រ និង Pi តាម SSH
# --- On your laptop (PowerShell, macOS Terminal or Linux) ---
# 1) First connection with password (answer "yes" to the fingerprint prompt)
ssh student@stunity-pi.local
# 2) Create an ed25519 key pair (press Enter to accept the default path)
ssh-keygen -t ed25519 -C "student-laptop"
# 3) Install the public key on the Pi (macOS / Linux)
ssh-copy-id student@stunity-pi.local
# 4) Log in again: no password prompt this time
ssh student@stunity-pi.local
# 5) Copy files laptop -> Pi and Pi -> laptop
scp blink.py student@stunity-pi.local:~/projects/
scp student@stunity-pi.local:~/projects/data.csv .
# 6) After re-flashing the SD card, forget the old host key
ssh-keygen -R stunity-pi.local
# --- On the Pi ---
hostname -I # show IP if .local does not resolve
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
exit # close the SSH sessionការពន្យល់៖ ដំណើរការពេញលេញ៖ ភ្ជាប់លើកដំបូង, បង្កើត Key, ដំឡើង Public Key, Login គ្មាន Password, ចម្លងឯកសារ និងដោះស្រាយបញ្ហា Host Key ក្រោយ Flash ថ្មី។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- Remote Access (SSH) — Raspberry Pi Documentation raspberrypi.comការបើក SSH, ការរក IP Address និងការប្រើ SSH Keys។
- OpenSSH Manual Pages openssh.comឯកសារយោង ssh, ssh-keygen, scp និង ssh_config។