面料差异sudo()run('sudo cmd') [英] fabric difference sudo() run('sudo cmd')

查看:139
本文介绍了面料差异sudo()run('sudo cmd')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道函数sudo()和函数run('sudo -u user smth')

在文档上有:

sudo在所有运行方式上都是相同的,除了它总是包裹 调用sudo程序以提供超级用户时的给定命令 特权.

sudo is identical in every way to run, except that it will always wrap the given command in a call to the sudo program to provide superuser privileges.

但是有一段时间,sudo('cmd')提示我输入密码,但是如果我使用run('sudo cmd')进行切换,它可以在不提示我任何内容的情况下工作.两者之间有什么变化吗? (我记得有人在说sudorun(sudo cmd)不能用于同一用途,但我找不到它)

But a few time, sudo('cmd') prompt me a password, but if I switch with run('sudo cmd') it works without prompting me anything. Is there anything that change between the two ? (I remember someone on SO saying that sudo and run(sudo cmd) are not for the same use, but I can't find it back)

推荐答案

我发现了这两个区别.

1: Fabric维护内存中的密码

2: sudo接受其他用户,并且组参数

首先,使用sudo()时,fabric将从缓存中获取密码,然后您无需输入密码.但是,如果您使用run('sudo cmd'),则需要为每个'sudo cmd'输入密码.

First, fabric would get password from cache when using sudo(), then you do not need to enter password. But if you use run('sudo cmd'), you need to enter password for each 'sudo cmd'.

第二,如果要执行的命令不是root用户,而是其他用户组(如www),则只需设置env.sudo_user ='www'或sudo('cmd',user ='www').第一个将在www下执行每个sudo(),第二个将在www下执行此单个cmd.但是使用run()命令时,您需要编辑运行("sudo -u'www'cmd").

Second, if you want to execute a command not under root but other user group like www, you just need to set env.sudo_user = 'www' or sudo('cmd', user='www'). The first would execute each sudo() under www, the second would execute this single cmd under www. But you need to edit to run("sudo -u 'www' cmd") when use run() command.

from fabric.api import sudo, run, env

env.hosts = ['host_ip',]
env.user = 'user_name'
env.sudo_user = 'sudo_user'


def test_1():
    run('sudo pwd')

def test_2():
    sudo('pwd')



$ fab -I --show=debug test_1 test_2
Initial value for env.password:   # enter password
Commands to run: test_1, test_2
Parallel tasks now using pool size of 1
[ip_address] Executing task 'test_1'
[ip_address] run: /bin/bash -l -c "sudo pwd"
[ip_address] out: [sudo] password for billy:   # needs to enter password here
[ip_address] out: /home/billy
[ip_address] out: 

Parallel tasks now using pool size of 1
[ip_address] Executing task 'test_2'
[ip_address] sudo: sudo -S -p 'sudo password:'  -u "root"  /bin/bash -l -c  "pwd"
[ip_address] out: sudo password:  # only prompt, do not need enter password
[ip_address] out: /home/billy
[ip_address] out: 


Done.
Disconnecting from ip_address... done.

这篇关于面料差异sudo()run('sudo cmd')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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