본문 바로가기

개발/troubleshooting

[AWS] EC2 ssh 접속 불가 문제 해결

전날에 ssh로 접속하여 작업을 잘 했는데, 갑자기 ssh 접속이 안되는 문제가 있었다.


ssh -i "mykey.pem" ec2-user@ec2-ip.compute-1.amazonaws.com
ec2-user@ec2-ip.compute-1.amazonaws.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

 

키 파일의 경우 보안문제로 권한이 많이 열려있는 경우에 사용이 안되는 현상이 있을수 있어 확인해보았으나, myKey.pem 의 권한은 400으로 설정이 잘 되어있었다.

 

AWS 공식 문서를 찾아보니 아래와같은 문장을 보았는데,

Permissions for /home/instance-user-name/.ssh/authorized_keys must be limited to the owner only.

 

공개키 또한 위에서 말한대로 권한이 너무 많이 열려있으면 안되는데, 생각해보니 전날 /home/ec2-user 폴더의 권한을 chmod -R로 하위까지 770으로 전체 수정을 했었다.

authorized_key 파일을 600으로 다시 돌려 주어야 해결이 가능할 것 같다.

 

해결하기 위해선 새로운 임시 instance 생성후, 기존 instance에서 volume을 분리하고 임시 instance에 붙여서 접근하는 방식을 이용해야한다고 함. 

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html#TroubleshootingInstancesConnectingMindTerm:~:text=To%20verify%20the%20permissions%20on%20your%20instance

 

Troubleshoot connecting to your instance - Amazon Elastic Compute Cloud

The device name might appear differently on your instance. For example, devices mounted as /dev/sdf might show up as /dev/xvdf on the instance. Some versions of Red Hat (or its variants, such as CentOS) might even increment the trailing letter by 4 charact

docs.aws.amazon.com

꽤 귀찮은 작업이기 때문에, user 폴더의 권한을 -R로 변경하는것은 주의 해야겠다.

 

나의 경우는 아직 한게 거의 없어서 그냥 새로 인스턴스를 만들어서 다시 작업하기로 했다.

authorized_key 파일 권한만 돌려주면 /home/ec2-usr 폴더 권한은 상관없을 줄 알았는데, /home/ec2-usr 권한만 770으로 변경했는데도 접속이 안되는 문제가 발생했다.

아예 다른 폴더에 작업을 하는게 좋을 것 같다.