AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1501934
Accepted
Socrates
Socrates
Asked: 2024-01-31 18:42:02 +0800 CST2024-01-31 18:42:02 +0800 CST 2024-01-31 18:42:02 +0800 CST

远程设置 DEBIAN_FRONTEND

  • 772

DEBIAN_FRONTEND=noninteractive通过 SSH 远程执行脚本时有什么方法可以设置吗?

这样做我得到这个错误:

sudo: sorry, you are not allowed to set the following environment variables: DEBIAN_FRONTEND

DEBIAN_FRONTEND=noninteractive我想准确设置在这个位置的原因是因为有时apt-get dist-upgrade会显示一些用户交互的掩码(鞭尾)。由于想要通过远程运行此脚本,crontab -e因此无需人工点击或按任何内容。因此,我需要将其关闭,否则脚本会卡住。

我尝试执行的脚本server_one位于/usr/local/bin/perform-update:

#!/bin/bash

sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
sudo apt-get -y autoremove
sudo snap refresh

server_two我尝试使用以下命令调用它:

SERVER_COMMAND="perform-update"
ssh -i $HOME/.ssh/id_rsa backupuser@server_one $SERVER_COMMAND

我还为backupuseron设置了适当的权限:server_onesudo visudo

backupuser      ALL=(ALL) NOPASSWD: /usr/local/bin/perform-update
backupuser      ALL=(ALL) NOPASSWD: /usr/bin/apt-get
backupuser      ALL=(ALL) NOPASSWD: /usr/bin/snap
command-line
  • 2 2 个回答
  • 56 Views

2 个回答

  • Voted
  1. Best Answer
    muru
    2024-01-31T18:45:52+08:002024-01-31T18:45:52+08:00

    DEBIAN_FRONTEND您可以允许用户在使用 时设置变量sudo,但是...您已授予用户使用 执行脚本的权限sudo,因此没有理由在脚本sudo 内部使用。只需使用 运行脚本本身sudo。

    将您的脚本更改为:

    #! /bin/bash
    apt-get update
    DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
    DEBIAN_FRONTEND=noninteractive apt-get -y autoremove
    snap refresh
    

    然后使用以下命令运行它:

    ssh -i "$HOME/.ssh/id_rsa" backupuser@server_one sudo "$SERVER_COMMAND"
    

    也就是说,您应该使用unattended-upgrades而不是重新发明轮子。


    对于允许用户为特定命令设置环境变量的特殊情况,SETENV除了NOPASSWD标记之外还添加标记。从man sudoers:

     SETENV and NOSETENV
    
       These tags override the value of the setenv flag on a per-command basis.  If SETENV has
       been set for a command, the user may disable the env_reset flag from the command line via
       the -E option.  Additionally, environment variables set on the command line are not
       subject to the restrictions imposed by env_check, env_delete, or env_keep.  As such, only
       trusted users should be allowed to set variables in this manner.  If the command matched
       is ALL, the SETENV tag is implied for that command; this default may be overridden by use
       of the NOSETENV tag.
    

    所以规则应该是这样的:

    backupuser      ALL=(ALL) NOPASSWD:SETENV: /usr/bin/apt-get
    
    • 3
  2. Daniel T
    2024-01-31T19:51:29+08:002024-01-31T19:51:29+08:00

    对于所有实际意图和目的,请参阅@muru 的回答。如果赶时间,只需sudo修改sshserver_two上的线路即可,而/usr/local/bin/perform-update无需编辑server_one上的脚本。但出于安全原因,应该完整遵循它,并在答案末尾加上我的编辑。

    如果我们换个角度思考事情会怎样呢?如果我们无法更改 server_two 上的命令而只能更改 server_one 上的命令怎么办?我一直在寻找 APT 选项来设置环境变量,但我发现的内容超出了我的预期。这是一个令人震惊的开箱即用的答案,只是为了证明您的/etc/sudoers保单需要受到保护。

    #!/bin/bash
    # Put this in /usr/local/bin/perform-update
    # No need to manually change any other file
    
    # Use the -o to inject any arbitrary apt-config option.
    # First write a wrapper file to execute later that
    # sets up DEBIAN_FRONTEND=noninteractive and passes on the arguments.
    sudo apt-get update -o APT::Update::Pre-Invoke::='echo '\'$'#!/bin/sh\nexport DEBIAN_FRONTEND=noninteractive\nexec /usr/bin/dpkg "$@"'\'' > /usr/dpkg; chmod 700 /usr/dpkg'
    # Then use -o again to make dist-upgrade use the wrapper.
    sudo apt-get -y dist-upgrade -o Dir::Bin::dpkg=/usr/dpkg
    # autoremove should use DEBIAN_FRONTEND=noninteractive too because
    # removing some packages like display managers may cause prompting.
    sudo apt-get -y autoremove -o Dir::Bin::dpkg=/usr/dpkg
    sudo snap refresh
    

    我不认为您打算提供backupuser完全的 root 访问权限。为了保护您的系统,您需要删除apt-get和snap中的行visudo,然后按照 @muru 的答案进行操作。

    apt-get编辑:除非您需要删除visudo. 这 3 行应该只替换为第一行:

    backupuser      ALL=(ALL) NOPASSWD: /usr/local/bin/perform-update
    
    • 1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve