如何在 Google Cloud 中的 Linux VM 中进入单用户模式

如何在 Google Cloud 中的 Linux VM 中进入单用户模式

谁能帮助我进入 Linux VM、Google Cloud 中的单用户模式。

我尝试过改变 grub 设置"/etc/default/grub" as " GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 ro single"但是没有成功。

[root@test-linux admin]# cat /etc/default/grub
GRUB_TIMEOUT=100
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL="serial console"
GRUB_SERIAL_COMMAND="serial --speed=38400"
GRUB_CMDLINE_LINUX="crashkernel=auto console=ttyS0,38400n8 elevator=noop"
GRUB_DISABLE_RECOVERY="true"
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 ro single"
[root@test-linux admin]#

答案1

看看我的以下步骤:

  1. 创建虚拟机实例(可选):
$ gcloud compute instances create instance-1 --zone us-central1-a  --machine-type=e2-medium --network=default  --tags=ubuntu  --image-family=ubuntu-1804-lts --image-project=ubuntu-os-cloud
  1. 使能够串行控制台:
$ gcloud compute instances add-metadata instance-1 --metadata serial-port-enable=TRUE --zone us-central1-a
  1. 通过 SSH 进入虚拟机实例:
$ gcloud compute ssh instance-1 --zone us-central1-a
  1. GRUB_CMDLINE_LINUX_DEFAULT修改中的行/etc/default/grub.d/50-cloudimg-settings.cfg并将其设置为GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 ro single",然后运行update-grub2:
instance-1:~$ cat /etc/default/grub.d/50-cloudimg-settings.cfg
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0.1
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=0.1
# old
#GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"
# new
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 ro single"
GRUB_RECORDFAIL_TIMEOUT=0
GRUB_TERMINAL=console

instance-1:~$ sudo update-grub2 
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/50-cloudimg-settings.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.0-1021-gcp
Found initrd image: /boot/initrd.img-5.4.0-1021-gcp
Adding boot menu entry for EFI firmware configuration
done
  1. 重启虚拟机实例:
instance-1:~$ sudo reboot
  1. 连接通过串行控制台进入虚拟机实例:
...
[  OK  ] Started Wait for Network to be Configured.
[  OK  ] Reached target Network is Online.
[  OK  ] Reached target Remote File Systems (Pre).
         Starting Availability of block devices...
[  OK  ] Started Availability of block devices.
You are in rescue mode. After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" or "exit"
to boot into default mode.
Press Enter for maintenance
(or press Control-D to continue): 
root@instance-1:~# runlevel
N 1
root@instance-1:~#
  1. 当您从 grub 中删除单用户标志后,运行该update grub2命令并重新启动:
root@instance-1:~# cat /etc/default/grub.d/50-cloudimg-settings.cfg
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0.1
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=0.1
# old
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"
# new
#GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 ro single"
GRUB_RECORDFAIL_TIMEOUT=0
GRUB_TERMINAL=console

root@instance-1:~# update-grub2
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/50-cloudimg-settings.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.0-1021-gcp
Found initrd image: /boot/initrd.img-5.4.0-1021-gcp
Adding boot menu entry for EFI firmware configuration
done

root@instance-1:~# reboot
  1. 禁用串行控制台:
$ gcloud compute instances add-metadata instance-1 --metadata=serial-port-enable=FALSE --zone us-central1-a

完成后不要忘记禁用串行控制台:

警告:交互式串行控制台不支持基于 IP 的访问限制,例如 IP 白名单。如果您在实例上启用交互式串行控制台,客户端可以尝试从任何 IP 地址连接到该实例。如果知道正确的 SSH 密钥、用户名、项目 ID、区域和实例名称,任何人都可以连接到该实例。使用防火墙规则来控制对您的网络和特定端口的访问。**

相关内容