使用python脚本以超级用户身份运行linux系统命令 [英] Run a linux system command as a superuser, using a python script

查看:502
本文介绍了使用python脚本以超级用户身份运行linux系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在计算机上安装了postfix,并且正在以编程方式(使用python)动态地更新virtual_alias(在某些操作上).更新/etc/postfix/virtual_alias中的条目后,我正在运行命令:

I have got postfix installed on my machine and I am updating virtual_alias on the fly programmatically(using python)(on some action). Once I update the entry in the /etc/postfix/virtual_alias, I am running the command:

sudo /usr/sbin/postmap /etc/postfix/virtual_alias 2>>/work/postfix_valias_errorfile

,但是却收到错误消息:

But I am getting the error:

sudo: sorry, you must have a tty to run sudo

我想以非人工方式运行上述sudo命令(这意味着我正在从python脚本运行此系统命令.).那么如何使该命令以编程方式运行?

I want to run the mentioned sudo command in a non-human way(meaning, I am running this system command from a python script.). So how do I get this command run programmatically?

推荐答案

您可以以root用户身份运行python脚本-则无需添加特权即可重新加载后缀.

You can either run your python script as root itself - then you won't need to add privilege to reload postfix.

或者您可以将sudo配置为不需要/etc/init.d/postfix的密码.

Or you can configure sudo to not need a password for /etc/init.d/postfix.

sudo配置(通过visudo)允许NOPASSWD:允许不带密码的命令.参见 http://www.sudo.ws/sudo/man/sudoers.html# nopasswd_and_passwd

sudo configuration (via visudo) allows NOPASSWD: to allow the command without a password. See http://www.sudo.ws/sudo/man/sudoers.html#nopasswd_and_passwd

<username>  ALL = NOPASSWD: /etc/init.d/postfix

或类似的东西.

这篇关于使用python脚本以超级用户身份运行linux系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