歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> visudo精確用戶賦權(sudo)

visudo精確用戶賦權(sudo)

日期:2017/2/28 15:53:45   编辑:Linux教程

sudo” 是Unix/Linux平台上的一個非常有用的工具,允許為非根用戶賦予一些合理的“權利”,讓他們執行一些只有根用戶或特許用戶才能完成的任務,從而減少根用戶的登陸次數和管理時間同時也提高了系統安全性。

  • sudo的目的:為非根用戶授予根用戶的權限;
  • 配置文件:/etc/sudoers
  • visudo命令編輯修改/etc/sudoers配置文件

1、一般用戶賦權設置:

[root@localhost ~]# visudo
……前面省略
69 ## Syntax:
70 ##
71 ## user MACHINE=COMMANDS
72 ##
73 ## The COMMANDS section may have other options added to it.
74 ##
75 ## Allow root to run any commands anywhere
76 root ALL=(ALL) ALL

77 test ALL=(root) /usr/sbin/useradd //新增加用戶行
……後面省略

說明:
第一個字段:root為能使用sudo命令的用戶;
第二個字段:第一個ALL為允許使用sudo的主機,第二個括號裡的ALL為使用sudo後以什麼身份(目的用戶身份)來執行命令;
第三個字:ALL為以sudo命令允許執行的命令;
上列解釋: test ALL=(root) /usr/sbin/useradd
表示允許test用戶從任何主機登錄,以root的身份執行/usr/sbin/useradd命令。

用戶執行命令效果:

[root@server ~]# su - RedHat
[redhat@server ~]$ sudo /usr/sbin/useradd test
//命令需要輸入完整的路徑
口令: //這裡輸入用戶redhat自己的密碼
[redhat@server ~]$ cat /etc/passwd |tail -5
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
redhat:x:500:500::/home/redhat:/bin/bash
test:x:501:501::/home/test:/bin/bash //新增加的用戶
2、sudo配置深入:
1)多個用戶的設置(非同一群組用戶):
對於不同需求的用戶:可以按照上面的方法依次增加多行,每行對應一個用戶。
對於相同需求的多個用戶
User_Alias UUU=user1,user2…… 定義用戶別名;
[root@localhost ~]# visudo
……前面省略
16 ## User Aliases
17 ## These aren't often necessary, as you can use regular groups
18 ## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
19 ## rather than USERALIAS
20 # User_Alias ADMINS = jsmith, mikem //這個就是一個實例行,按照這個寫自己的
21 User_Alisa sudouser=user1,user2,user3,user4 //第一一個別名suduouser
……後面省略
69 ## Syntax:
70 ##
71 ## user MACHINE=COMMANDS
72 ##
73 ## The COMMANDS section may have other options added to it.
74 ##
75 ## Allow root to run any commands anywhere
76 root ALL=(ALL) ALL77 sudouser ALL=(root) /usr/sbin/useradd ////命令行書寫格式,用戶列用別名
……後面省略
對於多個命令的設置:
Cmnd_Alias CCC=command1,command2…… 定義命令別名;
[root@localhost ~]# visudo
……前面省略
23 ## Command Aliases
24 ## These are groups of related commands...
25
26 ## Networking
27 Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dh client, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool //多個命令定義一個命令別名;
……後面省略
69 ## Syntax:
70 ##
71 ## user MACHINE=COMMANDS
72 ##
73 ## The COMMANDS section may have other options added to it.
74 ##
75 ## Allow root to run any commands anywhere
76 root ALL=(ALL) ALL77 sudouser ALL=(root) NETWORKING //命令行書寫格式,命令列用別名
……後面省略

對於多主機的設置和多登陸角色的設置:
•修改 Host_Alias HHH=host1,host2…… 定義主機別名;
修改後對應的命令行主機列位置也是用別名
•Runas_Alias RRR=role1,role2…… 定義runas別名,指定的是“目的用戶”,即sudo 允許轉換至的用戶;
建立相關別明後,相關命令行相應的列也是用我們定義的別名。
2)多個用戶的設置(同一群組用戶):

[root@localhost ~]# visudo
……前面省略
81
82## Allows people in group wheel to run all commands
83# %wheel ALL=(ALL) ALL //用戶列%+群組名
……後面省略

Copyright © Linux教程網 All Rights Reserved