This is probably a duplicate question, but I'm not familiar enough with the login/boot process of CentOS (especially on a VM) to know what to search for.
I'm running CentOS 7 in VirtualBox. I accidentally appended gnome-terminal to the end of my ~/.bashrc file. So now a terminal window pops up when I log in. That terminal session executes .bashrc, which opens another window, etc. etc., and I'm flooded with a ton of gnome-terminal terminal windows.
I'm trying to log into a terminal session without the GUI login (so gnome-terminal fails), but I can't get it to boot without the GUI. I've tried using the answers to this question, with no success. I get a GUI login every time: How to Boot CentOS in CLI?. Is there another way to edit the grub configuration at boot, or a way to get to a new terminal instance from the GUI login, like Ctrl+Alt+F1 in Ubuntu? (Yes, I tried it. It either doesn't work in CentOS 7, or it doesn't work in a VirtualBox VM.) Or is there some other way I can get a terminal session without a GUI, so I can edit my .bashrc and fix this mess?
4 Answers
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).
3You could boot with a live Linux CD and then mount the CentOS filesystem, and edit the .bashrc file from there.
2Init /bin/sh from grub configuration
You can edit your grub configuration to load up a root shell instead of going to the GUI.
- Reboot your VM
- When the grub menu appears, select the first entry and press
eto edit. If it doesn't appear, restart and hold Shift during boot Find the line beginning with
linux16orlinux. Mine looks like this. Yours may differ slightlylinux16 /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.UTF8Change the
rotorw(readonly flag to readwrite so you can write changes) and appendinit=/bin/shto the line. This tells linux to run/bin/shinstead ofiniton startup. Example for my entrylinux16 /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/shPress Ctrl-X to run the configuration. It won't be saved.
A root shell will appear. Use it to edit your
.bashrcand 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
initprocess. You can manually continue init withexec /sbin/initbut I'd recommend just rebootingReboot 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
3scp a repaired copy of .bashrc into place
If you have another machine to work from, grab a copy the broken file and fix the problem, then put it back where it came from. This avoids logging in and invoking any of the login commands.
user@backup ~ $ scp user@homehost:~/.bashrc busted.bashrc
user@backup ~ $ vim busted.bashrc # fix, fix
user@backup ~ $ scp busted.bashrc user@homehost:~/.bashrc
user@backup ~ $ rm busted.bashrcAddendum: As OP points out below, this requires SSH access to homehost. If that is not an option but FTP/SFTP is available instead, any file-transfer method will do what you need, which is simply to install the corrected login file.