新聞中心
sudo是linux下常用的允許普通用戶使用超級(jí)用戶權(quán)限的工具,允許系統(tǒng)管理員讓普通用戶執(zhí)行一些或者全部的root命令,如halt,reboot,su等等。這樣不僅減少了root用戶的登陸和管理時(shí)間,同樣也提高了安全性。

為密山等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及密山網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、密山網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
一、直接修改/etc/sudoers文件的注意事項(xiàng)
1、操作時(shí)最好用echo >> 追加,不過cat sed同樣也可以實(shí)現(xiàn)(不常用)
2、修改完成后一定記得檢查語法visudo -c
3、確保/etc/sudoers默認(rèn)的權(quán)限是440(防止權(quán)限誤用)
4、及時(shí)驗(yàn)證修改的配置是否正確
5、確保知道root密碼,以便普通用戶可以通過sudo su -命令切換
二、sudo的配置文件/etc/sudoers
[root@Centos ~]# cat /etc/sudoers
# Sudoers allows particular users to run various commands as
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
## This file must be edited with the 'visudo' command.
## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
# Host_Alias MAILSERVERS = smtp, smtp2
## 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
修改時(shí)盡量復(fù)制系統(tǒng)的格式進(jìn)行相關(guān)修改,防止配置錯(cuò)誤,難以改正
修改授權(quán)某用戶權(quán)限成功后,切換到用戶下面,用sudo -l來查看自己擁有哪些權(quán)限
[yuw001@Centos ~]$ sudo -l
[sudo] password for yuw001:
User yuw001 may run the following commands on this host:
(root) /bin/ping, /bin/hostname, /usr/bin/free, /sbin/route,
/bin/netstat
使用命令時(shí)記得加上sudo
[yuw001@Centos ~]$ hostname linux
hostname: you must be root to change the host name
[yuw001@Centos ~]$ /bin/hostname linux
hostname: you must be root to change the host name
[yuw001@Centos ~]$ sudo hostname linux
[root@linux ~]# 退出重新登陸后發(fā)現(xiàn)主機(jī)名修改成功
配置文件一行是一個(gè)規(guī)則,前面都會(huì)用#進(jìn)行注釋,用‘\’續(xù)行(換行)
三、配置文件中規(guī)則的分類
1、別名類型
別名類型分為以下幾類
a、Host_Alias(主機(jī)別名)
生產(chǎn)環(huán)境中一般不會(huì)設(shè)置主機(jī)別名,一般主機(jī)別名不太常用
root ALL=(ALL) ALL 第一個(gè)ALL就是主機(jī)別名的應(yīng)用位置
b、User_Alias(用戶別名)
如果是表示用戶組那么前面要加%
root ALL=(ALL) ALL root就是用戶別名的應(yīng)用位置
User_Alias ADMINS = jsmith, mikem
c、Runas_Alias別名
此別名是指定“用戶身份”,即 sudo允許切換到的用戶
root ALL=(ALL) ALL 第二個(gè)(ALL)就是用戶別名的應(yīng)用位置
Runas_Alias OP = root
d、Cmnd_Alias(命令別名)
就是定義一個(gè)別名,它可以包含一堆命令的內(nèi)容(一組相關(guān)命令的集合)
root ALL=(ALL) ALL 第三個(gè)ALL就是用戶別名的應(yīng)用位置
Cmnd_Alias DRIVERS = /sbin/modprobe
說明
用戶別名中的用戶必須是系統(tǒng)真實(shí)存在的,書寫時(shí)注意空格,用戶別名具有特殊意義,用戶別名必須使用大寫
命令別下的成員必須使用絕對(duì)路徑,可以用‘\’換行
2、授權(quán)規(guī)則
授權(quán)規(guī)則就是執(zhí)行的規(guī)則,授權(quán)中的所有ALL必須大寫
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
yumw ALL=(ALL) /usr/sbin/useradd,/usr/sbin/userdel
###user group sa allow to run commands anywhere
yuw ALL=/usr/sbin*,/sbin*
sa ALL= /usr/sbin*,/sbin*,!/sbin/fdisk
!表示禁止執(zhí)行這個(gè)命令
[sa@linux ~]$ sudo -l
User sa may run the following commands on this host:
(root) /usr/bin*, (root) /sbin*, (root) !/sbin/fdisk
[sa@linux ~]$ sudo fdisk
Sorry, user sa is not allowed to execute '/sbin/fdisk' as root on linux.
如果將配置做下修改
###user group sa allow to run commands anywhere
yuw ALL=/usr/sbin*,/sbin*
sa ALL= !/sbin/fdisk,/usr/sbin*,/sbin*
[sa@linux ~]$ sudo -l
User sa may run the following commands on this host:
(root) /usr/bin*, (root) /sbin*, (root) !/sbin/fdisk
[root@linux ~]# su - sa
[sa@linux ~]$ sudo fdisk
[sudo] password for sa:
Usage:
fdisk [options] disk change partition table
fdisk [options] -l disk list partition table(s)
fdisk -s partition give partition size(s) in blocks
Options:
-b size sector size (512, 1024, 2048 or 4096)
-c switch off DOS-compatible mode
-h print help
-u size give sizes in sectors instead of cylinders
-v print version
-C number specify the number of cylinders
-H number specify the number of heads
-S number specify the number of sectors per track
所以經(jīng)測(cè)試結(jié)果表明,sa ALL= !/sbin/fdisk,/usr/sbin*,/sbin*命令執(zhí)行的匹配規(guī)則是從后到前的,所以后面執(zhí)行sudo fdisk不會(huì)提示權(quán)限不足的現(xiàn)像。
分享標(biāo)題:sudo命令高級(jí)使用方法
文章路徑:http://m.5511xx.com/article/dphggsp.html


咨詢
建站咨詢
