Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

On my debian maschine I deleted /bin/bash by accident. Is there a way to get it back without reinstalltin the machine?

If it helps. I'm still logged in. Guess once I'm out I cannot log in since it's my login shell.

share|improve this question
1  
First, I'd change my login shell for anything else in /etc/shells. This would be my first move (don't use /sbin/nologin ;-)). Then I'd fix it. –  CloudWeavers Nov 23 '12 at 13:03

3 Answers 3

up vote 21 down vote accepted
ln -s /bin/sh /bin/bash
apt-get install --reinstall bash
share|improve this answer
    
Great! You made my day! –  BetaRide Nov 23 '12 at 13:06
    
Do you have to destroy the link, or will it just get overwritten? –  chovy Nov 25 '12 at 21:15
    
It will get overwritten. –  FINESEC Nov 25 '12 at 23:57

if you're still logged in, then issue the following command:

sudo apt-get install --reinstall bash
share|improve this answer
    
This will not work, see my answer. –  FINESEC Nov 23 '12 at 13:02
    
you are right. unless a person changed the /bin/sh link from dash to bash, as some do, in which case both our solutions will hiccup. –  Waleed Hamra Nov 23 '12 at 13:21

New Linux systems generally will not boot without a functioning bash. If you lost bash and you attempt to reboot you may be (temporarily) blocked out of your system.

if you still have an open session skip this next paragraph about booting from a live cd and go right to the following paragraph. otherwise try:

If youve lost bash and none of your user accounts or root can login and your system wont even boot, then you can try booting in to the system with a live cd, mount the hard drive that your system is on navigating to /bin on that system, say 'cd /mount/sda1/bin', moving bash to bash.bak 'mv bash bash.bak' then making a link from some shell that is working to bash such as 'ln ksh bash' exiting and trying to reboot. it may or may not work, it does for me on slackware and debian. once you get the system booted, change the login shell for user and root using the chsh command then remove the link from ksh to bash.

after that some of these suggestions may work.

sh itself is ancient and in some linux systems /bin/sh is actually there for legacy support and is just a link to /bin/bash, so changing your login shell to /bin/sh may not work.

If you still have a root terminal, youre in good shape. Use the chsh command to change your login shell to one that you still have. Ive used /bin/ksh for this. note your system probably wont BOOT this way but at least you can now do rescue changes.

If you only have a user shell, use chsh to change your login shell to something that is still working on your system (ksh) then try and run 'sudo chsh'.

If your sudo is working, you will be given a chance to change the login shell for root. if it isnt, go to the beginning of this post and follow the instructions to make a soft link from a working shell like ksh to bash and take it from there.

Once this is done, you can login as root and try and use your usual package managers to remove broken bash installation and install or reinstall a working version or compile a new installation from source or in the case of slackware, put the bash package in / and extract it...voila, instant bash. theres probably ways to extract bash from top level directories in to working systems on other distros but i havent tried.

note, some installation procedures for getting a working bash on the system end up putting a working bash executable in /bin but it isnt named bash, in which case youll have to link that to 'bash'. ultimately with the right combination of remove install cycles youll get it to do it but it took me a couple rounds on most tries to get it to do it on its own. most commonly i was getting /bin/bash4.new as the executable which i had to link to /bin/bash before i was able to fix it with the usual package manager tools.

If youre running package management tools from a ksh shell on a system with no bash, you may get a command not found error even as root unless you call them specifically with something like 'ksh apt-get' or 'ksh installpkg', and even then depending on the package you are trying to install....in this case for me it was bash, the installation script fails for some versions because it needs bash to run the installation script.... go figure. a dependency of itself. that can be fixed by going in and rewriting the installation scripts but really who wants to bother, its easier to do other things.

you may need to run a few rounds of install uninstall install to get it on cleanly.

alternately ... in slackware i was able to put the slack package in / then extract it and get a working bash executable in /bin

if these dont work, you can try removing any dead bash executables in /bin and grabbing the bash source, and compiling/installing it from source.

alternately you have probably not thought of it but many distros have a reconfigure utility you can call from the cli which will let you modify an already installed system, or you can do that by booting the installation cd/usb/media that came with your distro that you installed from. this also probably has a way to go in and modify your system as an option. ive reinstalled bash from there or by calling whatever the reconfigure program that it provides from the cli. this works in debian and slackware.

lastly you can boot the live cd that matches your system, provided you still have the same kernel, and chroot in to your system and reinstall it using the normal package management tools from there. ... this i have not tested.

one way or another, you probably eventually want the new bash to be a package that is recognized by whatever package manager you have on your system otherwise compiling new programs may complain that they cant find bash (probably not but its a potential issue for poorly written programs)

a wordy answer but its a scary thing to have an unbootable system and there are several approaches to fixing this.

share|improve this answer
    
I voted you up, but suggest to try to make your writing quality better, especially in such long posts. –  peterh Jan 10 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.