3

I'm trying to connect to my Redhat AWS instance with a port other than 22.

This command works: ssh -i my.pem -p 22 ec2-user@X.X.X.X But this command does not:ssh -i my.pem -p 8157 -vvv ec2-user@X.X.X.X

The second command outputs:

OpenSSH_7.6p1, LibreSSL 2.6.2
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to X.X.X.X port 8157.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file my.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file my.pem-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6

but then just hangs. On the server I'm trying to connect to, however, when I run nc -l 8157 I can see

SSH-2.0-OpenSSH_7.6

If I take out the -vvv part I just get a quick "Connection refused" error.

When I log into the machine and run ssh -p 8157 -vvv ec2-user@localhost

I get the following output:

OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug2: resolving "localhost" port 8157
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to localhost [127.0.0.1] port 8157.
debug1: connect to address 127.0.0.1 port 8157: Connection refused
ssh: connect to host localhost port 8157: Connection refused

Any idea what's going on?

6
  • Have you opened up the firewall or changed the AWS security policies to allow access to that port? Mar 22, 2018 at 23:24
  • If you login to the AWS system and then run ssh ... ec2-user@localhost , does that work? Mar 22, 2018 at 23:24
  • @GracefulRestart Yes, I've opened up the security group on the instance. I've also checked the VPC flow logs and I can see my source and target IP and the 8157 port showing up as "ACCEPT OK", so I don't think it's an AWS firewall issue. Also, if the security group were the issue I wouldn't be able to establish a connect at all, right? The ssh verbose command says Connection established, so I think it's getting through, it's just not finishing.
    – user554481
    Mar 22, 2018 at 23:35
  • @MarkPlotnick What could go in the . . .? I didn't know you could connect to localhost. I'm guessing I would just do ssh -p 8157 ec2-user@localhost?
    – user554481
    Mar 22, 2018 at 23:37
  • Same command - ssh -i my.pem -p 8157 -vvv ec2-user@localhost . This will remove from consideration all (or all but one) firewalls. Mar 23, 2018 at 0:00

1 Answer 1

2

This command works: ssh -i my.pem -p 22 ec2-user@X.X.X.X

But this command does not: ssh -i my.pem -p 8157 -vvv ec2-user@X.X.X.X


Your other side may have opened port in the firewall, but...

Since port 22 works, your server listens on that port.

Re-configure your server to listen on whatever port you wish.


You can do that in the file:

/etc/ssh/sshd_config

With the following setting:

Port 8157

Don't forget to restart your SSH daemon afterwards.

5
  • What do you mean by "Re-configure your server to listen on whatever port you wish." Do you mean change the firewall? My AWS security groups (firewall) are completely open. Are you referring to the ssh daemon's ports? If so, how can I change that?
    – user554481
    Mar 23, 2018 at 14:45
  • @user554481 Suffice that edit? Mar 23, 2018 at 14:46
  • Wow, that worked! Is it not open to all ports by default?
    – user554481
    Mar 23, 2018 at 14:54
  • @user554481 No, it is not.. Mar 23, 2018 at 14:55
  • My problem was, i opened a dhcp sserver, set port to 2223. I was trying to connect to the dhcp port 2223 while my ssh was set on 2222. No wonder the server threw error when I tried to open it at 2222. Your answer helped me realize this. I had to say thank you LinuxSecurityFreak. Your answer showed a path to solving my problem. Thank you so much. I've been pulling my hair out until I got no more left trying to solve this question. Now it's regrowing thanks to your answer. Thank you so much. Nov 16, 2021 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.