You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

误改limits.conf致VirtualBox(Vagrant)虚拟机无法SSH连接求助

Fixing SSH Access After Botching /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
    Run vagrant halt in 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) or C:\Users\<your-user>\.vagrant.d\boxes\... (Windows). Look for a file with .vdi or .vmdk extension.

  • Step 3: Mount the disk on your host

    • Linux:
      1. Create a loop device: sudo losetup -f /path/to/your/disk.vdi
      2. List partitions to find the root filesystem: sudo fdisk -l /dev/loop0 (replace loop0 with your loop device)
      3. Mount the root partition: sudo mount /dev/loop0p1 /mnt/vm-root (adjust the partition number as needed)
    • macOS:
      1. Attach the disk: hdiutil attach /path/to/your/disk.vdi
      2. Mount the root partition to a directory (e.g., sudo mount /dev/disk2s1 /mnt/vm-root)
    • Windows:
      1. Open Disk Management, right-click > Attach VHD, and select your disk file.
      2. Assign a drive letter to the root partition via Disk Management.
  • Step 4: Fix the limits.conf file
    Navigate to the mounted directory (e.g., /mnt/vm-root/etc/security/ on Linux/macOS, or your assigned drive letter on Windows) and edit limits.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 up and test SSH access.

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 vms

  • Run a sed command 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.conf
    

    Replace <vm-name> with your VM's name, and your-faulty-line-pattern with 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, press e to edit the boot commands. Find the line starting with linux (or linuxefi for EFI), and add init=/bin/bash to 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.conf with nano or vi:
    nano /etc/security/limits.conf

  • Step 5: Restore normal boot
    Exit the editor, then run exec /sbin/init to start the system normally, or reboot with reboot -f.


内容的提问来源于stack exchange,提问作者user5487193

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:47:29