Skip to content

Instantly share code, notes, and snippets.

@RELATO
Created August 1, 2015 11:22
Show Gist options
  • Save RELATO/0f87effa0be07a52858c to your computer and use it in GitHub Desktop.
Save RELATO/0f87effa0be07a52858c to your computer and use it in GitHub Desktop.
Top section is for regular Ubuntu installs. Bottom section is for EC2 installs.
Ubuntu Login as Root (regular Ubuntu VM or PC – not using AWS EC2)
Login with main user
Now gain root access to bash, by emulating it with sudo
# sudo -i
Then type
# passwd root
Set password for root and remember it
Also unlock the root account (although the above “passwd root” command might already do that)
# passwd -u root
To get it back to original default status (regular Ubuntu VM or PC -not using AWS EC2)
Login as Main user
# sudo passwd -dl root
-d deletes the password, so that we are back where we started with root not having a password
-l locks the account so that we are back where we started with a locked account (Thats a lower case L)
If you dont want to delete the password, but just lock the user “sudo passwd -l root”
Ubuntu Login as Root if using AMAZON EC2
Login to “ubuntu” user
Now gain root access to bash, by emulating it with sudo
# sudo -i
Then type
# passwd root
Set password for root and remember it
Also unlock the root account (although the above “passwd root” command might already do that)
# passwd -u root
Now for EC2 only stuff, Amazon Ubuntu AMI makes it so that you cannot login to Root with your password (only with your KEY unless you change the /etc/ssh/sshd_config file.. however thats not what we are here to do). They make it so that root cant login with the KEY as well. To fix that:
Go into your /root/.ssh/authorized_keys file and remove the first section that says:
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=”echo ‘Please login as the user \”ubuntu\” rather than the user \”root\”.';echo;sleep 10″
But keep anything thats after that will be like “ssh-rsa …” or “ssh-dsa …”, it will probably be rsa as thats what amazon uses (or at least thats what my key is like).
NOTE: that line applies those settings to people using that key (which us the main account of that ec2 instance with our main amazon ec2 key)
So example
# vi /root/.ssh/authorized_keys
You should see something like this
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=”echo ‘Please login as the user \”ubuntu\” rather than the user \”root\”.';echo;sleep 10″ ssh-rsa AAAAB3NzaC1yc2E0000DAQABAAABAQC9+9P2K0Fxa/tWoelrsCOgZt5lt2hjgwhPzCZG49eyWaTYYhMGLYOnj0rdzlEHk997HpZ2piF9Ftfdx5Hx71dMryv0FqOITvRay/hFCVMXd6diStSHp9eWKx0iULWy0aaaSpWazlgdvJUr6C7Aolt2TU9Gcj+bIxH4lv3RCTpLLLtlqhBhv8wb24xKSw+khpDuy83zWKkzRsZpIpYXVVEslUZD491yTC9xTg0tn2XRVeC82fWCdmR6bwYMbPY/EhTnTjJCwLirRX3123+hWahddyYD6brfE8yX6liRLLFPlBsxHNkAreisFO3EzBqwKNEqQH1EvMWyHLJx9v672Z MyCrazyFunKey-normal
Remove this part no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=”echo ‘Please login as the user \”ubuntu\” rather than the user \”root\”.';echo;sleep 10″, using the x key (the x key deletes one character at a time.)
Remove the space after the 10″ but before the ssh-rsa. So that ssh-rsa is starting the new line, and not a space character starting the new line.
So that it looks something like this
ssh-rsa AAAAB3NzaC1yc2E0000DAQABAAABAQC9+9P2K0Fxa/tWoelrsCOgZt5lt2hjgwhPzCZG49eyWaTYYhMGLYOnj0rdzlEHk997HpZ2piF9Ftfdx5Hx71dMryv0FqOITvRay/hFCVMXd6diStSHp9eWKx0iULWy0aaaSpWazlgdvJUr6C7Aolt2TU9Gcj+bIxH4lv3RCTpLLLtlqhBhv8wb24xKSw+khpDuy83zWKkzRsZpIpYXVVEslUZD491yTC9xTg0tn2XRVeC82fWCdmR6bwYMbPY/EhTnTjJCwLirRX3123+hWahddyYD6brfE8yX6liRLLFPlBsxHNkAreisFO3EzBqwKNEqQH1EvMWyHLJx9v672Z MyCrazyFunKey-normal
Now save and exit
ESCAPE :wq!
Now try to login with root and you will be able to! (might need to reboot but probably not)
How to get it back to original default status (AWS Amazon EC2)
First lock your account
Login as Ubuntu
# sudo passwd -dl root
-d deletes the password, so that we are back where we started with root not having a password
-l locks the account so that we are back where we started with a locked account (Thats a lower case L)
If you dont want to delete the password, but just lock the user “sudo passwd -l root”
Now go back to that file
sudo vi /root/.ssh/authorized_keys
And append this:
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=”echo ‘Please login as the user \”ubuntu\” rather than the user \”root\”.';echo;sleep 10″
To the first line (And to any line that might have a key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment