为 ubuntu 16.04 服务器调试 preseed/late_command:找不到 tee 与不存在的目录

Met*_*end 8 command-line preseed tee system-installation 16.04

我正在尝试在 ubuntu 16.04 服务器机器上运行以下预置文件(在打包程序构建期间):

d-i preseed/late_command string \
in-target mkdir -v -p -m 0440 "/etc/sudoers.d"; \
in-target echo "%vagrant ALL=(ALL) NOPASSWD: ALL" | tee -a /etc/sudoers.d/vagrant; \
in-target echo "Defaults:vagrant !requiretty" | tee -a /etc/sudoers.d/vagrant; \
in-target chmod 440 /etc/sudoers.d/vagrant;
Run Code Online (Sandbox Code Playgroud)

在 /var/log/installer/syslog 中,我可以看到以下错误:

log-output: sh:
log-output: tee: not found
Run Code Online (Sandbox Code Playgroud)

当我像这样将“| tee -a”部分更改为“>>”时:

d-i preseed/late_command string \
in-target mkdir -v -p -m 0440 "/etc/sudoers.d"; \
in-target echo "%vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/vagrant; \
in-target echo "Defaults:vagrant !requiretty" >> /etc/sudoers.d/vagrant; \
in-target chmod 440 /etc/sudoers.d/vagrant;
Run Code Online (Sandbox Code Playgroud)

它突然开始抱怨它找不到目录,没有说任何关于 mkdir 行的内容 - 所以它既没有创建目录,也没有找到它:

log-output: sh: can't create /etc/sudoers.d/vagrant: nonexistent directory
log-output: sh: can't create /etc/sudoers.d/vagrant: nonexistent directory
log-output: chmod:
log-output: cannot access '/etc/sudoers.d/vagrant'
log-output: : No such file or directory
Run Code Online (Sandbox Code Playgroud)

我一直在研究 github 上的其他脚本。此外,我将以下行添加到 preseed.cfg 文件中:

d-i pkgsel/include string openssh-server coreutils wget sudo
Run Code Online (Sandbox Code Playgroud)

我什至尝试将 coreutils 安装为目标命令,以确保 tee 应该可用。已经在这个问题上呆了几天,一次又一次地重建 ubuntu,只是在 syslog 中发现了相同的错误。如果有人可以对此有所了解 - 它一定很简单,但我没有看到......

小智 6

两件事情:

  1. 进行in-target some_command > /some/path重定向时不会在目标内部发生。你需要这样做:in-target --pass-stdout some_command > /target/some/path

  2. in-target命令会将命令的输出重定向到日志文件。因此,尝试重定向输出in-target默认情况下不起作用。您需要做的是将--pass-stdout参数用于目标内

in-target --pass-stdout echo "hello" > /target/root/hello.txt
Run Code Online (Sandbox Code Playgroud)

这将创建一个内容为“hello”的文件 /target/root/hello.txt

来自:https: //salsa.debian.org/installer-team/debian-installer-utils/-/blob/3e67bc7987eb4a9cdff5fe8d1084e612fe7bb48f/README

in-target:运行/target中指定的命令并返回其退出状态。debconf 直通前端用于在安装程序中使用 cdebconf 询问 debconf 问题。这对于运行 dpkg-reconfigure、debconf-apt-progress 和taskel 等命令特别有用。日志输出实用程序用于记录任何输出;如果使用选项 --pass-stdout 调用 in-target,则 log-output 将尊重它。


San*_*der 5

echo使用into似乎不起作用in-target。要解决此问题,请尝试以下操作(在 Ubuntu 18.04 中尝试过):

d-i preseed/late_command string \
    echo "some text" >> /target/path/to/file.ext ; \
    in-target [some-other-command-in-target]
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 不要in-target在行前使用
  • /target在真实路径之前使用
  • in-target在其他命令中,您主要可以在该行之前使用


小智 3

如果您更喜欢使用 debian 安装程序,在将 ssh 公钥添加到authorized_keys 时,这对我在 17.10.1 服务器上有效。您必须确保预先创建 .ssh 目录。

d-i preseed/late_command string in-target /bin/sh -c 'echo "my string" >> /home/username/.ssh/authorized_keys';
Run Code Online (Sandbox Code Playgroud)

我花了大约一天的时间对此进行调试,并尝试了以下方法,得到了不同的结果。如果有更详细的文档说明 debian 安装程序中使用哪个 shell 来处理各种非/“目标内”指令,那就太好了,但我可能没有在正确的位置查找。

  1. 带有普通的authorized_keys路径的普通目标内回显:

    in-target echo "my string" >> /home/username/.ssh/authorized_keys;
    
    Run Code Online (Sandbox Code Playgroud)

    给出“目录未找到”/var/log/installer/syslog

  2. 带有目标authorized_keys路径的普通目标内回显:

    in-target echo "my string" >> /target/home/username/.ssh/authorized_keys;
    
    Run Code Online (Sandbox Code Playgroud)

    在目标机器上生成一个空白的authorized_keys文件,查看时/var/log/installer/syslog发现“我的字符串”输出到stdout

  3. sh -c...具有显式和目标authorized_keys路径的目标内:

    in-target /bin/sh -c 'echo "my string" >> /target/home/username/.ssh/authorized_keys';
    
    Run Code Online (Sandbox Code Playgroud)

    产生“目录未找到”错误/var/log/installer/syslog

希望这可以节省一些时间和调试!


RAA*_*AAD 2

您可以使用 Ubiquity 代替 Debian-Installer ( https://wiki.ubuntu.com/Installer/FAQ ),因此您的命令是:

ubiquity  ubiquity/success_command string \
 in-target /bin/mkdir -v -p -m 0440 "/target/etc/sudoers.d"; \
 in-target /bin/echo "%vagrant ALL=(ALL) NOPASSWD: ALL" >> /target/etc/sudoers.d/vagrant; \
 in-target /bin/echo "Defaults:vagrant !requiretty" >> /target/etc/sudoers.d/vagrant; \
 in-target /bin/chmod 440 /target/etc/sudoers.d/vagrant
Run Code Online (Sandbox Code Playgroud)

它通过指向正确的位置解决了报告的“没有这样的文件或目录”:/target/etc/