24

I messed up bigtime with the bashrc file. I was installing Gurobi and I had to set path variables in the bashrc file. So, I replaced all the text in bashrc with the path variables and saved the bashrc file. And I also made sure, if anything bad happens, I have a backup of this file. The original unmodified backup file is in the Downloads folder.

Now when I restarted the system, I was not able to login despite giving the proper password. Hence, I'd like to ask you for some help. What kind of fixes are possible given the fact that I still have the bashrc file in the Downloads directory

15
  • 1
    use grub rescue or a live session to copy it from the downloads dir over the wrong one.
    – Rinzwind
    Oct 26, 2016 at 17:26
  • could you please elaborate your suggestion? I'm still a beginner when it comes to ubuntu
    – crypto
    Oct 26, 2016 at 17:29
  • 1
    Now you got me wondering what happened to the fail-safe option that used to exist on the login screen for this kind of scenario.
    – kasperd
    Oct 26, 2016 at 20:31
  • 3
    Why not log in as root and correct your user's .bashrc file?
    – jamesqf
    Oct 27, 2016 at 5:08
  • 3
    The classical way is to boot into single user mode. You have to interrupt the boot loader and add "single" to the line specifying your kernel and boot parameters, as described for example here. Oct 27, 2016 at 15:29

10 Answers 10

38

Use LiveUSB or LiveDVD.

Boot into live session, mount your hard-drive, and copy your original .bashrc file over the modified.

Then you can safely reboot (eject USB or DVD).

General info: In case you wouldn't have backup of .bashrc file, you can use the one from liveUSB/DVD.

3
  • I assume the whole point of making back-ups is when your files are different from the default ones from the installation CD. Oct 28, 2016 at 9:54
  • 2
    Yes they are, but there is also possibility, that you ruined something and don't know what exactly, or you have no backup. That's why I added to response to OPs question also general info for whoever might not have backup ;) Oct 28, 2016 at 10:05
  • Well yeah, if you don't have a backup of something you've ruined, you'll have to start from scratch. Fortunately, the OP has one. Oct 28, 2016 at 11:25
25

You should be able to boot into recovery mode, which will let you drop into a root shell and then correct the problem from there. This avoids the need to download an additional OS.

Instructions taken from Ubuntu wiki:

  1. Switch on your computer.
  2. Wait until the BIOS has finished loading, or has almost finished. (During this time you will probably see a logo of your computer manufacturer.)
  3. Quickly press and hold the Shift key, which will bring up the GNU GRUB menu. (If you see the Ubuntu logo, you've missed the point where you can enter the GRUB menu.)

  4. Select the line which starts with "Advanced options".

  5. Select the line ending with "(recovery mode)", probably the second line, something like:

    Ubuntu GNU/Linux, with Linux 3.8.0-26-generic (recovery mode)

  6. Press Return and your machine will begin the boot process.

  7. After a few moments, your workstation should display a menu with a number of options. One of the options (you may need to scroll down to the bottom of the list) will be "Drop to root shell prompt". Press Return with this option highlighted.

  8. The root partition is mounted read-only. To mount it read/write, enter the command

    mount -o remount,rw /

  9. If you have /home, /boot, /tmp, or any other mount point on a separate partition, you can mount them with the command

    mount --all

    (This must be done following step 8 so that /etc/mtab is writable.)

If you do not have the recovery option, you can press e in GRUB to edit the standard boot option and add recovery at the end of the linux line (second-last line by default, just before initrd). Then press F10 to run it.

9

Use a live CD

  1. Boot using live CD/DVD. In case you do not have the iso and do not want to download the large iso, download Tiny Core - 10MB.
  2. On a command line type sudo mount /dev/sdXn /mnt where sdXn is your Ubuntu partition.
  3. Type cd /mnt/home/<your user name>/Downloads.
  4. Now restore your .bashrc as your new .bashrc using this command mv ../.bashrc ../.bashrc_old && cp ./.bashrc ../.
  5. Now restart into your system and you should be good to go.
8

Insert the dvd or USB you used to install Ubuntu and boot from it. Pick "try Ubuntu" and wait for it to return the desktop.

Check the disks in the lauchers for the one that has your home. Clicking it mounts it into /media/

Open a terminal and cd into it to your Downloads and copy the file into your home. That would be:

cd /media/home/{your_username}/Downloads/
sudo cp .bashrc ../

and reboot without the dvd/USB.

4
  • I get something like cp: cannot create regular file '../bashrc': Permission denied. :(
    – crypto
    Oct 26, 2016 at 17:49
  • ah use sudo! :)
    – Rinzwind
    Oct 26, 2016 at 17:51
  • It was a lame question. Sorry. Lesson learnt! And as you suggested, I'm accepting Michal's answer. Hope thats okay
    – crypto
    Oct 26, 2016 at 17:52
  • not a problem, I am on 200 anyways >:)
    – Rinzwind
    Oct 26, 2016 at 17:55
