是否可以通过编程方式让特定用户登录特定的虚拟控制台?

是否可以通过编程方式让特定用户登录特定的虚拟控制台?

我想实现其中一个答案如何确保某些功能(如 Alt+Ctrl+F1 切换到 tty)在切换系统中可用?,为此我需要一种方法来让用户在虚拟控制台上登录。是否可以采用非交互方式?

答案1

这是一个不完全有效的解决方案。如果您有时间,请更正此问题。

步骤 1. 创建文件 /etc/init/autologtty.conf

# Get the user to guess the number. If they get it right, let them
# login.

start on runlevel [23]
stop on runlevel [!23]

env VLOCK_PRESENT=1
env VLOCK_CURRENT_MESSAGE=
env TTY=tty9

respawn
respawn limit 10 5


# XXX: Ensure job is connected to the terminal device
console output

script
    [ -f /etc/default/autologtty ] && . /etc/default/autologtty

    # XXX: Ensure all standard streams are connected to the console
    exec 0</dev/$TTY >/dev/$TTY 2>&1
    clear
    if [ "$DEBUG" -eq 1 ]; then
        set -x 
    fi
    dpkg -s vlock>/dev/null
    if [ $? -ne 0 ]; then
        VLOCK_PRESENT=0
    fi  

    trap '' INT TERM HUP #Prevent Ctrl+C

    rcfile=`mktemp`

    if [ "$VLOCK_PRESENT" -eq "1" ]; then
        echo "[ -f ~/.bashrc ] && . ~/.bashrc">>$rcfile
        if [ -n "$VLOCK_CURRENT_MESSAGE" ]; then
            echo "echo \"$VLOCK_CURRENT_MESSAGE\"" >>$rcfile
            vlock=/usr/sbin/vlock-main
        else
            vlock=vlock
        fi
        echo $vlock >>$rcfile
        if [ -n "$USER" ]; then
            chown $USER $rcfile
            sudo -u $USER /bin/bash --rcfile $rcfile && stop
        else
            bash -l --rcfile $rcfile && stop
        fi
    else
        if [ -n "$USER" ]; then
            su -l $USER -c "bash -l" && stop
        else
            "bash -l" && stop
        fi
    fi
#   exec /sbin/getty -8 38400 $TTY
end script

步骤 2. 创建文件 /etc/default/autologtty

VLOCK_PRESENT=1
USER=adam
VLOCK_CURRENT_MESSAGE="Welcome user $USER! Please hit <ENTER> and enter your password to unlock the tty"
TTY=tty9
DEBUG=0

确保该文件只有 root 可写入,因为所有命令都将由 root 执行。

  1. 在指定的 tty(默认为 9)下会有一个可用的 shell。如果你已经vlock安装,首先会询问你密码。

您可以通过以下方式测试sudo service autologtty restart

嗯,这个会工作,如果我能够运行完全正常工作的 bash。如果我只得到问题的答案https://unix.stackexchange.com/questions/166326/why-does-sudo-su-l-user-c-bash-fails-to-load-users-environment剧本就准备好了。


编辑:

upstart 脚本现在运行正常。

相关内容