误改limits.conf致VirtualBox(Vagrant)虚拟机无法SSH连接求助
/etc/security/limits.conf in VirtualBox/Vagrant VM Alright, let's work through this frustrating scenario step by step. You messed up the /etc/security/limits.conf file on your VirtualBox/Vagrant VM, broke SSH access, the usual vboxmanage guestcontrol copyto trick isn't responding, and you can't get into single-user mode via the GUI. Here are three actionable approaches to get your VM back online:
1. Mount the Virtual Disk Directly on Your Host Machine
This is the most reliable fallback when guest-side commands fail—you'll edit the problematic file directly from your host by mounting the VM's disk image.
Step 1: Shut down the VM completely
Runvagrant haltin your project directory, or use the VirtualBox GUI to power off the VM (make sure it's not paused).Step 2: Locate the virtual disk file
Vagrant stores VM disks by default in~/.vagrant.d/boxes/<your-box-name>/<box-version>/virtualbox/(Linux/macOS) orC:\Users\<your-user>\.vagrant.d\boxes\...(Windows). Look for a file with.vdior.vmdkextension.Step 3: Mount the disk on your host
- Linux:
- Create a loop device:
sudo losetup -f /path/to/your/disk.vdi - List partitions to find the root filesystem:
sudo fdisk -l /dev/loop0(replace loop0 with your loop device) - Mount the root partition:
sudo mount /dev/loop0p1 /mnt/vm-root(adjust the partition number as needed)
- Create a loop device:
- macOS:
- Attach the disk:
hdiutil attach /path/to/your/disk.vdi - Mount the root partition to a directory (e.g.,
sudo mount /dev/disk2s1 /mnt/vm-root)
- Attach the disk:
- Windows:
- Open Disk Management, right-click > Attach VHD, and select your disk file.
- Assign a drive letter to the root partition via Disk Management.
- Linux:
Step 4: Fix the
limits.conffile
Navigate to the mounted directory (e.g.,/mnt/vm-root/etc/security/on Linux/macOS, or your assigned drive letter on Windows) and editlimits.conf—remove the faulty lines or restore it to a working state (you can copy a default version from another VM if needed).Step 5: Unmount and restart the VM
- Linux/macOS:
sudo umount /mnt/vm-root && sudo losetup -d /dev/loop0 - Windows: Detach the VHD from Disk Management.
- Start the VM with
vagrant upand test SSH access.
- Linux/macOS:
2. Force a File Edit via vboxmanage guestcontrol run
If the VM is still running (even though SSH fails), the guestcontrol run command might bypass the limits issue by executing a direct edit command as a valid user.
First, get your VM's name with:
vboxmanage list vmsRun a
sedcommand to delete the problematic lines (replace placeholders with your VM details):vboxmanage guestcontrol <vm-name> run --username vagrant --password vagrant -- /bin/sed -i '/your-faulty-line-pattern/d' /etc/security/limits.confReplace
<vm-name>with your VM's name, andyour-faulty-line-patternwith a snippet of the bad config you added (e.g.,* soft nofile 999999).If you need to reset the file entirely, use:
vboxmanage guestcontrol <vm-name> run --username vagrant --password vagrant -- /bin/echo "" > /etc/security/limits.conf(Note: This clears the file—you can add back default limits after restoring SSH.)
3. Bypass Boot Restrictions with GRUB Init Override
If the GUI won't let you enter single-user mode, editing the GRUB boot parameters to boot directly into a root shell often works:
Step 1: Access the GRUB menu
Start the VM in VirtualBox GUI. When the VM starts booting, hold the Shift key (for BIOS-based VMs) or press Esc (for EFI-based VMs) to bring up the GRUB menu.Step 2: Edit the boot line
Select your default kernel, presseto edit the boot commands. Find the line starting withlinux(orlinuxefifor EFI), and addinit=/bin/bashto the end of the line.Step 3: Boot into a root shell
Press Ctrl+X or F10 to boot with the modified parameters. You'll land directly in a root shell—no password required.Step 4: Fix the file
First, remount the root filesystem as read-write:mount -o remount,rw /Edit
limits.confwithnanoorvi:nano /etc/security/limits.confStep 5: Restore normal boot
Exit the editor, then runexec /sbin/initto start the system normally, or reboot withreboot -f.
内容的提问来源于stack exchange,提问作者user5487193

