07. Console connection through Local - CloudDarsh/OracleCloud GitHub Wiki
Getting into a server if password authentication is not set and we have also lost our public and private key
We have two ways to solve this issue:
1st way --> If our main server is also connected with any other server with the same VCN and both servers are reachable to each other. 2nd way --> If our main server is not connected to any other server.
2nd Way
Use ssh-keygen command to create a set of temporary public and private key.
We have the public and private keys ,
Using cat we can see the public key , open and save it.
@cloudshell:.ssh (ap-hyderabad-1)$ cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCZJE9XQ3yKnl+wBEKU8rX094jQuzhM+jIUyBk/QeZyPMld+7+1GKDZ+4epXjW76Uc13+xL9zdUEM2KKQRJds5HMZ4ZoY/ucfvk1fbQTC3JobsLi1BJUXP0yzMqzhEbOzQumCxyM87/JaQPZnySTVtZC3fU3+JwvFDC+Wyu/ka01cvhA0lE5VVG+a4a/46g5rc+VaF3yLFP2QA6DfCJk7mHGiGuzpW0Dic2UW0I2swLiphGVs/ @3f583cf94c58
--> Now we have to launch the console connection, Go to the compute instance , scroll down --? Console Connection
Now we have to paste the public key which we have created above and create a console connection.
Console connection created --> click on three dots and copy the serial console connection for linux.
Now we have to paste the serial console connection and give yes for the below options, after that we are inside our server.
Now we have to go reboot the server and keep pressing the esc key so that we can enter into the boot mode of the server
Now we have to select the latest kernel
Now we have to press e and go to the last line of the linux command and add init=/bin/bash
After this press ctrl+x
After booting, now we are inside the root user.
We have to go to the below directory, here we will find the real public key which was used when instance was created.
Now to login into the server , we have two ways :-
--> We can use several tools to generate the private key using the above public key.
--> We can use ssh-keygen to create a new public and private key.
We are trying ssh-keygen to create the keys, but we are getting error because we have read only access only.
Now we have to give read write access, then we have to switch to opc user and go to the below directory and run ssh-keygen to generate the new public key
/bin/mount -o remount, rw /
New Public and Private key got created.
Using cat, copy both public and private key and save it.
Now we have two ways to get into the server :
1st process : We have to paste the new public key using vi command in the authorized_keys or else this new public key will not be accepted.
Use cat to see the new public key is added.
Or else if we are unable to edit the authorized_keys, then we have to follow the other way.
2nd Process : We have to go to the root user and try to reset its password.
First take a copy of sshd_config file.
We have to change the password authentication option from No to Yes in sshd_config file , so we have to run this command and also add a parameter as allowusers root.
Command for password authentication : sed -i -s 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
Command for Adding allowusers root : Try using vi first if it is not working we can try the sed command
We can check using cat and verify
Now we have to come out of the console connection using ~. Command , if this command is not working just close the cloud shell and open again.
Now we are outside the console connection and we have to delete the console connection which we created in starting.
Now we have to reboot the server to make these changes reflect in the server.