Sub topic 12
Running SSH through Session Manager
This chapter assumes you can already connect to an instance with ssh and a key pair, and that you know how ~/.ssh/config works. It also assumes the instance already works with Session Manager: SSM Agent is running, an instance profile is attached, and aws ssm start-session opens a shell. You also need the AWS CLI and the Session Manager plugin installed on your machine.
Why tunnel SSH through Session Manager
A plain Session Manager shell gives you a terminal and nothing else. Some tools expect a real SSH connection:
scpandsftpfor file transferrsyncover SSH- IDE remote-development features that use SSH
- Git over SSH to a private host
- SSH agent forwarding and
-L/-Rport forwards
SSH over Session Manager lets you keep those tools while closing inbound port 22. Session Manager carries the TCP stream, and ssh runs on top of it as usual. You get SSH's features with Session Manager's network model and IAM-based access control.
How the tunnel works
The AWS-managed document AWS-StartSSHSession makes SSM Agent open a TCP connection from the instance to a port on the instance itself, by default port 22. The Session Manager plugin on your machine pipes your ssh client's standard input and output through that session. The SSH handshake and encryption run end to end between your client and sshd. Session Manager only moves the bytes.
Two consequences follow from this design:
- The security group needs no inbound rule for port 22. The agent reaches
sshdover the loopback interface, so onlysshdlistening locally matters. - SSH authentication still happens. You need a valid key (or an Instance Connect key) and the right login user, in addition to the IAM permissions for Session Manager.
Instance requirements
Everything from the Session Manager setup still applies:
- SSM Agent must be running, with version 2.3.672.0 or later for SSH sessions.
- The instance profile must allow the instance to talk to Systems Manager, for example through
AmazonSSMManagedInstanceCore. - The instance needs a network path to the Systems Manager endpoints: NAT, an internet gateway, or VPC endpoints for
ssm,ssmmessages, andec2messages. sshdmust be installed, running, and listening on the port you target.
Confirm that the instance is registered and online before you debug anything else:
aws ssm describe-instance-information \
--query "InstanceInformationList[].[InstanceId,PingStatus]" \
--output textThe instance should show Online. If it is missing or shows ConnectionLost, fix that first. No SSH setting can compensate for it.
IAM permission for the SSH document
The shell-only policy from the previous chapter allows only the SSM-SessionManagerRunShell document. SSH tunnelling uses a different document, so you must allow it explicitly. A user who can open a shell but gets AccessDeniedException on SSH is usually missing this.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": [
"arn:aws:ec2:us-east-1:111122223333:instance/i-0123456789abcdef0",
"arn:aws:ssm:us-east-1::document/AWS-StartSSHSession"
]
}
]
}Keep the TerminateSession and ResumeSession statement from the earlier policy alongside this one. To add port forwarding later, include the AWS-StartPortForwardingSession document ARN in the same Resource list.
Configuring the SSH client
Add a block to ~/.ssh/config that matches instance IDs and routes them through the AWS CLI:
Host i-* mi-*
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"%h expands to the host name you typed, which is the instance ID, and %p expands to the port, 22 unless you override it. mi-* covers managed on-premises nodes, which use the same mechanism. Because the host name is an instance ID, ssh never does a DNS lookup for it.
Now connect with the instance ID in place of an address:
ssh -i ~/.ssh/my-key.pem ec2-user@i-0123456789abcdef0To avoid repeating the user and key, add them to the same block:
Host i-* mi-*
User ec2-user
IdentityFile ~/.ssh/my-key.pem
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"Then ssh i-0123456789abcdef0 is enough. File transfer works the same way:
scp -i ~/.ssh/my-key.pem ./app.tar.gz ec2-user@i-0123456789abcdef0:/tmp/If you want a one-off connection without editing the config, pass the same string with -o ProxyCommand=... on the command line.
Region and profile
The aws command inside ProxyCommand uses your default region and credentials. If the instance is in another region or account, add --region and --profile to the aws ssm start-session command. You can also set AWS_PROFILE and AWS_REGION in your shell. A mismatch produces TargetNotConnected, because the service cannot find that instance ID in the region it was asked about.
Windows clients
The sh -c wrapper in the example is for Linux and macOS. On Windows, OpenSSH runs the ProxyCommand through cmd, so use the AWS-documented PowerShell form for that platform and test the aws ssm start-session command by hand first.
Combining with EC2 Instance Connect
You do not need a long-lived key on the instance. Push a temporary public key first, then connect within about 60 seconds:
aws ec2-instance-connect send-ssh-public-key \
--instance-id i-0123456789abcdef0 \
--instance-os-user ec2-user \
--ssh-public-key file://$HOME/.ssh/id_ed25519.pub
ssh -i ~/.ssh/id_ed25519 ec2-user@i-0123456789abcdef0This pairs a short-lived key with a tunnel that needs no inbound port. Your IAM policy then needs ec2-instance-connect:SendSSHPublicKey in addition to the Session Manager permissions. The 60-second window starts when the key is pushed, and an authenticated connection that has already started stays open after it.
What gets logged
Session Manager's session logging to CloudWatch Logs or S3 does not record what you type inside an SSH session. The content is encrypted between your client and sshd, and the session only carries the encrypted stream. What you do get:
- CloudTrail records the
StartSessioncall: who, which instance, which document, and when. - Session history in Systems Manager shows the session with the
AWS-StartSSHSessiondocument. - Command-level auditing must come from the instance itself, such as
auditdorsshdlogs.
If you must record every command, a plain Session Manager shell with logging enabled is a better fit than SSH over SSM.
Troubleshooting
| Symptom | Likely cause | What to check |
|---|---|---|
| `SessionManagerPlugin is not found` | Plugin missing on your machine | Install it and confirm `session-manager-plugin` runs |
| `TargetNotConnected` | Instance not online in SSM, or wrong region or profile | `describe-instance-information` in the same region and profile |
| `AccessDeniedException` on `StartSession` | IAM lacks the `AWS-StartSSHSession` document or the instance ARN | Compare the policy `Resource` list to the instance ID and document |
| `Connection closed by UNKNOWN port 65535` | The proxy command failed before SSH started | Run the `aws ssm start-session` command alone to see the real error |
| Session starts, then `Connection refused` | `sshd` not listening on that port | Open a normal shell with `aws ssm start-session` and run `ss -tlnp` |
| `Permission denied (publickey)` | Wrong user or key, or an expired Instance Connect key | Check the AMI's login user and the key file permissions (`chmod 400`) |
The most useful habit is to separate the layers. Start with aws ssm start-session --target <id>. If that works, the Session Manager layer is healthy, and the remaining problem is sshd, the user, or the key.
Exercise
A teammate can open a shell on an instance with aws ssm start-session --target i-0abc..., but ssh ec2-user@i-0abc... (with the ProxyCommand config above) fails with an access denied error from Systems Manager. The security group has no inbound rules. What is the most likely cause, and how do you fix it?
Show answer
The IAM policy allows only the SSM-SessionManagerRunShell document. SSH tunnelling uses AWS-StartSSHSession, so ssm:StartSession is denied for that document. Add arn:aws:ssm:<region>::document/AWS-StartSSHSession to the Resource list of the ssm:StartSession statement. The missing inbound rule is not the problem, because the tunnel needs no inbound port.
Exercise
Your audit team asks for a record of every command run during SSH sessions made through Session Manager, and you have enabled CloudWatch Logs session logging. Will it capture the commands? What do you get, and what could you use instead?
Show answer
No. The SSH stream is encrypted end to end between your client and sshd, so Session Manager logging cannot see the commands. CloudTrail records the StartSession call, and Systems Manager session history lists the session. For command-level records, use instance-side auditing such as auditd, or have users open a plain Session Manager shell, where session logging captures the terminal output.