python 判断当前执行用户是否是 root 用户

非 root 用户下

os-liming@os-W330-H30:~$ python
Python 3.6.2 (default, Jul 17 2017, 13:39:29) 
[GCC 6.4.0 20170704] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.getuid())
1000
>>> assert os.getuid() == 0, 'must use root'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: must use root

root 下

root@os-W330-H30:/# python
Python 3.6.2 (default, Jul 17 2017, 13:39:29) 
[GCC 6.4.0 20170704] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> assert os.getuid() == 0 , 'must use root'
>>> print(os.getuid())
0

 

posted @ 2021-08-17 11:17  夜晚的潜水艇  阅读(379)  评论(0)    收藏  举报