Hướng Dẫn Toàn Diện về Bastion Host trong AWS
Giới Thiệu
Trong thế giới của điện toán đám mây, việc thiết kế một kiến trúc bảo mật mạnh mẽ là vô cùng quan trọng. Một trong những thành phần thiết yếu trong chiến lược bảo mật cho môi trường AWS là Bastion Host. Bài viết này sẽ đi sâu vào tìm hiểu về Bastion Host, cách triển khai, các phương pháp tối ưu hóa, và các phương pháp thực hành tốt nhất để bảo vệ hệ thống AWS của bạn.
Bastion Host là gì?
Bastion Host (còn được gọi là Jump Server hoặc Jump Box) là một máy chủ đặc biệt được thiết kế để cung cấp điểm truy cập duy nhất vào một mạng riêng từ mạng bên ngoài, thường là internet. Nó hoạt động như một “cổng vào” an toàn và được kiểm soát chặt chẽ cho phép quản trị viên kết nối với các tài nguyên trong mạng riêng của bạn.
Trong môi trường AWS, Bastion Host thường là một EC2 instance được đặt trong một public subnet của VPC (Virtual Private Cloud), trong khi các tài nguyên khác như database, application servers, và các dịch vụ nội bộ được đặt trong các private subnets.

Tại sao cần Bastion Host?
- Tăng cường bảo mật: Tập trung điểm truy cập vào hệ thống, giảm thiểu “attack surface”
- Kiểm soát truy cập: Quản lý và giám sát tập trung việc truy cập vào mạng nội bộ
- Tuân thủ quy định: Đáp ứng các yêu cầu về kiểm toán và tuân thủ quy định
- Quản lý SSH key: Đơn giản hóa việc quản lý các SSH key cho các EC2 instances
- Logging và Monitoring: Tạo điểm tập trung để ghi nhật ký và giám sát hoạt động truy cập
Triển khai Bastion Host trong AWS
1. Kiến trúc VPC cho Bastion Host
Một VPC điển hình với Bastion Host thường bao gồm:
- Public subnet(s): Chứa Bastion Host và các tài nguyên cần tiếp xúc với internet như load balancers
- Private subnet(s): Chứa các ứng dụng, cơ sở dữ liệu và các tài nguyên khác
- Internet Gateway: Cho phép kết nối từ internet đến public subnet
- NAT Gateway/Instance: Cho phép tài nguyên trong private subnet kết nối ra internet
- Route Tables: Định tuyến traffic giữa các subnet và internet
2. Thiết lập Bastion Host với EC2 Instance
# Tạo EC2 Key Pair cho Bastion Host (nếu chưa có)
aws ec2 create-key-pair --key-name bastion-key --query 'KeyMaterial' --output text > bastion-key.pem
chmod 400 bastion-key.pem
# Tạo Security Group cho Bastion Host
aws ec2 create-security-group \
--group-name bastion-sg \
--description "Security group for bastion host" \
--vpc-id vpc-12345678
# Thêm inbound rule cho SSH (chỉ từ IP address của bạn)
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp \
--port 22 \
--cidr YOUR_IP_ADDRESS/32
# Tạo EC2 Instance làm Bastion Host
aws ec2 run-instances \
--image-id ami-12345678 \
--instance-type t3.micro \
--key-name bastion-key \
--security-group-ids sg-12345678 \
--subnet-id subnet-12345678 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Bastion-Host}]'
3. Kết nối thông qua Bastion Host
Để kết nối đến một EC2 instance trong private subnet thông qua Bastion Host:
# Phương pháp 1: SSH qua 2 bước
ssh -i bastion-key.pem ec2-user@BASTION_PUBLIC_IP
ssh -i private-instance-key.pem ec2-user@PRIVATE_INSTANCE_IP
# Phương pháp 2: SSH ProxyJump (cần OpenSSH 7.3+)
ssh -i private-instance-key.pem -J ec2-user@BASTION_PUBLIC_IP ec2-user@PRIVATE_INSTANCE_IP
# Phương pháp 3: SSH Agent Forwarding
ssh-add private-instance-key.pem
ssh -A -i bastion-key.pem ec2-user@BASTION_PUBLIC_IP
ssh ec2-user@PRIVATE_INSTANCE_IP
4. Sử dụng Session Manager thay thế cho Bastion Host
AWS Systems Manager Session Manager là một giải pháp thay thế không cần Bastion Host:
# Cài đặt Session Manager plugin
# Kết nối đến instance thông qua Session Manager
aws ssm start-session --target i-12345678abcdef012
Bảo mật cho Bastion Host
1. Hardening Bastion Host
# Cập nhật hệ thống
sudo yum update -y # Amazon Linux/RHEL
# hoặc
sudo apt update && sudo apt upgrade -y # Ubuntu
# Cấu hình SSH để tăng cường bảo mật
sudo nano /etc/ssh/sshd_config
Các thiết lập SSH được khuyến nghị:
PermitRootLogin no
PasswordAuthentication no
AllowUsers ec2-user
ClientAliveInterval 300
ClientAliveCountMax 0
# Khởi động lại dịch vụ SSH
sudo systemctl restart sshd
2. Security Groups
- Inbound Rules:
- Chỉ cho phép SSH (TCP port 22) từ các IP addresses đã được phê duyệt
- Sử dụng CIDR ranges càng hẹp càng tốt, lý tưởng là /32 (một IP address)
- Outbound Rules:
- Chỉ cho phép SSH đến các instance trong private subnet
- Hạn chế các kết nối ra ngoài không cần thiết
3. Network ACLs
Bổ sung thêm một lớp bảo mật với Network ACLs:
# Tạo Network ACL
aws ec2 create-network-acl --vpc-id vpc-12345678
# Thêm inbound rule
aws ec2 create-network-acl-entry \
--network-acl-id acl-12345678 \
--ingress --rule-number 100 \
--protocol tcp --port-range From=22,To=22 \
--cidr-block YOUR_IP_ADDRESS/32 \
--rule-action allow
4. Giám sát và Logging
# Bật CloudTrail
aws cloudtrail create-trail \
--name bastion-trail \
--s3-bucket-name your-cloudtrail-bucket \
--is-multi-region-trail
aws cloudtrail start-logging --name bastion-trail
# Thiết lập CloudWatch Logs
aws logs create-log-group --log-group-name bastion-logs
Các Mô Hình Triển Khai Tiên Tiến
1. Auto Scaling Bastion Host
Tạo một Auto Scaling Group để đảm bảo tính sẵn sàng cao cho Bastion Host:
# CloudFormation snippet
BastionAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- !Ref PublicSubnet1
- !Ref PublicSubnet2
LaunchTemplate:
LaunchTemplateId: !Ref BastionLaunchTemplate
Version: !GetAtt BastionLaunchTemplate.LatestVersionNumber
MinSize: '1'
MaxSize: '2'
DesiredCapacity: '1'
Tags:
- Key: Name
Value: Bastion-Host
PropagateAtLaunch: true
2. Bastion Host với Elastic IP
Gán Elastic IP để duy trì địa chỉ IP cố định cho Bastion Host:
# Cấp phát Elastic IP
aws ec2 allocate-address
# Gán Elastic IP cho Bastion Host
aws ec2 associate-address \
--instance-id i-12345678abcdef012 \
--allocation-id eipalloc-12345678
3. Bastion Host với Multiple AZs
Triển khai Bastion Host trên nhiều Availability Zones để tăng tính sẵn sàng:
# Tạo Bastion Host trong AZ thứ hai
aws ec2 run-instances \
--image-id ami-12345678 \
--instance-type t3.micro \
--key-name bastion-key \
--security-group-ids sg-12345678 \
--subnet-id subnet-87654321 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Bastion-Host-AZ2}]'
Các Phương Pháp Thay Thế cho Bastion Host
1. AWS Systems Manager Session Manager
Ưu điểm:
- Không cần mở port SSH
- Quản lý truy cập thông qua IAM
- Logging tích hợp với CloudTrail
Thiết lập cơ bản:
# Gắn IAM Role cho EC2 instances
aws iam attach-role-policy \
--role-name EC2Role \
--policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
# Kết nối sử dụng Session Manager
aws ssm start-session --target i-12345678abcdef012
2. AWS VPN (Client VPN hoặc Site-to-Site VPN)
# Tạo Client VPN Endpoint
aws ec2 create-client-vpn-endpoint \
--client-cidr-block 10.0.0.0/22 \
--server-certificate-arn arn:aws:acm:region:account:certificate/certificate-id \
--authentication-options Type=certificate-authentication,MutualAuthentication={ClientRootCertificateChainArn=arn:aws:acm:region:account:certificate/certificate-id} \
--connection-log-options Enabled=true,CloudwatchLogGroup=vpn-logs
3. AWS Direct Connect
Thiết lập kết nối riêng giữa mạng on-premises và AWS.
Best Practices cho Bastion Host
1. Quản lý SSH Keys
# Tạo user với key-based authentication
sudo adduser newuser
sudo mkdir -p /home/newuser/.ssh
sudo chmod 700 /home/newuser/.ssh
# Thêm public key
echo "ssh-rsa AAAAB3Nza..." | sudo tee /home/newuser/.ssh/authorized_keys
sudo chmod 600 /home/newuser/.ssh/authorized_keys
sudo chown -R newuser:newuser /home/newuser/.ssh
2. Sử dụng MFA (Multi-Factor Authentication)
# Cài đặt Google Authenticator
sudo yum install -y google-authenticator
# Cấu hình PAM
sudo nano /etc/pam.d/sshd
# Thêm dòng: auth required pam_google_authenticator.so
# Cấu hình SSH
sudo nano /etc/ssh/sshd_config
# Thiết lập: ChallengeResponseAuthentication yes
3. Giới hạn Thời Gian Phiên Kết Nối
# Cấu hình TMOUT cho bash sessions
echo "TMOUT=900" | sudo tee -a /etc/profile
echo "readonly TMOUT" | sudo tee -a /etc/profile
echo "export TMOUT" | sudo tee -a /etc/profile
4. Giám sát Truy Cập
# Cấu hình auditd
sudo yum install -y audit
sudo systemctl enable auditd
sudo systemctl start auditd
# Cấu hình audit rules
echo "-a exit,always -F arch=b64 -S execve" | sudo tee -a /etc/audit/rules.d/audit.rules
sudo service auditd restart
Triển khai Bastion Host với AWS CloudFormation
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Bastion Host Setup'
Parameters:
KeyName:
Description: EC2 Key Pair
Type: AWS::EC2::KeyPair::KeyName
AllowedIP:
Description: IP address range allowed to connect to bastion
Type: String
Default: 0.0.0.0/0
VPC:
Description: VPC for Bastion Host
Type: AWS::EC2::VPC::Id
PublicSubnet:
Description: Public Subnet for Bastion Host
Type: AWS::EC2::Subnet::Id
Resources:
BastionSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for bastion host
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref AllowedIP
Tags:
- Key: Name
Value: bastion-sg
BastionHost:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: ami-0c55b159cbfafe1f0 # Amazon Linux 2 AMI (update this)
KeyName: !Ref KeyName
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeviceIndex: 0
GroupSet:
- !Ref BastionSecurityGroup
SubnetId: !Ref PublicSubnet
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y
# Hardening SSH
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
echo "ClientAliveInterval 300" >> /etc/ssh/sshd_config
echo "ClientAliveCountMax 0" >> /etc/ssh/sshd_config
systemctl restart sshd
# Setup CloudWatch Logs
yum install -y awslogs
systemctl enable awslogsd
systemctl start awslogsd
Tags:
- Key: Name
Value: Bastion-Host
Outputs:
BastionPublicIP:
Description: Public IP of the bastion host
Value: !GetAtt BastionHost.PublicIp
BastionPrivateIP:
Description: Private IP of the bastion host
Value: !GetAtt BastionHost.PrivateIp
SSHCommand:
Description: Command to SSH into the bastion host
Value: !Sub ssh -i ${KeyName}.pem ec2-user@${BastionHost.PublicIp}
Triển khai Bastion Host với Terraform
provider "aws" {
region = "us-east-1"
}
variable "vpc_id" {
description = "ID of the VPC"
type = string
}
variable "subnet_id" {
description = "ID of the public subnet"
type = string
}
variable "allowed_ip" {
description = "IP address allowed to SSH to bastion"
type = string
default = "0.0.0.0/0"
}
variable "key_name" {
description = "EC2 Key Pair name"
type = string
}
resource "aws_security_group" "bastion" {
name = "bastion-sg"
description = "Security group for bastion host"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.allowed_ip]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "bastion-sg"
}
}
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_instance" "bastion" {
ami = data.aws_ami.amazon_linux.id
instance_type = "t3.micro"
key_name = var.key_name
vpc_security_group_ids = [aws_security_group.bastion.id]
subnet_id = var.subnet_id
user_data = <<-EOF
#!/bin/bash
yum update -y
# Hardening SSH
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
echo "ClientAliveInterval 300" >> /etc/ssh/sshd_config
echo "ClientAliveCountMax 0" >> /etc/ssh/sshd_config
systemctl restart sshd
# Setup CloudWatch Logs
yum install -y awslogs
systemctl enable awslogsd
systemctl start awslogsd
EOF
tags = {
Name = "Bastion-Host"
}
}
resource "aws_eip" "bastion" {
instance = aws_instance.bastion.id
domain = "vpc"
tags = {
Name = "Bastion-EIP"
}
}
output "bastion_public_ip" {
value = aws_eip.bastion.public_ip
}
output "ssh_command" {
value = "ssh -i ${var.key_name}.pem ec2-user@${aws_eip.bastion.public_ip}"
}
Giám sát và Quản lý Bastion Host
1. CloudWatch Metrics và Alarms
# Tạo alarm cho CPU usage
aws cloudwatch put-metric-alarm \
--alarm-name bastion-cpu-alarm \
--alarm-description "Alarm when CPU exceeds 80%" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 80 \
--comparison-operator GreaterThanThreshold \
--dimensions Name=InstanceId,Value=i-12345678abcdef012 \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:region:account-id:alert-topic
2. Logging SSH Sessions
# Cấu hình SSH để log các sessions
sudo nano /etc/ssh/sshd_config
# Thêm các dòng
LogLevel VERBOSE
SyslogFacility AUTH
3. Cập Nhật Tự Động
# Thiết lập cập nhật tự động cho Amazon Linux 2
sudo yum install -y yum-cron
sudo nano /etc/yum/yum-cron.conf
# Thiết lập
apply_updates = yes
update_cmd = security
# Kích hoạt dịch vụ
sudo systemctl enable yum-cron
sudo systemctl start yum-cron
Xử Lý Sự Cố Thường Gặp
1. Không Thể Kết Nối SSH
Kiểm tra:
- Security Group rules
- Network ACLs
- SSH key permissions (chmod 400)
- Route tables
- Internet Gateway configuration
2. Kết Nối Chậm hoặc Time Out
# Kiểm tra kết nối mạng
ping -c 3 8.8.8.8
# Kiểm tra SSH daemon
sudo systemctl status sshd
# Kiểm tra tài nguyên hệ thống
top
3. Mất Quyền Truy Cập
# Tạo mới EC2 Key Pair và thay thế
aws ec2 create-key-pair --key-name new-bastion-key --query 'KeyMaterial' --output text > new-bastion-key.pem
chmod 400 new-bastion-key.pem
# Cập nhật authorized_keys (qua console/user data)
Chi Phí và Tối Ưu Hóa
- Sử dụng Instance Type phù hợp: t3.micro hoặc t3.nano thường đủ
- Spot Instances: Có thể sử dụng cho môi trường không yêu cầu 100% uptime
- Auto Shutdown: Tắt Bastion Host khi không sử dụng
- Reserved Instances: Cho môi trường production dài hạn
So Sánh Các Giải Pháp Truy Cập Remote
Giải Pháp | Ưu Điểm | Hạn Chế |
---|---|---|
Bastion Host | Kiểm soát truy cập tập trung, Giám sát dễ dàng | Cần duy trì EC2 instance, Chi phí bổ sung |
Session Manager | Không cần mở port SSH, Quản lý IAM tích hợp | Cần cài đặt SSM Agent, Hạn chế shell |
VPN | Truy cập bảo mật cho nhiều dịch vụ | Phức tạp hơn, Chi phí cao hơn |
Direct Connect | Kết nối độc lập với internet | Chi phí cao, Phức tạp khi triển khai |
Kết Luận
Bastion Host là một thành phần thiết yếu trong chiến lược bảo mật của AWS, cung cấp một điểm truy cập an toàn và được kiểm soát vào mạng riêng của bạn. Bằng cách tuân theo các phương pháp thực hành tốt nhất được trình bày trong bài viết này, bạn có thể triển khai một Bastion Host bảo mật, đáng tin cậy và hiệu quả để quản lý cơ sở hạ tầng AWS của mình.
Dù vậy, các giải pháp thay thế như AWS Systems Manager Session Manager đang trở nên phổ biến hơn và cung cấp nhiều lợi ích về bảo mật và quản lý. Đánh giá nhu cầu của tổ chức bạn và lựa chọn giải pháp phù hợp nhất.
Tài Liệu Tham Khảo
- AWS Bastion Host Best Practices
- AWS VPC Documentation
- AWS Systems Manager Session Manager
- [AWS Security Best Practices](https://aws.amazon.com/architecture/security-identity-compliance/