Tag Archive Password

Byphunsanit

SSH: ช่วยจำ host โดย .ssh\config

การใช้ ssh แต่ละครั้ง ต้องมี HostName, IP, user, port password, passphrase เอาจริง ๆ มันมี Password Manager ช่วยเก็บคำสั่งไว้ให้แต่ จริง ๆ แล้วสามาถทำ command ให้สั้นลงโดยใช้ .ssh\config ย่อให้เหลือแต่คำสั่ง 2 คำ

config ของ SSH เป็นมาตรฐานสากล (OpenSSH) ที่ใช้ได้ทั้ง 3 ระบบปฏิบัติการ แต่ตำแหน่งที่เก็บไฟล์จะต่างกันไปตามโครงสร้าง OS

  • Linux (Ubuntu, CentOS, etc.)
    /home/phunsanit/.ssh/config
  • macOS
    /Users/phunsanit/.ssh/config
  • Windows
    C:\Users\phunsanit.ssh\config

จะเห็นว่าทั้ง mac, linux จะสามารถใช้ ~/.ssh/config เปิดเข้าไปได้

เนื้อหาในไฟล์
~/.ssh/config

# ----------------------------
Host digitalocean_vps
    HostName 924.172.222.123
    IdentityFile ~/.ssh/digitalocean_vps
    Port 59019
    User Pitt
# ---------------------------- host อื่น ๆ

หลังจากนี้ใช้ command สั้น ๆ
ssh {Host}
เช่น
ssh digitalocean_vps
ข้อควรระวัง ถึง /.ssh/config จะสามารถใช้ได้หลาย os แต่ path IdentityFile จะต่างกันนิดหน่อย

  • บน Mac/Linux:
    IdentityFile ~/.ssh/digitalocean_vps
  • บน Windows
    IdentityFile C:\Users\phunsanit.ssh\digitalocean_vps

เท่านี้ก็ SSH หลาย os หลาย server ได้ง่ายขั้นแล้ว แค่จำ host ก็จบแล้ว