Search code examples
javasshamazon-ec2sftpjsch

Where to get username/password for JSch connecting to AWS EC2?


I am trying to connect to EC2 instance with the following Java code:

Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
Session session = jSch.getSession(username, EC2-IP, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();

In EC2 IAM web console I created an user/password and I can use it to login to AWS management console. But when I use this user/password in the above Java code, I receive:

Error: com.jcraft.jsch.JSchException: Auth fail

Solution

  • SSH/SFTP username for Amazon EC2 is:

    • For an Amazon Linux AMI, the user name is ec2-user.

    • For a RHEL AMI, the user name is ec2-user or root.

    • For an Ubuntu AMI, the user name is ubuntu or root.

    • For a Centos AMI, the user name is centos.

    • For a Fedora AMI, the user name is ec2-user.

    • For SUSE, the user name is ec2-user or root.

    See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-linux-inst-from-windows.html


    To authenticate, use a private key that you have associated with the instance, when you were creating the instance.

    See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html


    In general, you should first setup and test connection with some standalone SFTP/SSH client. And only then try to connect with your code.

    I have written a guide for connecting to Amazon EC2 with WinSCP SFTP client. If you follow it and make a successful connection with WinSCP, it should be then easy to do the same with JSch code.