為 ubuntu 16.04 伺服器調試 preseed/late_command:未找到 tee vs 不存在的目錄

為 ubuntu 16.04 伺服器調試 preseed/late_command:未找到 tee vs 不存在的目錄

我正在嘗試在 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;

在 /var/log/installer/syslog 中我可以看到以下錯誤:

log-output: sh:
log-output: tee: not found

當我將“| 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;

它突然開始抱怨它找不到目錄,沒有說任何有關 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

我一直在研究 github 上的其他腳本。我還在 preseed.cfg 檔案中新增了以下行:

d-i pkgsel/include string openssh-server coreutils wget sudo

我什至嘗試將 coreutils 安裝為目標內命令,以確保 tee 可用。這已經好幾天了,一次又一次地重建 ubuntu,卻在 syslog 中發現了相同的錯誤。如果有人可以闡明這一點 - 這一定是簡單的事情,但我沒有看到它...

答案1

兩件事情:

  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

這將創建一個內容為“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 將尊重它。

答案2

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]

筆記:

  • 不要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';

我花了大約一天的時間對此進行調試,並嘗試了以下方法,得到了不同的結果。如果有更詳細的文件說明 debian 安裝程式中使用哪個 shell 來處理各種非/「目標內」指令,那就太好了,但我可能沒有在正確的位置找到。

  1. 帶有普通的authorized_keys路徑的普通目標內回顯:

    in-target echo "my string" >> /home/username/.ssh/authorized_keys;
    

    給予“目錄未找到”/var/log/installer/syslog

  2. 帶有目標authorized_keys路徑的普通目標內回顯:

    in-target echo "my string" >> /target/home/username/.ssh/authorized_keys;
    

    在目標機器上產生一個空白的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';
    

    產生“目錄未找到”錯誤/var/log/installer/syslog

希望這可以節省一些時間和調試!

答案4

echo 將在目標內指令中工作。您必須將其與反引號 (`) 放在一起。例如,我想從預置檔案更新我的機器的主機名稱:

目標內主機名echo "ubn"$(lshw | grep -m 1 serial | awk '{print tolower ($2)}')

反引號告訴 shell 將其作為一個命令運行。

相關內容