在limits.conf中输入错误,无法ssh托管

问题描述

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

试图在GUI中打开VM,然后进入控制台并尝试了一些选项,但无法进入单用户模式。

解决方法

由于您已经尝试了vbox cli命令并且命令挂起,这意味着virtualbox无法访问系统或无法打开外壳。

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

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

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.