最近CI有两个job中编译的时候需要不同的环境,分别依赖同一个包的不同版本,而且运行加载库包的时候需要用到sudo的权限。由于Jenkins在运行的时候使用的是非root账户,所以就必须输入密码。但是jenkins servr现在不支持tty的插件,而且jenkins是由几个Unit公用的,要安装新的插件走的流程比较复杂,短期内不可能。为了达到目标,只能考虑到其他的方法了。
Google一下知道了一个叫做sudoers的文件可以达到目的。官方解释是,sudoers可以决定用户的sudo权限,这个文件在/etc/sudoers。管理员可以通过添加用户到sudoers文件来控制运行sudo命令时候的行为,比如是否需要密码(英文没有选好,翻译成中文也说不好,悲剧啊)。所以,正好满足我的需求,也不需要去走那个复杂的流程。
这里记录一下我是怎么做的:
>sudo visudo #不能使用vi或者vim去打开,应该使用visudo命令来打开
文件如下:
# sudoers file. # # This file MUST be edited with the 'visudo' command as root. # Failure to use 'visudo' may result in syntax or file permission errors # that prevent sudo from running. # # See the sudoers man page for the details on how to write a sudoers file. # # Host alias specification # User alias specification # Cmnd alias specification # Defaults specification Defaults env_reset Defaults env_keep += "BLOCKSIZE" Defaults env_keep += "COLORFGBG COLORTERM" Defaults env_keep += "__CF_USER_TEXT_ENCODING" Defaults env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE" Defaults env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME" Defaults env_keep += "LINES COLUMNS" Defaults env_keep += "LSCOLORS" Defaults env_keep += "SSH_AUTH_SOCK" Defaults env_keep += "TZ" Defaults env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY" Defaults env_keep += "EDITOR VISUAL" Defaults env_keep += "HOME MAIL" # Runas alias specification # User privilege specification root ALL=(ALL) ALL %admin ALL=(ALL) ALL # Uncomment to allow people in group wheel to run all commands # %wheel ALL=(ALL) ALL # Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL # Samples # %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom # %users localhost=/sbin/shutdown -h now
所以,从上面的解释来说,在#User privilege Speciation后面加上目标username就可以了,比如:
username ALL=(ALL) ALL
我这里使用的是用户名是MSTV,看好了,是大写的,添加如下行:
MSTV ALL = (ALL) ALL
保存,结果报错,如下:
visudo: Warning: User_Alias `MSTV' referenced but not defined
看来这个文件不认识'MSTV',因为没有设置User_Alias, 查看cat /etc/passwd|grep MSTV, 没有搜到,查看sudoers果然有个设置User_Alias的地方。那就设置吧。查看设置的语法:
User_List ::= User | User ',' User_List User ::= '!'* user name | '!'* #uid | '!'* %group | '!'* %#gid | '!'* +netgroup | '!'* %:nonunix_group | '!'* %:#nonunix_gid | '!'* User_Alias
User_list可以有一个或者多个用户名,user ID(前面必须加#),系统的group(前面必须加%),Group ID(前面要加%#), netgroups(前面必须加+), Non-Unix group(前面加上%:), Non-Unix gid(前面必须加上%:)等等。
所以,我改成了:
User_Alias MSTV_ = MSTV
结果,还是报错, 看来不能使用MSTV这个用户名,那就使用uid吧,运行ls -ln,查看uid是504,所以:
User_Alias MSTV = #504
:wq, 这下终于不报错了。
由于,我只想在使用特定命令的时候带sudo,不需要输入密码,所以,可以指定哪些特定的命令。在这里使用的是Cmnd_Alias来指定:
Cmnd_Alias FINALIZER5 = /usr/local/jenkins/Finalizer/Finalizer-5.0.0.70600-macosx-x86/Xcode/install-xcode-finalizer-wrapper.sh Cmnd_Alias FINALIZER4 = /usr/local/jenkins/Finalizer/Finalizer-4.1.1.1-macosx-x86/Xcode/install-xcode-finalizer-wrapper.sh
组成新的rule,就是:
MSTV ALL=(ALL) NOPASSWD: FINALIZER4, FINALIZER5
:wq 保存,没有错误输出,sudo -l,查看一下,
Matching Defaults entries for MSTV on this host: env_reset, env_keep+=BLOCKSIZE, env_keep+="COLORFGBG COLORTERM", env_keep+=__CF_USER_TEXT_ENCODING, env_keep+="CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE", env_keep+="LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME", env_keep+="LINES COLUMNS", env_keep+=LSCOLORS, env_keep+=SSH_AUTH_SOCK, env_keep+=TZ, env_keep+="DISPLAY XAUTHORIZATION XAUTHORITY", env_keep+="EDITOR VISUAL", env_keep+="HOME MAIL" User MSTV may run the following commands on this host: (ALL) ALL (ALL) NOPASSWD: /usr/local/jenkins/Finalizer/Finalizer-4.1.1.1-macosx-x86/Xcode/install-xcode-finalizer-wrapper.sh, (ALL) /usr/local/jenkins/Finalizer/Finalizer-5.0.0.70600-macosx-x86/Xcode/install-xcode-finalizer-wrapper.sh
可以看到这条规则生效了,测试一下,运行这两个命令前面带上sudo,很顺利,不需要输入密码,就执行成功了。反过来执行一个其他的命令,带上sudo,就需要密码。可见sudoers已经生效了。
这里,主要参考的文档是:http://www.sudo.ws/sudo/sudoers.man.html,另外sudo也有很多命令含有意思,输入sudo -h,可以得到:
sudo usage: sudo -h | -K | -k | -L | -V usage: sudo -v [-AknS] [-g groupname|#gid] [-p prompt] [-u user name|#uid] usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U user name] [-u user name|#uid] [-g groupname|#gid] [command] usage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>] usage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user name|#uid] file ... Options: -A use helper program for password prompting -b run command in the background -C fd close all file descriptors >= fd -E preserve user environment when executing command -e edit files instead of running a command -g group execute command as the specified group -H set HOME variable to target user's home dir. -h display help message and exit -i [command] run a login shell as target user -K remove timestamp file completely -k invalidate timestamp file -L list supported sudoers Defaults values -l[l] command list user's available commands -n non-interactive mode, will not prompt user -P preserve group vector instead of setting to target's -p prompt use specified password prompt -S read password from standard input -s [command] run a shell as target user -U user when listing, list specified user's privileges -u user run command (or edit file) as specified user -V display version information and exit -v update user's timestamp without running a command -- stop processing command line arguments