This is an old revision of the document!
Connecting Securely to Amazon EC2 Server with SFTP
With WinSCP you can easily upload and manage files on your Amazon EC2 (Elastic Compute Cloud) instance/server over SFTP protocol.
- Direct Connection
- Connecting to EC2 instance in VPC
- Connecting using AWS SSM (Session Manager)
- Further reading
Direct Connection
Before starting you should:
- Have WinSCP installed;
- Have Amazon EC2 instance running;
- Have enabled inbound SSH traffic from your IP address to your instance;
- Have your key pair ready;
Advertisement
Collect information about your EC2 instance:
- Host name: Check Public DNS column on Instances page of Amazon EC2 console. Note that the public DNS may change when instance is restarted.
- Host key fingerprint: On the first connect you will be prompted to verify server host key.
- You can locate key fingerprint in server’s initial start log, when host keys are generated.1 Use Actions > Instance Settings > Get System Log command on Instances page of Amazon EC2 console:
The format of host key display in the log may differ with distribution or its version.
Alternatively useaws ec2 get-console-output
command. - If you did not save the fingerprint on the first instance run, but you have another EC2 instance that you can connect to safely (you know its fingerprints), you can connect to the target instance using private IP from the trusted instance. Staying within private Amazon network keeps you safe from man-in-the-middle attacks. When on the trusted instance terminal, you can use following commands to collect fingerprints:
$ ssh-keyscan <target_instance_private_ip> > ec2key $ ssh-keygen -l -f ec2key 256 SHA256:oZHeiMEPLKetRgd3M5Itgwaqr2zJJH93EvSdx5UoHbQ <ip> (ED25519) 2048 SHA256:8zg105EUFFrPFpVzdfTGsgXnxuSpTiQd85k0uNapUio <ip> (RSA) 256 SHA256:L7UXLw0djE5B9W7ZhvrkYVSTZyi1MEQ2dBaRtpkkUGY <ip> (ECDSA)
- If you do not have another trusted instance, you can create new temporary instance, just for the purpose of collecting the keys. First find keys for the new temporary instance, using it’s initial start log. Then collect keys of the target instance by connecting to it from the temporary instance. After that you can discard the temporary instance.
- You can locate key fingerprint in server’s initial start log, when host keys are generated.1 Use Actions > Instance Settings > Get System Log command on Instances page of Amazon EC2 console:
Advertisement
Start WinSCP. Login dialog will appear. On the dialog:
- Make sure New site node is selected.
- On New site node, make sure SFTP protocol is selected.
- Enter your EC2 instance public DSN name (see above) into Host name box.
- User name differs with instance type:
- For an Amazon Linux AMI, the user name is
ec2-user
. - For a RHEL5 AMI, the user name is either
root
orec2-user
. - For an Ubuntu AMI, the user name is
ubuntu
. - For an Centos AMI, the user name is
centos
. - For a Fedora AMI, the user name is either
fedora
orec2-user
. - For SUSE Linux, the user name is
root
orec2-user
.
- For an Amazon Linux AMI, the user name is
- Press Advanced button to open Advanced site settings dialog and go to SSH > Authentication page.
- In Private key file box select the
.pem
private key file. WinSCP will need to convert the key to its.ppk
format (you can then use the converted.ppk
key for example with PuTTY SSH client). - Submit Advanced site settings dialog with OK button.
- Save your site settings using Save button.
- Login using Login button.
- Verify the host key by comparing fingerprints with those collected before (see above).
If you are managing a large amount of servers, and it is not feasible for you to save a site for each of them in WinSCP, consider using an user script that injects “Open in WinSCP” link to an Amazon EC2 management portal.
Connecting to EC2 instance in VPC
To connect to an EC2 instance in an Amazon VPC, you can tunnel through a NAT instance.
For details, see a guide to connecting to an SFTP server which can be accessed via another server only.
Connecting using AWS SSM (Session Manager)
In Host name, specify your Instance ID.
Press Advanced button to open Advanced site settings dialog and go to Connection > Proxy page. There:
- For Proxy type, select Local.
- In Local proxy command, specify:
aws ssm start-session --target %host --document-name AWS-StartSSHSession --parameters "portNumber=%port" --profile <aws profile> --region <region>
- For Do DNS name lookup at proxy end, select No.
Further reading
- Guide to uploading files to SFTP server;
- Guide to automating operations (including upload);
- How do I change user after login (e.g. su root)?
- Official AWS guide for connecting using PuTTY/WinSCP;
- Official AWS guide for transfering files using a WinSCP
- Guide to connecting to Google Compute Engine server with SFTP;
- Guide to connecting to Microsoft Azure Service with SFTP or FTPS.
Advertisement
- Using
cloud-init
script.Back