6

Connect via SFTP

Connect to the system via SFTP, either from another Linux machine's built-in sftp command or a PC with WinSCP or FileZilla, and repair the file. The .bashrc file is not ordinarily involved in SFTP (it's possible to tweak your authorized_keys file to make it involved, but doubtful you've done that).

1
  • 1
    Or, similarly, connect with ssh dash (or any other non-bash shell). Oct 28, 2016 at 18:25
5

I suggest always having TWO sudo accounts. These are accounts that can act as root in ALMOST all cases, if you give the normal password for that account. You temporarily get to act as root.

If you screw one account up, you can fix it with the other one.

But, it MAY be possible to be in a REGULAR account and 'su screwed-up-sudo-account-name' if you know the password. Then you will be acting as the screwed up account and can 'sudo cp backup .bashrc' after 'dc /home/screwed-up-account' I now it's possible without it being screwed up, but not sure in your particular case.

4

As said by a few people, more then one "sudo" account is the best way to go. Secondly, if you do a lot of mods to your .bashrc file, drop a copy on a memory stick before you start tinkering, then you don't have to start over with a blank, you can go back to the last known good .bashrc file without losing all your previous tinkering. The first thing I ever do when tinkering with my .bashrc or .vimrc or . . . any configuration file is make a copy to my handy memory stick before I start. I'm human I have kids I get distracted lol. I have a memory stick full of config files for bash, vim, conky, openbox, tint2 and more.

1
  • Another trick is to use RCS to version history of configuration files (such as bashrc, vimrc, or anything in /etc). Oct 29, 2016 at 0:56
0

Log in as root instead, and use your backup to fix your .bashrc as in the other answers.

For optimum safety, don't log in to the normal X Windows environment. Instead, hit control-alt-F2 to go to a text console and log in. Once you've fixed the problem, run exit to log out from the root shell, then hit control-alt-F7 to go back to X Windows. (Note that X Windows might be on tty1 instead of tty7, so you might need to use control-alt-F1 instead. tty6 or tty7 are historically traditional, but some distros are moving it to tty1 because people rarely use ttys are more. Ubuntu may or may not have done so. Experiment to find out which way your system is configured.)

6
  • The root account is disabled in Ubuntu so unless you enabled it there is no way to login as root.
    – Seth
    Oct 26, 2016 at 23:18
  • 1
    @Seth If you have access to the bootloader, you can boot into recovery mode and get a root shell there.
    – Bob
    Oct 27, 2016 at 2:34
  • @Bob true, but that is not what the answer says. That should work though.
    – Seth
    Oct 27, 2016 at 2:41
  • 1
    @Seth Yea, I've already added my own answer with more detail there. Oh, btw @db48x, normally X11 runs on tty7 (Ctrl+Alt+F7), not tty1.
    – Bob
    Oct 27, 2016 at 4:03
  • Ouch, I didn't know that they disabled the root account. Maybe I won't upgrade my one Ubuntu machine after all...
    – db48x
    Oct 27, 2016 at 16:58
0

Everytime I turned on the PC after this incident, I was greeted with the usual ubuntu login screen except that after entering the password, the screen blinked for a second and the login screen presented itself again.

I don't know if this is what your problem was, but this happened to me once. If any of the files loaded on shell-start (.bashrc, .bash_profile, .profile) outputs text to stdout, you won't be able to log in graphically. You can still go into a tty (ctrl+alt+f1), login from there, and then recover your file.

0

You can use a Live CD, and then navigate to etc/skel on the drive. You can then copy the .bashrc there, and then paste it into your home directory (you may need root to copy the file).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .