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

visudo精確用戶賦權(sudo)

日期:2017/3/2 10:10:11   编辑:關於Linux

sudo授權,後來又博友留言提醒了我,直接編輯sudoers文件對新接觸linux的人來說還是有風險的,比較直接使用vi編輯該文件無法實現語法檢查,而且還需要修改默認文件的權限,非常麻煩,下面再轉載一篇51CTO博友的文章,講述了使用visudo配飾sudo權限,該命令的好處在於可以實現語言的檢查,不過最終的目的還是一樣的,都是實現修改sudoers文件。

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

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

1、一般用戶賦權設置:

[root@localhost ~]# visudo
……前面省略
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
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 - barlow
[redhat@server ~]$ sudo /usr/sbin/useradd test
##命令最好輸入完整的路徑
口令:
##這裡輸入用戶barlow自己的密碼
[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
……前面省略
## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
User_Alias ADMINS = jsmith, mikem ##這個就是一個實例行,按照這個寫自己的
User_Alisa sudouser=user1,user2,user3,user4 ##第一個別名suduouser
……後面省略
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL77 sudouser ALL=(root) /usr/sbin/useradd ##命令行書寫格式,用戶列用別名
……後面省略

對於多個命令的設置:
Cmnd_Alias CCC=command1,command2…… 定義命令別名;

[root@localhost ~]# visudo
……前面省略
## Command Aliases
## These are groups of related commands...
## Networking
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 ##多個命令定義一個命令別名;
……後面省略
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL77 sudouser ALL=(root) NETWORKING ##命令行書寫格式,命令列用別名
……後面省略

Copyright © Linux教程網 All Rights Reserved