متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 54Cloud · AWS

How to connect to an EC2 instance without SSH keys

Port 22 open to the internet and .pem key files shared in chat are a classic risk. AWS Systems Manager Session Manager gives you a shell on an EC2 instance with no open inbound port, no SSH key and no bastion — access is controlled by IAM, and every session can be logged. This how-to prepares the instance, opens a shell, forwards a port to a private database, and shows EC2 Instance Connect Endpoint as the alternative when you really need SSH.

  • 5 الدروس
  • 5 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

54.1

The quick answer

aws ssm describe-instance-information --query 'InstanceInformationList[].[InstanceId,PingStatus,PlatformName]' --output text
i-0abc1234def567890   Online   Ubuntu
aws ssm start-session --target i-0abc1234def567890
Starting session with SessionId: alice-0a1b2c3d4e5f
sudo -i

الأوامر في هذا الدرس

54.2

Step by step

  1. The instance needs three things:
NeedHow
SSM AgentInstalled on Amazon Linux 2023 and the official Ubuntu AMIs; on RHEL install the amazon-ssm-agent package from AWS.
An instance profile (IAM role)With the managed policy AmazonSSMManagedInstanceCore.
Outbound HTTPS (443) to SSMThrough a NAT gateway — or, for private subnets, VPC endpoints ssm, ssmmessages, ec2messages.
aws ec2 associate-iam-instance-profile --instance-id i-0abc1234def567890 --iam-instance-profile Name=ec2-ssm-core
  1. Your laptop needs AWS CLI v2 and the Session Manager plugin (a small package from AWS), and IAM rights for ssm:StartSession on that instance.
  2. Open a shell (quick answer). You start as ssm-user; sudo -i for root if your role allows it.
  3. Port forwarding — reach a private database through the instance, no port open anywhere:
aws ssm start-session --target i-0abc1234def567890 --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters host=shop-db.cluster-abc.eu-central-1.rds.amazonaws.com,portNumber=5432,localPortNumber=15432
psql -h 127.0.0.1 -p 15432 -U app shop
  1. Need real SSH (scp, Ansible)? Two good options without public port 22:
  • EC2 Instance Connect Endpoint — SSH to private instances with a short-lived key:
aws ec2-instance-connect ssh --instance-id i-0abc1234def567890 --connection-type eice
  • SSH over Session Manager (a ProxyCommand in ~/.ssh/config), so tools like Ansible work.
  1. Log every session — Session Manager preferences: send session logs to CloudWatch Logs or S3 (encrypted). Then close port 22 in the security group:
aws ec2 revoke-security-group-ingress --group-id sg-0123456789abcdef0 --protocol tcp --port 22 --cidr 0.0.0.0/0

54.3

Check that it worked

describe-instance-information shows the instance as Online, start-session gives a shell, the security group has no inbound 22, and the session appears in Systems Manager → Session Manager → Session history.

54.4

Common problems

ProblemCauseFix
Instance not in describe-instance-informationNo agent, no instance profile, or no way out to SSM.Check the role, the agent (systemctl status amazon-ssm-agent / the snap on Ubuntu) and 443 outbound or VPC endpoints.
SessionManagerPlugin is not foundPlugin missing on your computer.Install the Session Manager plugin, open a new terminal.
AccessDeniedException … ssm:StartSessionYour IAM role may not start sessions on that instance.Ask for ssm:StartSession on that instance (or tag-based).
Port forward works, the database refusesRDS security group does not allow the instance.Allow the instance's security group on 5432 in the RDS group.
Session ends after some minutes idleIdle timeout (default 20 min).Change it in Session Manager preferences.
Agent was "Online", now "ConnectionLost"Instance stopped, role removed, or endpoint/NAT change.Check the instance state, role and network path.

الأوامر في هذا الدرس

54.5

Go further

1Which IAM policy does an EC2 instance need for Session Manager?

AmazonSSMManagedInstanceCore (in its instance profile).

2How do you reach a private RDS database from your laptop without opening any port?

Session Manager port forwarding with the AWS-StartPortForwardingSessionToRemoteHost document.

كل الأوامر في هذا الفصل