Linux 服务器中的自动 root 登录

Linux 服务器中的自动 root 登录

我正在尝试在不同的 Linux 服务器发行版(Fedora、Ubuntu、Centos 等)中执行测试。

我知道有一个 init 系统可以允许程序在启动后执行(无需登录)。问题是我不想在不同的发行版之间为每个不同的 init 系统设置一个特殊情况。有没有办法允许 root 在启动后自动登录?

通过这样做,我可以为任何发行版使用相同的 shell 脚本。或者有办法解决这个问题吗?

答案1

我知道有一个 init 系统可以允许程序在启动后执行(无需登录)。

不幸的是,现在没有init 系统有六个流行的 init 系统。例如 sysv init、systemd、upstart 等。

无论如何,如果您真的希望系统为您提供具有 root 访问权限的控制台,您可能需要更新您的 init 系统。

我在我的几个系统上对串行端口执行了此操作。我有两个非常不同的配置,并且我只使用两个不同版本的 Debian。 我无法想象你能想出一种适用于所有发行版的方法。事情的开始方式没有一致性。 Systemd 在各个发行版中应该非常相似,但它尚未被广泛接受。

Debian 使用 sysv init (wheezy) 在串行端口上降级为 root

# /etc/inittab
...
# serial port getty spawns sulogin, which drops to a root shell
# on debian if root has a disabled password
T0:23:respawn:/sbin/getty -n -l /sbin/sulogin -L ttyS0 57600 vt102
...

带有 systemd 的 Debian(jessie)在串行端口上降级为 root

#/etc/systemd/system/getty.target.wants/[email protected]
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Serial Getty on %I
Documentation=man:agetty(8) man:systemd-getty-generator(8)
Documentation=http://0pointer.de/blog/projects/serial-console.html
BindsTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

[Service]
ExecStart=-/sbin/agetty -n -l /sbin/sulogin --keep-baud 115200,38400,9600 %I $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes

[Install]
WantedBy=getty.target

最好的办法可能是放弃以 root 身份登录的想法,而是使用配置管理工具,例如 puppet 或可以为您抽象出各种发行版差异的工具。让该工具触发您的测试运行。

答案2

你添加了一条评论:“我有一个程序需要在不同的 Linux 发行版上进行物理测试以报告性能”

在这种情况下,您可能需要考虑使用码头工人这样,您实际上就不需要完整的操作系统或任何东西了...但如果您的程序由于某种原因需要在物理实例上运行,您可能需要检查一些作业执行环境,例如詹金斯

答案3

我不清楚你想要什么,但是如果你想在 Linux 启动后以 root 身份运行某些东西,请在大多数发行版中将其粘贴到这里:

/etc/rc.local

或者有时

/etc/rc.d/rc.local

答案4

如果是命令行编程,更好的方法是通过 SSH 执行。此外,您可以使用 SSH 密钥无需密码即可登录。

相关内容