Use sudo without password INSIDE a script

前端 未结 8 1917
礼貌的吻别
礼貌的吻别 2020-11-29 03:29

For some reason I need, as user, to run without sudo a script script.sh which needs root privileges to work.
I saw as the only solution to put sudo INSIDE script.sh. Let

相关标签:
8条回答
  • 2020-11-29 04:03

    In new /etc/sudoers.d/apt-get file, put single line:

    user ALL=(ALL:ALL) NOPASSWD:/usr/bin/apt-get update
    

    Fully qualified path to executable is required here.

    Then use following in your script:

    sudo apt-get update
    

    Here, fully specified name is not required. Sudo uses PATH environment variable for executable resolution.

    While changing and checking sudoers configuration, be sure to keep another root session open for error recovery.

    0 讨论(0)
  • 2020-11-29 04:07

    As mentioned by Basilevs you need to add your user to the sudoers file in order to avoid that sudo commands in the script get stuck awaiting the password.

    On Ubuntu 16, there is a simpler way: just add the user to the sudo group, like this:

    sudo usermod -aG sudo *username*
    

    From then on, it should work like a charm.

    Note:

    This works only on the condition that the following line is in file /etc/sudoers:

    %sudo ALL=NOPASSWD: ALL

    (such line gives passwordless sudo privileges at group level, in this case to the sudo group)

    (if this line is not present and you want to add it, make sure you use visudo)

    0 讨论(0)
提交回复
热议问题