Looking for the Amazon Lightsail Ubuntu 16.04 LTS Upgrade Guide?
While writing my article on installing OLS on Amazon Lightsail, I experimented with upgrading Ubuntu 18.04 LTS to Ubuntu 20.04 LTS using the guide at nixCraft. Great guide, but it fails to mention that upgrading Ubuntu 18.04 LTS to Ubuntu 20.04 LTS on Amazon Lightsail breaks the Lightsail Browser SSH client. The error message simply says:
Your instance encountered an error and closed the connection. Try again or contact customer support. UPSTREAM_ERROR [515]
As a result, here is my guide, which fixes the error before it happens (without contacting support). All the commands in this article can be run by connecting to the instance from the Amazon Lightsail dashboard with the Lightsail Browser SSH client.
Check Swap Space
In my tests, I used the smallest Amazon Lightsail instance, with 512MB RAM. The installation was Ubuntu 18.04 LTS OS-only, so it didn’t have any default swap space. The first upgrade I performed, without swap space, was killed by the system due to running out of memory.
Check that you have swap space!
cat /proc/swaps
If your instance does not have swap space allocated, you can run the following command to allocate 2GB of swap space. Swap space should be twice the size of the instance’s RAM, unless the instance has more than 2GB of RAM. It is not necessary to reboot after allocating swap space – it will start to be used immediately after being made active.
TL;DR
sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile && sudo sed -i '$ a\/swapfile swap swap defaults 0 0' /etc/fstab
Long Version (explanation of the above command string – do not run as separate commands)
Run the command as the root user.
sudo
Allocate space to a file.
fallocate
Specify the length of the file.
-l
Specify the length as 2GB.
2G
Create the file in / with the name “swapfile”.
/swapfile
If the previous command executed successfully, do the next, else stop.
&&
Run the command as the root user.
sudo
Change mode for a file.
chmod
Change the mode to 6(Owner can Read&Write,Not Execute) 0 (Group cannot Read, Write or Execute) 0 (Public cannot Read, Write or Execute).
600
The file that we want to change the mode for is “/swapfile”.
/swapfile
If the previous command executed successfully, do the next, else stop.
&&
Run the command as the root user.
sudo
Prepare a block to be used as swap.
mkswap
The block we want to prepare is “/swapfile”.
/swapfile
If the previous command executed successfully, do the next, else stop.
&&
Run the command as the root user.
sudo
Enable a device of file to be used as swap space.
swapon
The file to use as swap space is “/swapfile”.
/swapfile
If the previous command executed successfully, do the next, else stop.
&&
Run the command as the root user.
sudo
Run SEd
sed
Make changes in-place (in the file we’re editing)
-i
The expression for changes we want to make (add the string to the end of the file)
(Write “/swapfile swap swap defaults 0 0” to /etc/fstab. Fstab is the file system table. The line adds a filesystem of /swapfile with mount point “swap” as type “swap” with options “default”, dump disabled (0) and pass disabled (0). This filesystem will be mounted at reboot because it has been added to fstab.)
'$ a\/swapfile swap swap defaults 0 0'
The file we are editing is
/etc/fstab
Upgrade Ubuntu 18.04 LTS’s Packages
Ubuntu 18.04 LTS will not upgrade to Ubuntu 20.04 LTS without having its own packages updated first.
TL;DR
sudo apt update && sudo DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confold,confdef" -y full-upgrade && sudo reboot
Long Version (explanation of the above command string – do not run as separate commands)
Run the command as the root user.
sudo
Run the Advanced Package Tool.
apt
Update the list of available packages from sources.
update
If the previous command executed successfully, do the next, else stop.
&&
Run the command as the root user.
sudo
Tell the package manager to run in non-interactive mode.
DEBIAN_FRONTEND=noninteractive
Run the Advanced Package Tool.
apt
Use the following options.
-o
Set the dpkg options to force using of the old configuration file if it exists, else use the default configuration for the package.
Dpkg::Options::="--force-confold,confdef"
Assume yes to all prompts.
-y
Do a full system package upgrade – if removal of installed packages is required, remove them.
full-upgrade
If the previous command executed successfully, do the next, else stop.
&&
Run the command as the root user.
sudo
Reboot the system
reboot
Upgrade Ubuntu 18.04 LTS to Ubuntu 20.04 LTS
We are now ready to upgrade Ubuntu 18.04 LTS to Ubuntu 20.04 LTS and fix the issue with the Amazon Lightsail Browser SSH client at the same time.
TL;DR
sudo do-release-upgrade -d -f DistUpgradeViewNonInteractive && sudo sed -i -e '$ a\KexAlgorithms=+diffie-hellman-group14-sha1' -e '$ a\CASignatureAlgorithms +ssh-rsa' /etc/ssh/sshd_config && sudo reboot
Long Version (explanation of the above command string – do not run as separate commands)
Run the command as the root user.
sudo
Run the upgrade manager.
do-release-upgrade
Upgrade to the next development release.
-d
Upgrade with the following frontend.
-f
Tell the upgrade manager to run with the non-interactive frontend (old configurations are kept if they exist – if they do not, the default configuration is used).
DistUpgradeViewNonInteractive
If the previous command executed successfully, do the next, else stop.
&&
Run the command as the root user.
sudo
Run SEd
sed
Make changes in-place (in the file we’re editing)
-i
The changes are part of a string of expressions
-e
The expression for changes we want to make (add the string to the end of the file)
'$ a\KexAlgorithms=+diffie-hellman-group14-sha1'
The changes are part of a string of expressions
-e
The expression for changes we want to make (add the string to the end of the file)
'$ a\CASignatureAlgorithms +ssh-rsa'
The file we are editing is
/etc/ssh/sshd_config
&&
Run the command as the root user.
sudo
Reboot the system
reboot
Amazon Lightsail Browser SSH Client Error 515 After Upgrade
Using this method, we will have avoided the “Upstream Error [515]” message in the Amazon Lightsail Browser SSH client.
Why Does Upstream Error [515] Happen?
Ubuntu 18.04 LTS uses OpenSSH Version 7.6, whereas Ubuntu 20.04 LTS uses OpenSSH Version 8.2. In OpenSSH Version 8.2, the algorithms used by the Amazon Lightsail Browser SSH to connect to and communicate with the instance have been deprecated. By adding these methods back through the SSH Configuration File (/etc/ssh/sshd_config), we supplement the default OpenSSH Version 8.2 settings to include the deprecated methods.
I Didn’t Follow This Guide. How Do I Fix Upstream Error [515]?
Connect to your Lightsail instance using PuTTY and run the following in the terminal:
sudo sed -i -e '$ a\KexAlgorithms=+diffie-hellman-group14-sha1' -e '$ a\CASignatureAlgorithms +ssh-rsa' /etc/ssh/sshd_config && sudo systemctl restart ssh