スクリプトの「sudo -u」は、まだユーザーパスワードを呼び出すように求められます。

スクリプトの「sudo -u」は、まだユーザーパスワードを呼び出すように求められます。

私はとしてログインし、user1script1がscript2を呼び出そうとする2つのスクリプトを持っていますsudo -u user2。私の問題は、user1私が指定したオプションにもかかわらず、-uまだパスワードを入力するように求められますsudo。これは私の設定です。

  1. スクリプト1:

    #! /bin/bash
    
    echo "Current user in script1:" $USER
    
    # Call script2
    sudo -u user2 /full/path/to/script2
    
  2. スクリプト2:

    #! /bin/bash
    
    echo "Current user in script2:" $USER
    
    # Execute command as user2
    some-command-that-works
    
  3. 私が持っているものと同じです/etc/sudoers

    # /etc/sudoers
    #
    # This file MUST be edited with the 'visudo' command as root.
    #
    # See the man page for details on how to write a sudoers file.
    #
    
    Defaults        env_reset
    
    # Host alias specification
    
    # User alias specification
    
    # Cmnd alias specification
    
    # User privilege specification
    user1 ALL=(ALL:ALL) ALL
    
    
    # Allow members of group sudo to execute any command
    # (Note that later entries override this, so you might need to move
    # it further down)
    %sudo ALL=(ALL) ALL
    #
    #includedir /etc/sudoers.d
    
    # Don't ask for user2 password for script2
    user2 ALL= NOPASSWD: /full/path/to/script2
    
    # FYI: I experimented with the line below for group1 that user1 is a member of
    %group1 ALL= NOPASSWD: /full/path/to/script2
    
  4. ls -blah出力

    drwxrwsr-x 2 user1 group1 4.0K Oct 19 15:22 .
    drwxrwsr-x 7 user1 group1 4.0K Oct 18 18:48 ..
    -rwxrwxr-x 1 user1 group1  180 Oct 20 17:37 script1
    -rwx------ 1 user2 group1  166 Oct 20 16:29 script2
    

実行したいシェルの例script1

user1@host1 /full/path/to/script1 $ script1
Current user in script1: user1
[sudo] password for user1:

編集する:これは私が使用して接続するサーバーにありますssh。サーバーはDebian 6.0.4を実行しています。Squeeze

ベストアンサー1

sudo -u <user>指定されたユーザーとしてコマンドを実行するためのユーザー権限を付与します。

su - <user>特定のユーザーに切り替えるのとは異なります。su - <user>そのユーザーでセッションを開くには、特定のユーザーのパスワードを入力する必要があります。

sudo -u <user>NOPASSWD:sudoersファイルにこのフラグが指定されていない限り、現在のユーザーパスワードが必要です。

必要なものを取得するには、これをsudoersファイルに追加します。

%group1 ALL=(user2) NOPASSWD: /full/path/to/script2

これにより、group1はパスワードを入力しなくてもuser2でscript2を実行できます。

おすすめ記事