How do you change the public key of a Oracle Cloud Instance? - oracle-cloud-infrastructure

I accidentally deleted my public and private key and had to generate new SSH keys due to not being able to restore the keys (and not having a backup anywhere). How do I change the public SSH key then of my Oracle Cloud instance?
Terminating the instance and remaking it isn't an option, and I've tried looking online but wasn't able to find much. Any help would be appreciated.
Thanks

Some background
Found a solution! Just so people are aware, there are methods online that involve connecting to the machine via VNC, but for me personally it felt very trial-an-error, when pressing buttons at the wrong time, and it ended up not working properly (VNC didn't display recovery mode for me, just a blank screen after selecting it).
Summary
This guide involves: Creating another machine (as incl. in free tier anyway), detaching the boot volume drive from the machine and attaching it to the machine just created, to do editing to change the keys over, then attaching the drive back up.
Create another VPS (Oracle have them incl. > free tier)
I deleted one of my other VPS' in the Oracle panel (that was a free machine - as I didn't need it and wasn't using it) and created it again anew (I made sure to delete the old boot volumes before continuing).
(This solution is assuming your using Ubuntu 20.04, but this will probably work for other OS's as well)
Basically from there,
I powered off the machine I wanted to change my SSH key of.
After fully being powered off, just detach the boot volume from the VPS, and attached it as a block volume to the machine just created.
Login to the machine via SSH, and run the connection commands by hitting the three dots (image below) and viewing the connection commands, to connect the drive up.
Editing files on the drive & mounting process
Then by running blkid (or sudo fdisk -l for a more friendly view)
you're able to see what drives are available for mounting. So then you just make a folder and simply type:
sudo mount [drive path e.g. /dev/sdb] [folder path e.g. ./drive]
Edit the file at /home/ubuntu/.ssh/authorized_keys, or however your machine is configured (Oracle by default disallows root, but if you've edited your configuration it's up to your end).
Then, simply go to the relevant path to be able to unmount the drive, umount [folder path e.g. ./drive]
Run the disconnect commands for the drive from the panel.
Then, simply detach the drive from your other machine and reattach it back to the original machine. Wait till it's fully attached and then start the machine again.

You can create a console connection, connection to it, then reboot the instance (through OCI console), and get to GRUB in the console connection... a few more steps and you can upload a new ssh key: https://docs.oracle.com/en-us/iaas/Content/Compute/References/serialconsole.htm

Related

I need a foolproof checklist for hosting an already built e-commerce site on Amazon Web Services

I have built an e-commerce website on my local computer that uses Django version 2.2 and python 3.7.
The website consists of:
fancyfetish is the main project directory.
The apps, (cart, users, baseapp, products, blog) are all stored in their own directory 'apps.
Within the settings folder I have three settings files:
- production.py
- base.py
- development.py
The static file in the main directory is where I put collectstatic files.
Media is where I store externally uploaded images (product images for example)
Docs is just random bits like a hand drawn site layout.
Static files like JS and CSS are stored within baseapp, within apps.
I want to host this website on Amazon Web Services, and I assume I need to use Elastic Beanstalk. I went through the process of trying to host with free version of EB, installed the EB CLI, and after using eb create and eb deploy on the CLI my website appeared.
However, the static files didn't load properly in the first instance because I had not properly configured DJANGO_SETTINGS_MODULE. I have now done this. But before deploying I added eb migrate functionality so that I could also migrate my database.
This seems to have messed everything up. I can no longer deploy because there is a DATABASE error, which I expected. The error said 'Not able to connect to MySQL database through 'localhost'. Well, of course it cant.
So, in order to deploy my site on AWS I needed to configure the databases, because with the eb migrate functionality it will no longer deploy without trying to also connect to my database using the settings I have configured.
I have so far, whilst in development mode, connected my project to MySQL and everything is running perfectly on localhost, with my models transferring beautifully to the database just as I would like.
I worked out that I need to create a database on AWS, obviously. So I set up an RDS. I didn't link it to my deployed application because it would appear that the application doesn't have an environment that I can see when I log into my console. So where my project has been deploying to I don't know, because it doesn't look like the CLI version is connected to the online version in my console.
So I thought I'd deal with that problem later and work out how to make a database, which I managed to do. However, migrating the database I already have up and running on MySQL to my RDS database seems impossible, and there are not very good instructions. Let alone trying to then connect said database to my deployed application, which doesn't seem to sync with my local app.
So, I have ended up deleting everything because I was becoming so confused, with so many new directories (.ebextensions etc etc) and a database that wont connect, a project that won't deploy, a database that wont point to my project etc. I ended up created an EC2 folder and all sorts, getting myself massively confused with what I actually need to do to make this whole thing work.
If any part of this ramble makes any sense to anyone out there, and you yourself have managed to deploy a larger django project to AWS and keep your existing databases then please do let me know. But I have a feeling this may be a long shot.
Basically I need a step by step list of what to do to deploy:
For example:
1) Create an elastic beanstalk instance
2) Create an environment on CLI that syncs to the one in my AWS console
etc
etc
(With how to's if you possibly have the time!)
Thank you, and I am so sorry for being so confused by something that may be simple
Edited to show my process:
I have built a directory called .ebextensions with a file within it called django.config with the following content:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: fancyfetish/wsgi.py
I have run the following command:
eb init -p python-3.6 fancyfetish
There was no output as a result of this in the terminal, however a directory was created called .elasticbeanstalk with one file in it called config.yml
I then typed eb init to create an SSH key pair and there was no output from this command at all:
As you can see I have tried doing this several times.
Instead I created a key pair manually within AWS console and a file automatically to my computer called keyname.pem
I then typed into the console
chmon 400 path/to/key/keyname.pem
This provided no output on the terminal so I cannot know if it worked.
I moved the downloaded SSH file into the .SSH directory in the Home directory of my computer, and then in the terminal typed:
eb init -k nameofkey
The output was:
WARNING: Uploaded SSH public key for "fancyfet" into EC2 for region us-
west-2.
I then went on to type
eb create fancyfet-env
And an environment was created with the following output:
I know that this has to do with databases and connecting to MySQL.
I then typed:
eb deploy
With the following output:
So now comes the bit where I get stuck, successfully creating a database that connects to my already existing database that is populated with database in MySQL, and connecting the project to the database.
HELP!(Thank you so much!)

Customizing a GCE Ubuntu VM image

I have a Google Cloud Platform account that I access from a VirtualBox VM. I am using the Google Compute Engine for a project that I am currently working on, and I had to create a custom image based on the Ubuntu 14.04 image that's available there.
I made changes to the Ubuntu image by ssh'ing into an Ubuntu 14.04 instance, (from my Vbox VM terminal) installing the Matlab compiler runtime, and downloading some other files that I needed. I created the custom image by following the steps according to the documentation.
However, now the changes I made are only available to me when I SSH from my Vbox VM terminal. I need to be able to run a certain matlab program Via startup scripts, how can I make it so that all users using this image have access to the customizations I made? Is there a way I can do this without having to make the edits by ssh'ing from the developers console and redoing all the changes?
EDIT: I don't think I was very clear so Ill give some examples. say my Google account is alexanderlang. When I ssh into an instance created from my custom image from the developers console, bash prompt looks like:
alexanderlang#myinstance $
My Vbox username is alex, and when I ssh into the same instance from my vbox terminal, bash prompt looks like:
alex#myinstance $
alex#myinstance can run matlab programs, but alexanderlang#myinstance cannot. I'm talking about the same instance, created from the same image. I think this might have something to do with the ssh keys for my custom image, but I don't know how to change or remove those keys.
When you connect to your VM instance via ssh by using either Developers Console or gcloud, the user account is dynamically created (if it doesn't already exist) by setting metadata on the VM. The question is: how does each tool choose your username?
When you use Google Developers Console, the only information it knows about you is your Google Account name, so it uses that, e.g., <first-name>_<last-name> or similar.
When you connect to your instance via gcloud, it knows the value of $USER so it uses that instead.
Note that in either case, your account has passwordless sudo access, so if you want to switch from one account to the other, you can run:
sudo su alex
while logged in as alexanderlang and then you have access to all the programs that alex does.
Similarly, you can run:
sudo su alexanderlang
while logged in as alex to do the reverse.
Startup scripts run as root. To run commands as another user, you need to do two things:
change to that username
run commands as that user
sudo su alex will create a new shell and hence ignore the rest of the script (until you manually exit the user shell, which is not what you want).
You can use sudo su alex -c 'command to run' but since what you want to run is a complex script, you need to first save the script to a file, and then run it.
Your options are:
pre-create the shell script to run
dynamically generate it from the startup script
Doing (1) is easy if the script never changes. For frequently-changing scripts (and it sounds like, many dynamically created VMs), you want to use option (2).
Here's how to do this in a startup script:
cat > /tmp/startup-script-helper.sh <<EOF
# ... put the script contents here ...
EOF
sudo su alex -c '/tmp/startup-script-helper.sh'
You can use Packer to create a derived image from a stock GCE VM image. Packer will let you do the following very easily:
boot a GCE VM using an image you specify
run some customization step, e.g., shell script, or Chef/Puppet/etc.
save the resulting image in your Google Cloud Platform project
Then, you can boot any number of new VMs using your newly-created image.
Note that since your VM image will be stored on Google Cloud Storage, you will be charged for the space it uses. Current pricing for Google Cloud Storage standard class is USD $0.026 / GB / month. A typical VM image should be less than 1GB.
You can see a complete example of how I used Packer to build VMs and pre-installed Ambari on it via my GitHub repo.

downloading the datasciencetoolkit.org server to use (R, Vagrant, VirtualBox)

I am running R on a Windows computer. I find myself making many JSON requests to datasciencetoolkit.org and I recently discovered that datasciencetoolkit.org can be downloaded to a computer as a "virtual server." I imagine this could cut down on my calculation time significantly, not to mention that it won't burden someone else's servers with my queries.
The trouble is, I know nothing about creating my own virtual machine. Here is the guide for beginning. I am planning on using Vagrant since I don't believe Amazon is free (please correct me if I'm wrong.) I am going to keep it to 3 questions.
1) I already downloaded Vagrant and ran the step that says:
vagrant box add dstk http://static.datasciencetoolkit.org/dstk_0.50.box
...from within my own computer, not a virtual machine. I installed it to my own computer I think. Did I make a mistake? Do I need to do that over again?
2) How do I set up Vagrant on VirtualBox? I don't even know what that should look like.
3) Do I need to install R on the Virtual Machine as well? I am assuming that once all is done I will just need to run my requests in the virtual machine by typing in the http://localhost:8080 address, but I will cross that bridge when I get to it. Right now I just want to set up my virtual machine with the datasciencetoolkit.
Thank you!!
Installing Vagrant for Windows
Vagrant is a tool to help you use VirtualBox more easily and consistently.
VirtualBox is a free program which hosts and manages virtual machines. Vagrant
looks for a file called Vagrantfile, which is a construction plan that takes a
basic or pre-configured system image, configures it according to your plan, and
then hosts it via VirtualBox.
I assume you have some "power user" comfort with Windows. If you find it
difficult to model what's going on, it may be possible that this toolchain could
bring on more pain than doing things manually.
Download a ssh client, such as Putty. Just in case.
Before you do anything, make sure you have both the latest 'VirtualBox' AND
'VirtualBox Extension Pack' installed.
Download and install Vagrant. While installing, make sure to choose the
option to add Vagrant to your Windows PATH variable.
Create an empty project directory, such as at C:/Projects/R/, and navigate
to it.
Right-click inside the folder to bring up the standard system dialogue, and
select Open command window here. A command line should pop up.
Type vagrant init http://static.datasciencetoolkit.org/dstk_0.50.box. You
should see this success message:
A 'Vagrantfile' has been placed in this directory. You are now ready to
'vagrant up' your first virtual environment! Please read... [and so on].
Confirm that a Vagrantfile has indeed been created in your project
directory, which I assume to be at C:/Projects/R/.
If you receive a fail message at this step about not recognizing the command
called vagrant, then something may be wrong with your PATH variable.
Edit that Vagrantfile with a basic text editor and completely replace with this:
Vagrant.configure(2) do |config|
config.vm.box = "http://static.datasciencetoolkit.org/dstk_0.50.box"
config.vm.network "forwarded_port", guest:80, host:8080
# config.ssh.username = 'vagrant'
# config.ssh.password = 'nova'
config.ssh.insert_key = true # important: touch if you understand.
config.vm.provider "virtualbox" do |v|
v.gui = false # optionally brings up VirtualBox visual interface
v.name = "vbox_vagrant" # name that VirtualBox will use for your virtual machine
v.memory = 2048 # megabytes ram
v.cpus = 2 # cores
end
end
Type vagrant up, which will start downloading an image. Your image could
take a few hours. This command will only be slow the first time. After the
initial download, your virtual machine will then launch via VirtualBox, but
the VirtualBox visual interface won't launch because we set the option above.
Time to check if it works. In the command console, which should still be
pointed to the correct project directory, type in vagrant ssh. You should
now have command-line access to your virtual machine. Success! If you're
saying there's a server in there, then try going to your browser, visit
and the URL localhost:8080. I'm speculating about your situation so it may
not work.
To stop the virtual machine, do vagrant halt. To delete it, type vagrant
destroy. To check the status, type vagrant status. People create and
destroy virtual machines all the time, because once you download the 'box',
these steps become very fast.
I haven't checked if the image you downloaded already as R installed, but I
presume so. If not, then you need to do it on the virtual machine.

Gnome 3 automatic execution of a script that needs network

my old father is using ubuntu-gnome. He has no static ip address. In order to perform remote administration, I need to know his ip. I was using dyndns free account (configuration in the adsl modem), but this will stop working in a couple of days.
I would like to run a script each time he logs in to publish his ip on my website. I have tried to put a script on the boot, but the network is not available. It seems that it is gnome 3 that starts the network, but I do not know much about gnome 3.
How should I do to have my script run automatically as soon as the network is available ?
One possible non-elegant solution for this is to put your script in his cron to run every X minutes :)
Looking to mine /etc/NetworkManager/ looks like there is a folder dispatcher.d that I think it'll do what you want. Just experiment with a bash/perl/python w/e script in there set the permission appropriately. You can find the UUID in the system-connections/ folder. More information is available in man networkmanager.
EDIT: Look what I found: https://askubuntu.com/questions/13963/call-script-after-connecting-to-a-wireless-network. Seems like this is exactly what you want.
The easiest way is to use another dynamic DNS service. I used to use my own. You could also put curl or wget command to cron or create a systemd service that will call that command periodically. As a target you would have to use your machine with a web server where you can see the IP in your logs.
It is not Gnome that connects the network, it is a system service called NetworkManager. It tries to connect at boot if possible. In some cases it waits for wireless signal, in other cases it waits for a user password. I recently verified that in Fedora, NetworkManager properly implements the systemd's network-online.target but it may have yet to be fixed in other distributions, see the upstream bug report.
https://bugzilla.gnome.org/show_bug.cgi?id=728965
If you want to run a system service just after boot, you need to use:
[Unit]
...
Wants=network-online.target
After=network-online.target
You could also just run a script that calls nm-online at the beginning to wait for the network connectivity if you can expect the connectivity to come up in reasonable time, otherwise it times out. Such a script can be run from any environment including a user session.
And, as noted already, you can put a script into /etc/NetworkManager/dispatcher.d that will be called on any network configuration change and such a script can then filter connection up events and start the notification script.

