How to edit .bashrc without logging in

You could boot with a live Linux CD and then mount the CentOS filesystem, and edit the .bashrc file from there.


Init /bin/sh from grub configuration

You can edit your grub configuration to load up a root shell instead of going to the GUI.

  1. Reboot your VM
  2. When the grub menu appears, select the first entry and press e to edit. If it doesn't appear, restart and hold Shift during boot
  3. Find the line beginning with linux16 or linux. Mine looks like this. Yours may differ slightly

    linux16 /vmlinuz-3.10.0-327.18.2.el7.x86_64 root=/dev/mapper/centos-root ro crashkernal=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF8
    
  4. Change the ro to rw (readonly flag to readwrite so you can write changes) and append init=/bin/sh to the line. This tells linux to run /bin/sh instead of init on startup. Example for my entry

    linux16 /vmlinuz-3.10.0-327.18.2.el7.x86_64 root=/dev/mapper/centos-root rw crashkernal=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF8 init=/bin/sh
    
  5. Press Ctrl-X to run the configuration. It won't be saved.

  6. A root shell will appear. Use it to edit your .bashrc and remove the offending line.

    Note that you shouldn't keep using the OS in this state as the shell will be running as PID 1, normally reserved for the init process. You can manually continue init with exec /sbin/init but I'd recommend just rebooting

  7. Reboot as normal. Your previous changes will be forgotten.

We're essentially editing the boot options passed to Linux from GRUB, which tell Linux to mount the root filesystem read-write and start /bin/sh for the init process

This works for me using Centos 7 and VirtualBox 4.3.12


CtrlAltF1 might be getting captured by the host, or VirtualBox might not be passing it on correctly. A couple of quick tests tells me that you can use the Host key defined in VirtualBox instead of CtrlAlt (could be the left Ctrl, or the left on Macs). So, pressing F1 switched to TTY1 in VirtualBox for me (and similarly for F7 back to GUI).