기본기/AWS

[AWS] EC2 ssh 접속오류 - Failed to connect to your instance

dohyeon2 2024. 8. 9. 15:15

목차

    visual studio code에 AWS EC2를 SSH로 연동해서 열심히 작업하던 중 갑자기 SSH 연결이 안되는 문제가 발생했습니다.

    제 데스크탑 문제일 수가 있어서 AWS 콘솔에서 직접 로그인 해보았는데 다음과 같은 에러가 발생했습니다.

    Failed to connect to your instance EC2 Instance Connect is unable to connect to your instance. Ensure your instance network settings are configured correctly for EC2 Instance Connect. For more information, see EC2 Instance Connect Prerequisites at https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-prerequisites.html.

     

     AWS 콘솔에서도 접속이 불가능하므로 제 컴퓨터 문제는 아니였습니다. 따라서 기존 인스턴스의 네트워크 설정에서 뭔가 잘못됐다고 생각해볼 수 있었습니다.

     

    시도 1. EC2 재부팅

    ==> 실패

     

    시도 2. EC2 보안정책 22번 port 개방

    ==> 이미 열려있었습니다...

     

     

    시도 3. Edit Instance user data

    ==> 성공!!!!

    위 오류를 구글링 해보니 저와 동일한 오류를 겪으시고 해결한 블로그를 찾았습니다.

    해결방법은 다음과 같습니다.

    • 인스턴스 중지
    • 인스턴스 설정-사용자 데이터 편집-아래 내용 복붙

    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    #!/bin/bash
    ufw disable
    iptables -L
    iptables -F
    --//

    각 줄이 뜻하는 바는 다음과 같습니다.

     

    1. MIME type 및 경계  선언: 이 줄은 내용이 여러 부분으로 나뉘어 있으며, 경계가 '//'로 설정되어 있음을 말합니다.

    Content-Type: multipart/mixed; boundary="//"

    : 이 줄은 내용이 여러 부분으로 나뉘어 있으며, 경계가 '//'로 설정되어 있음을 말합니다.

     

    2. Cloud-Config 설정: 첫 번째 블록은 cloud-config 형식의 설정 파일로, 인스턴스 부팅 시 마지막 단계에서 scripts-user 모듈을 항상 실행하도록 지정합니다.

    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]

     

    3. 쉘 스크립트 실행: bash 쉘 스크립트로, UFW(방화벽)를 비활성화하고, iptables 규칙을 목록화한 뒤 초기화합니다. 이 스크립트는 인스턴스 부팅 시 실행됩니다.

    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    #!/bin/bash
    ufw disable
    iptables -L
    iptables -F

     

    출처: https://stackoverflow.com/questions/41929267/locked-myself-out-of-ssh-with-ufw-in-ec2-aws

     

    Locked myself out of SSH with UFW in EC2 AWS

    I have an EC2 Instance with Ubuntu. I used sudo ufw enable and after only allow the mongodb port sudo ufw allow 27017 When the ssh connection broke, I can´t reconnect

    stackoverflow.com

     

     

    성공적으로 EC2 인스턴스를 복구했습니다 :)

    (지우고 다시해야할까봐 조마조마했습니다....) 

    '기본기 > AWS' 카테고리의 다른 글

    [AWS] ELB 및 ASG  (0) 2023.12.19
    [AWS] EC2  (0) 2023.12.06
    [AWS] IAM  (0) 2023.12.04
    [AWS] Region과 Availability Zone  (0) 2023.12.04