Linux Tip #33: SSH Jump Host - SSH Bastion Host

25/01/2021

Jump host (hay còn gọi là Jump Server/Bastion Host) là một SSH server trung gian (ssh_gateway) đứng giữa người dùng và server cần quản trị (app_server). Nói cách khác, nếu muốn SSH vào app_server, bạn phải đi qua ssh_gateway trước.

ssh bastion host

Có vài cách để thực hiện điều trên:

  • SSH trực tiếp vào SSH Gateway, sau đó SSH vào App server
  • Sử dụng SSH Gateway như một Socks5 Server over SSH Tunnel để kết nối vào App Server
  • Sử dụng tính năng Jump Host của SSH – Linux Tip hôm nay Bo sẽ chia sẻ cách dùng này.

SSH Jump Host Command Line

Dùng option -J của lệnh ssh:

[[email protected] ~]$ ssh -J ssh_gateway app_server

Để thuận tiện, bạn nên setup passwordless SSH cho các ssh_gateway và app_server. Nếu trường hợp username & port của các server khác nhau thì dùng cú pháp:

[[email protected] ~]$ ssh -J username@ssh_gateway:port1 username@app_server -p port2

Cũng có thể “jump” qua nhiều Host theo thứ tự. Ví dụ: muốn SSH vào DB Server từ Client thông qua việc đi qua SSH Gateway trước rồi thông qua App Server:

 [[email protected] ~]$ ssh -J ssh_gateway,app_server db_server

Sử dụng file ssh_config

Khai báo ssh_gateway trong file ssh_config

Host ssh_gateway
     Hostname x.x.x.x

Cấu hình SSH vào app_server thông qua ssh_gateway trong file ssh_config

Host app_server
     Hostname y.y.y.y
     ProxyJump ssh_gateway

Cấu hình SSH vào db_server thông qua ssh_gateway và app_server theo thứ tự:

Hostname db_server
     Hostname z.z.z.z
     ProxyJump ssh_gateway,app_server

Sau khi hoàn tất, bạn có thể SSH vào App server thông qua SSH Gateway bằng cách gõ lệnh:

[[email protected] ~]$ ssh app_server

SSH vào DB server thông qua SSH Gateway và App Server:

[[email protected] ~]$ ssh db_server

Sử dụng Alias

Bạn cũng có thể tạo Alias để SSH vào server bất kỳ thông qua ssh_gateway

 [[email protected] ~]$ alias jssh="ssh -v -J ssh_gateway"

Để SSH thông qua SSH Gateway, dùng lệnh jssh

 [[email protected] ~]$ jssh app_server

Để sử dụng được lệnh “jssh” sau mỗi lần khởi động lại máy, nhét dòng alias vào file “~/.bashrc”:

[[email protected] ~]$ echo 'alias jssh="ssh -v -J ssh_gateway"' >> ~/.bashrc

Hãy tham gia Group Telegram Quản Trị Linux để học thêm các Tips & Tricks khác nhé!