Skip to content

Instantly share code, notes, and snippets.

@nietzscheson
Forked from tracend/aws_ec2_root.md
Created March 7, 2018 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nietzscheson/e862177062394c094232071257b1c8f2 to your computer and use it in GitHub Desktop.
Save nietzscheson/e862177062394c094232071257b1c8f2 to your computer and use it in GitHub Desktop.
AWS EC2: Steps to enable root access with your local key - Inspired by: https://forums.aws.amazon.com/thread.jspa?threadID=86876

Note: replace {{server}} with your domain or ip

  • Login as the ec2-user
ssh -i key.pem ec2-user@{{server}}
  • Switch to administrator
sudo -i
  • Load the configuration file
sudo vi /etc/ssh/sshd_config
  • Make sure these are enabled:
	PermitRootLogin yes
	# Only allow root to run commands over ssh, no shell
	#PermitRootLogin forced-commands-only
	RSAAuthentication yes
	PubkeyAuthentication yes
	AuthorizedKeysFile     .ssh/authorized_keys
  • copy your local public key to the authorized list
cat ~/.ssh/id_rsa.pub | ssh root@{{server}} 'cat - >> ~/.ssh/authorized_keys'
  • Make sure the files have the right permisions
chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh/
  • Restart the service
sudo service sshd reload
  • Exit and try to login again
exit
ssh root@{{server}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment