--- title: create ssh-key tags: บทความ, sysadmin, debian, linux, ssh, ssh-key, security --- # สร้าง SSH Key ใช้งานกัน SSH Key เป็นตัวช่วยเพิ่มความปลอดภัยอีกระดับหนึ่งในการตรวจสอบการเข้าระบบ SSH หรือ Secure Shell เป็นการเข้าถึงระบบหรือ Server ผ่านทาง network ซึ่งโดยปกติแล้วจะเป็นการใช้ username กับ password คู่กันในการ authentication แต่ด้วย password authentication ระบบอาจมีช่องโหว่ เช่นถูก brute force Key authentication ก็คล้ายกับ password ตรงที่ต้องมีคู่ที่ตรงกัน แต่ Key มีการเข้ารหัส (encrypt) ระบบ Key จะประกอบด้วยกุญแจ 2 ดอก คือ 1. Private key เป็นไฟล์ที่เจ้าของเก็บไว้ Key นี้มีความสำคัญมากใช้สำหรับไขเข้าระบบ เพราะฉนั้นต้องเก็บรักษาให้ปลอดภัย 2. Public key เป็นไฟล์ที่เก็บไว้บนระบบหรือ server เพื่อใช้ตรวจสอบกับ Private key ## สร้าง SSH Key > หากท่านใช้ Windows 10 ท่านสามารถติดตั้ง Ubuntu หรือ Bash เพิ่มเติมได้จาก Store ของ Microsoft และสามารถใช้วิธีการเดียวกันนี้ได้ โดยทำการผ่าน Ubuntu/Bash (แม้แต่ MS ยังรองรับ Linux แล้วท่านจะไม่ switch หรือ) การสร้าง Key นั้นง่ายมาก เข้า Linux terminal แล้วใช้คำสั่ง ssh-keygen ในการสร้าง **รูปแบบคำสั่ง** > **ssh-keygen** [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1] [-N new_passphrase] [-C comment] [-f output_keyfile] > > ดูรูปแบบคำสั่งเต็มๆ พร้อมคำอธิบายได้จาก `$ man ssh-keygen` > ตัวอย่างที่แนะนำ ```bash $ ssh-keygen -b 4096 ตัวอย่างหน้าจอ choo@debdock01:~$ ssh-keygen -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/choo/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/choo/.ssh/id_rsa. Your public key has been saved in /home/choo/.ssh/id_rsa.pub. The key fingerprint is: SHA256:XOFdxD3Um10hqZo50qGqREj8SHhF5MlCcj0RgZoVXEE choo@debdock01 The key randomart image is: +---[RSA 4096]----+ |..+OE= . +=+o| |o+++o. . o oo.+| |.Bo +. o o *| |=.+. . o . o.| | o o S = | | . o * | | . . . . | | . . | | .. | +----[SHA256]-----+ choo@debdock01:~$ ls -l .ssh total 12 -rw------- 1 choo choo 414 Aug 30 12:26 authorized_keys -rw------- 1 choo choo 3243 Aug 30 13:23 id_rsa -rw-r--r-- 1 choo choo 740 Aug 30 13:23 id_rsa.pub ``` ## การนำไปใช้ เมื่อได้ SSH Key มา เราสามารถทำการ copy public key ไปยัง server ที่เราต้องการได้ โดยใช้คำสั่ง ssh-copy-id แล้วตามด้วย user@host ตามตัวอย่างนี้ ```bash choo@debdock01:~$ ssh-copy-id pi@192.168.1.119 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/choo/.ssh/id_rsa.pub" The authenticity of host '192.168.1.119 (192.168.1.119)' can't be established. ECDSA key fingerprint is SHA256:yvAnjtZGwCFhakJQDGZaHqeTG90u5Qfeq/GHn+wFsvc. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys pi@192.168.1.119's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'pi@192.168.1.119'" and check to make sure that only the key(s) you wanted were added. ``` เมื่อทำการส่ง public key ขึ้น host เป็นที่เรียบร้อย เราก็จะสามารถ SSH เข้าได้โดยใช้แค่ key ```bash choo@debdock01:~$ ssh pi@192.168.1.119 Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Fri Aug 30 12:06:37 2019 from 192.168.1.102 SSH is enabled and the default password for the 'pi' user has not been changed. This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password. pi@raspberrypi:~ $ ``` เป็นอย่างไรบ้างครับ ง่ายไหมครับกับการสร้าง และนำไปใช้งาน SSH Key ขอให้สนุกและปลอดภัย Youtube link: https://www.youtube.com/watch?v=IvEbYyoJ-S8&t=403s ![](https://cdn.pixabay.com/photo/2016/10/21/14/56/railway-1758208_960_720.jpg) > ความคิดเห็นคำแนะนำของท่านต่อบทความนี้ จะถูกนำมาใช้ในการพัฒนาปรับปรุงงานเขียนของผม อย่างไงก็รบกวนแสดงความเห็นคิดด้วยนะครับ > > สนับสนุนการเงินได้ที่ > - บัญชีกสิกรไทย 003–3–29344–5