Bash is arguably one of the most important parts of an Ubuntu machine. Once the Bash is corrupted or not accessible, doing almost any tasks on an Ubuntu machine, especially if you are running the Ubuntu server version that doesn’t have a UI, will be extremely difficult. Whether you accidentally replace the bash file with something else, or one of the malicious programs you installed corrupted your system, recovering Bash is entirely possible. In this article, we are going to take a look at some of the methods you can try to restore Ubuntu Bash.
Before you continue, if you are still logged into your system, DO NOT LOGOUT. Also, if you still have an open terminal, do not close it as well. This will make the process much easier. You will understand why, as you read further.
Case 1 – If you are still logged in to Ubuntu and have a running terminal
As mentioned earlier, if you still have access to your system and if you are still logged in to Ubuntu, you are in luck. These are the easiest methods you could follow in order to restore Ubuntu Bash to its original state.
Method 1 – Copy Bash from running processes
If your Ubuntu system still have a running terminal where you can execute commands, you can simply run the following command in the terminal in order to restore Bash back to a working state.
sudo cp /proc/$$/exe /bin/bash
This command simply copies the Bash file from the running processes and pastes it to the original location of the Bash. That means, Bash will be restored to its original state, and you can start using it again right away. If the process is successful, you will be able to continue using Bash even after you close the terminal. You could verify that the file was copied properly by navigating to the respective directory.
Method 2 – Re-install Bash
Like most of the parts of Ubuntu, you can try re-installing Bash as well. Just run the following commands in order to re-install Bash on your system.
sudo apt-get install --reinstall bash
This command will try to reinstall Bash on your machine. If the process is successful, you are good to go and no more changes are required. If not, you will have to dig deeper to solve the issue following the upcoming methods.
Case 2 – You can’t log in to Ubuntu
If you already logged out of your Ubuntu machine or already turned it off, things are going to get much more complex. You can try one of the following methods in order to recover Bash when you can’t access the terminal anymore.
Method 1 – Using Ubuntu live CD
If you have an Ubuntu live CD/USB, you can simply boot up to the live CD and copy its /bin/bash to your original Ubuntu system. The location of the bash is the same. After copying the file, you can remove the live CD/USB and restart your machine. Once you do that, Bash will come back to normal. Even if you don’t have a live CD, it is pretty easy to create one following the steps given on the official website.
Method 2 – Using Dash shell
On top of Bash, there is also a much simpler shell, Dash available on almost all Ubuntu machines. Which means, you can switch to that for a while and use it to reinstall Bash. Then switch back to Bash again after that.
- Restart your Ubuntu machine.
- Press and hold the Shift key until the grub menu appears.
- Press e to edit the default menu entry.
- Search for the line that starts with Linux.
- Add init=/bin/sh to the end of that specific line.
- Press Ctrl + X.
- You will get a shell where you can run commands.
- Simply run -> chsh username -s /bin/sh in the shell.
- Reboot your Ubuntu machine.
- Once you log back in, run the command to reinstall bash again -> sudo apt-get install –reinstall bash.
- Change your default shell back to Bash -> sudo chsh username -s /bin/bash
That’s all you can try to restore the bash shell on Ubuntu. No matter which version of Ubuntu you are running, one of the above methods will work for you. If none of them seems working no matter what you do (which is extremely rare), then your best bet is backing up your system and simply restoring Ubuntu to a working state using a live CD or USB. I repeat once again, before going through all that pain of restoring your entire OS, make sure that you followed above methods carefully.