chown 操作は許可されていません

chown 操作は許可されていません

私はオリジナルを作りましたStack Overflow の投稿

このコマンドにより、Jupyter Notebook のさらに下でエラーが発生しています (詳細は SO の投稿を参照)。

! chown -R daemon:daemon elasticsearch-7.9.2

次のような出力が多数あります。

chown: changing ownership of ‘elasticsearch-7.9.2/NOTICE.txt’: Operation not permitted
...
---------------------------------------------------------------------------
SubprocessError                           Traceback (most recent call last)
<ipython-input-25-5f043305a2ca> in <module>
      8 es_server = Popen(['elasticsearch-7.9.2/bin/elasticsearch'],
      9                    stdout=PIPE, stderr=STDOUT,
---> 10                    preexec_fn=lambda: os.setuid(1)  # as daemon
     11                   )
     12 # wait until ES has started

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    798                                 c2pread, c2pwrite,
    799                                 errread, errwrite,
--> 800                                 restore_signals, start_new_session)
    801         except:
    802             # Cleanup if the child failed starting.

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1550                             err_msg += ': ' + repr(err_filename)
   1551                     raise child_exception_type(errno_num, err_msg, err_filename)
-> 1552                 raise child_exception_type(err_msg)
   1553 
   1554 

SubprocessError: Exception occurred in preexec_fn.
---------------------------------------------------------------------------
SubprocessError                           Traceback (most recent call last)
<ipython-input-25-5f043305a2ca> in <module>
      8 es_server = Popen(['elasticsearch-7.9.2/bin/elasticsearch'],
      9                    stdout=PIPE, stderr=STDOUT,
---> 10                    preexec_fn=lambda: os.setuid(1)  # as daemon
     11                   )
     12 # wait until ES has started

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    798                                 c2pread, c2pwrite,
    799                                 errread, errwrite,
--> 800                                 restore_signals, start_new_session)
    801         except:
    802             # Cleanup if the child failed starting.

~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1550                             err_msg += ': ' + repr(err_filename)
   1551                     raise child_exception_type(errno_num, err_msg, err_filename)
-> 1552                 raise child_exception_type(err_msg)
   1553 
   1554 

SubprocessError: Exception occurred in preexec_fn.

追加すると、ステートメントが表示されなくなるsudoため、問題が部分的に解決されるようです。Operation not permitted

! sudo chown -R daemon:daemon elasticsearch-7.9.2

しかし、SubprocessErrorトレースバックは残ります。


Python またはカーネルまたは AWS SageMaker にルート権限を付与するにはどうすればよいですか?

答え1

ルート権限でプログラムまたはスクリプトを実行するには 2 つの方法があります。

  1. sudo: の代わりにを/path/to/your/script.py使って実行しますsudo /path/to/your/script.pysudoこの特定のファイルに対してパスワードを求めないように設定すると役立つ場合があります。これは、次の内容のファイル (任意の名前) をディレクトリに配置することで実行できます/etc/sudoers.d

    ALL ALL=(root) NOPASSWD: /path/to/your/script.py
    
  2. 使うsetuid ビットこの方法は主にバイナリプログラムに使用されます。スクリプト(Pythonスクリプトなど)の場合、Linuxはセキュリティ上の理由からsetuidビットを無視するためです。ただし、バイナリラッパー、つまり、スクリプトを呼び出す以外の何も行わない非常に小さなバイナリ プログラムです。次に、chownバイナリ プログラムをルートに渡し、 を使用して setuid ビットを設定する必要がありますchmod u+s /path/to/your/binary。setuid ビットを持つプログラムは、所有者 (この場合は root) の権限で実行されます。

    ラッパー プログラムは、たとえば次のように C で記述できます。

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main()
    {
      int rc;
      setuid( 0 );
      rc=WEXITSTATUS(system( "/path/to/your/script.py" ));
      exit(rc);
    }
    

    build-essential(C プログラムをコンパイルするには、 C コンパイラが Ubuntu にデフォルトでインストールされていないため、パッケージをインストールする必要があります)。

関連情報