如何自动化设置"keyboard-configuration"软件包的安装?

我正在编写一个脚本,用于在Ubuntu 16.04服务器上使用debootstrap将其安装到chroot监狱中(在一台Ubuntu 16.04服务器机器上)。
在设置keyboard-configuration软件包时,它会要求选择键盘类型:
Setting up keyboard-configuration (1.108ubuntu15) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
Configuring keyboard-configuration
----------------------------------

The layout of keyboards varies per country, with some countries having multiple
common layouts. Please select the country of origin for the keyboard of this
computer.

  1. Afghani                                     48. Irish
  2. Albanian                                    49. Italian
...    
  28. English (UK)                               75. Slovak
  29. English (US)                               76. Slovenian
...
  45. Icelandic                                  92. Vietnamese
  46. Indian                                     93. Wolof
  47. Iraqi
Country of origin for the keyboard: 

我想自动化这个过程,这样它就不会询问,而是继续安装。
我该如何做到这一点?

相关链接:https://stackoverflow.com/questions/38165407/installing-lightdm-in-dockerfile-raises-interactive-keyboard-layout-menu - Ciro Santilli OurBigBook.com
5个回答

从一个类似的StackOverflow问题中得到的信息:
如果在运行apt-get install keyboard-configuration时设置了环境变量DEBIAN_FRONTEND=noninteractive,它将不会提示任何交互。因此,你可以简单地运行:
DEBIAN_FRONTEND=noninteractive apt-get install keyboard-configuration

2如果你正在使用sudo,请确保将其放在sudo之后,例如sudo DEBIAN_FRONTEND=noninteractive apt-get install ... - altschuler

这很容易自动化,你只需要为这个软件包设置正确的debconf配置。
首先安装debconf-utils:
sudo apt install debconf-utils

如果您已经配置了软件包,您可以使用以下命令读取debconf配置:
debconf-get-selections | grep keyboard-configuration

如果您尚未配置软件包或想要更改您的选择,您可以使用以下方式进行操作:

dpkg-reconfigure keyboard-configuration

将您的选择导出到文件中
debconf-get-selections | grep keyboard-configuration > selections.conf

selections.conf 复制到目标机器并设置选择项。
debconf-set-selections < selections.conf

当您安装或重新配置软件包时,您的选择将会自动选定。
dpkg-reconfigure keyboard-configuration -f noninteractive

这个方法会非常有效,只需在你的命令之前添加这几行代码即可:
  echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections
  sudo apt-get install -y -q

你真的建议将文件权限修改为777吗?这样所有用户都可以读取、写入和执行该文件,可能会带来巨大的安全风险! - MatsK
没错,我进行了更新。谢谢你的有益见解。 - Zekarias Taye Hirpo

你可以使用xdotool。在启动脚本时,输入& sleep <到达该点所需的时间> && xdotool type <要输入的数字> && xdotool key Return
我没有测试过,但应该可以工作。
答案2:
运行命令,但将输出重定向到文件(> testfile)。
打开另一个终端并运行
while true
do 
    if [ "$(tac testfile | grep -m 1 .)" = "Country of origin for the keyboard" ]
    then 
    xdotool type <number you want to put> && xdotool key Return && break
    fi
done  

然后,点击回到第一个终端。
答案3:
我认为你只需要将你想要的数字放入一个名为“testfile”的文件中,并在命令后面添加“< testfile”。

谢谢,这个想法很有趣,但是要么会浪费很多时间,要么在安装过程中偶尔会因为安装时间超出预期而卡住,这取决于休眠的时长。 - fadedbee
阅读xdotool的man页面,它似乎是一个X窗口的东西。我正在尝试在Ubuntu服务器环境中使用它。我会更新我的问题。 - fadedbee

"debootstrap其实只是一个shell脚本" --来自https://wiki.debian.org/Debootstrap 这意味着你可以阅读该脚本,看看是否有通过环境变量传递信息的方法,当调用debootstrap时提供参数,或者为你特定的应用程序创建自己修改过的版本。

此软件包正在作为初始debootstrap之后的apt-get install ....运行的一部分进行安装。 - fadedbee
所以,你的意思是在执行apt-get install debootstrap之后,你的安装范式不允许更改环境变量或运行自定义脚本?好的。 - BenjaminBrink
也许这可以帮到你。这个链接展示了如何设置一个“preseed”文件来预先回答Ubuntu安装中的提示问题的示例:http://askubuntu.com/questions/122505/how-do-i-create-a-completely-unattended-install-of-ubuntu - BenjaminBrink