limits.conf 中输入错误,无法通过 ssh 连接到主机

wrong entry in limits.conf , unable to ssh to host

我们有 VirtualBox(使用 vagrant)环境,错误地在 /etc/security/limits.conf 中输入了一个条目 [没有根 shell 打开:( ] 现在我无法 ssh (连接立即断开)。 以前我们有一个这样的场景(由其他人完成的限制),能够使用 vboxmanage guestcontrol copyto CLI 修复并且能够覆盖 limits.conf 然后 ssh 被允许,这次 vboxmanage CLI 也挂起

尝试在 GUI 中打开虚拟机并转到控制台并尝试了几个选项,但无法进入单用户模式。

由于您已经尝试过 vbox cli 命令并且命令挂起,这意味着即使 virtualbox 也无法访问系统或无法打开 shell。

在这种情况下,您将不得不启动一个 ubuntu VM 并使用 qemu-nbd 模块来解决这个问题。步骤如下。

通过执行以下步骤,在同一台主机上使用 hashicorp 的 bionic64 启动一个非常简单的 ubuntu 虚拟机。

mkdir bionic

cd bionic

vagrant box add hashicorp/bionic64

vagrant init

Open the Vagrantfile and change the config.vm.box = "base" to config.vm.box = "hashicorp/bionic64"

Also mount the folder in the host where the .vdi file for the VM is located by adding the following to the Vagrant file by adding the following line(replace the file path with the correct one corresponding to your system. Here /nbd2 will be created on the ubuntu machine and will contain the files including the .vdi file.

config.vm.synced_folder "/home/topcat/VirtualBox\ VMs/your_vm", "/nbd2"

Now do vagrant up

Once the machine boots up

vagrant ssh #to ssh as vagrant

sudo su #to become root

apt-get update #This will refresh the apt cache 

apt-get install qemu

modprobe nbd (to check if the module is loaded successfully. Will exit without any output if it is installed)

qemu-nbd -c /dev/nbd1 "/nbd2/box-disk001.vdi" - (Here change the path to whatever you gave in the config.vm.synced_folder property)

mkdir -p /mnt/vdi-boot

mount /dev/nbd1p1 /mnt/vdi-boot

cd /mnt/vdi-boot/etc/security (This folder will have all the files as it were in your VM)

touch limits.conf (if the file is already there, delete it)

chmod 644 limits.conf

chown root:root limits.conf

open the /mnt/vdi-boot/etc/security/nsswitch.conf file and check if the following three lines are present

passwd:     files
shadow:     files
group:      files
umount /mnt/vdi-boot (unmounts the mounted path)

qemu-nbd -d /dev/nbd1 (disconnects from qemu-nbd)

Exit the VM and start the VM

Open another shell and try to ssh. It should go through fine this time.