在 AWS SageMaker Jupyter 实验室中授予 Python 根权限

分享于2022年07月17日 chown elasticsearch lambda machine-learning python 问答
【问题标题】:在 AWS SageMaker Jupyter 实验室中授予 Python 根权限(Give Python root permissions in AWS SageMaker Jupyter Labs)
【发布时间】:2022-06-17 15:52:38
【问题描述】:

我正在使用这个 Notebook ,其中 Apply DocumentClassifier 部分更改如下。

Jupyter Labs ,内核: conda_mxnet_latest_p37


chown 命令输出很多: Operation not permitted preexec_fn lambda 函数错误。

chown 行上添加 sudo... 不会输出任何 Operation not permitted ,但仍会抛出 SubprocessError

! sudo chown -R daemon:daemon elasticsearch-7.9.2

运行 os.setuid(1) 会抛出 PermissionError

如何在我的 AWS SageMaker Jupyter 实验室中授予 Python root 用户权限?

我怀疑这可能是我的错误的原因。


我的代码:

# In Colab / No Docker environments: Start Elasticsearch from source
! wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar.gz -q
! tar -xzf elasticsearch-7.9.2-linux-x86_64.tar.gz
! chown -R daemon:daemon elasticsearch-7.9.2

import os
from subprocess import Popen, PIPE, STDOUT
es_server = Popen(['elasticsearch-7.9.2/bin/elasticsearch'],
                   stdout=PIPE, stderr=STDOUT,
                   preexec_fn=lambda: os.setuid(1)  # as daemon
                  )
# wait until ES has started
! sleep 30

错误:

...
chown: changing ownership of ‘elasticsearch-7.9.2/modules/x-pack-enrich’: Operation not permitted
chown: changing ownership of ‘elasticsearch-7.9.2/modules’: Operation not permitted
chown: changing ownership of ‘elasticsearch-7.9.2’: Operation not permitted
---------------------------------------------------------------------------
SubprocessError                           Traceback (most recent call last)
 in 
      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)
 in 
      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.

独立运行 os.setuid(1)

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
 in 
----> 1 os.setuid(1)

PermissionError: [Errno 1] Operation not permitted
---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
 in 
----> 1 os.setuid(1)

PermissionError: [Errno 1] Operation not permitted


【解决方案1】:

尝试设置 os.setuid(0)

os.setuid() 方法用于设置当前进程的真实用户id。使用 os.setuid() 方法设置当前进程的真实用户 ID。

# In Colab / No Docker environments: Start Elasticsearch from source
! wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar.gz -q
! tar -xzf elasticsearch-7.9.2-linux-x86_64.tar.gz
! chown -R daemon:daemon elasticsearch-7.9.2

import os
from subprocess import Popen, PIPE, STDOUT

es_server = Popen(
    ["elasticsearch-7.9.2/bin/elasticsearch"], stdout=PIPE, stderr=STDOUT, preexec_fn=lambda: os.setuid(0)  # as daemon
)
# wait until ES has started
! sleep 30