Locked myself out of SSH with UFW in EC2 AWS

后端 未结 6 902
孤城傲影
孤城傲影 2020-12-04 14:38

I have an EC2 Instance with Ubuntu. I used sudo ufw enable and after only allow the mongodb port

sudo ufw allow 27017

When th

6条回答
  •  眼角桃花
    2020-12-04 15:21

    Here's a little more extended version of the user-data-script thing:

    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
    set -x
    USERNAME="ubuntu"
    ls -Al
    ls -Al /home
    ls -Al /home/${USERNAME}
    ls -Al /home/${USERNAME}/.ssh
    sudo cat /home/${USERNAME}/.ssh/authorized_keys
    ls -Al /etc/ssh
    ls -ld /etc/ssh
    
    sudo grep -vE '^$|^#' /etc/hosts.*
    sudo sed -i -e 's/^\([^#].*\)/# \1/g' /etc/hosts.deny
    sudo sed -i -e 's/^\([^#].*\)/# \1/g' /etc/hosts.allow
    sudo grep -vE '^$|^#' /etc/hosts.*
    sed '/^$\|^#/d' /etc/ssh/sshd_config
    
    chown -v root:root /home
    chmod -v 755 /home
    chown -v ${USERNAME}:${USERNAME} /home/${USERNAME} -R
    chmod -v 700 /home/${USERNAME}
    chmod -v 700 /home/${USERNAME}/.ssh
    chmod -v 600 /home/${USERNAME}/.ssh/authorized_keys
    
    sudo tail /var/log/auth.log
    sudo ufw status numbered
    sudo ufw disable
    sudo iptables -F
    sudo service iptables stop
    sudo service sshd restart
    sudo service sshd status -l
    --//
    

提交回复
热议问题