Why does my custom beanstalk keep restarting?

I am trying to customize the default AMI of beanstalk, but everytime I get server restarts after some random time. I went so far as not to change anything, but nothing works.
I have tried the following:
find the instance of running beanstalk, create AMI, modify the AMI of beanstalk-crashing
create new instance with same AMI as on beanstalk, create AMI, modify configuration-crashing
I have tried both stopping the instance before creating AMI, and creating AMI of running instance.
Edit: I found the answer here: Can't generate a working customized EC2 AMI from Amazon Beanstalk sample appl
From personal experience, place the health status page to point to a dummy, static .html file. Although not recommended, this will prevent the health checks from restarting the machine and you could make more inside inspection.
AWS captures into the S3 logs only the ones output via java.util.logging. It means all console logging is not transferred.
That said, make sure you define an private key in your environment config, so you could ssh to it easily and see its output (it changes - for Tomcat 7, it is at /opt/tomcat7. For tomcat6, it is under /usr/share/tomcat6)
Just to add to what aldrinleal wrote (can't comment yet): In the past, I would often find a failed Healthcheck would also disable my site. By which I mean: If you have the health check on your actual app and that app threw an exception, you wouldn't actually get to see anything, the environment would just report a failed state. Only after I changed to a static file for the health check, did I manage to see the errors.
Now I obviously this is more a problem with a dev environment and you can always just pull the logs. But especially in the beginning as someone new to AWS/Beanstalk this helped me a lot.