1. 程式人生 > >Linux下sudo配置和用法

Linux下sudo配置和用法

Linux環境:CentOS 6.7

結構說明

可以通過編輯檔案/etc/sudoers來配置,通常使用visudo命令來進行修改,因為如果你修改的格式不符合它會進行提示。接下來就通過一個格式來了解它

<user> <host> = [<operator user> [<par>]] <command list>
chenmh localhost = (root)    NOPASSWD: /bin/mkdir test

<user>:指的是具體的使用者或者使用者別名,如果使用%user指的是使用者組。

<host>:指的是具體的host(可以是機器名也可以是ip)或者是host別名,ALL代表所有的host。

<operator user>:可選,指定可以用呼叫哪個使用者的來執行,ALL代表呼叫root使用者來執行。這裡要說明一下root使用者的許可權代表什麼意思,比如一個目錄的所有者是root,那麼必須具備root使用者的許可權才能執行相關操作,比如上面的chenmh使用者如果它配置的是其它使用者比如它自己,那麼它在root所有者的目錄下面是沒許可權執行操作的。同樣如果這裡配置的是哪個使用者那麼比如mkdir建立的資料夾就的所有者就是哪個使用者。預設不指定代表使用ALL

<par>:可選,指定引數,通常使用NOPASSWD(代表該使用者在執行sudo的時候不需要再輸入自己的密碼)。

<command list>:指定的具體命令或者是命令別名或者是ALL,ALL代表所有許可權。

說明: [<operator user> [<par>]]這兩個選項是可以選的可以不指定,如果不指定預設是呼叫root使用者執行,但是使用sudo必須輸入使用者自己的密碼

配置檔案

接下來詳細來看看它的配置檔案,它的配置檔案以及很詳細的告訴了我們該怎樣使用,在前面一部分是示例怎樣將一組許可權建立組別名,注意別名需要大寫,

## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## 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配置成一個host別名 ## Host Aliases ## Groups of machines. You may prefer to use hostnames (perhaps using ## wildcards for entire domains) or IP addresses instead. Host_Alias FILESERVERS = 192.168.137.40,192.168.137.30 # Host_Alias MAILSERVERS = smtp, smtp2 ##將多個user配置成一個user別名 ## 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 = chenmh ###接下來是命令別名,就是將一組命令放在一起,這樣可以簡便配置 ## Command Aliases ## These are groups of related commands... ## Networking # Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool ## Installation and management of software # Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum ## Services # Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig ## Updating the locate database # Cmnd_Alias LOCATE = /usr/bin/updatedb ## Storage # Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount ## Delegating permissions # Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp ## Processes # Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall ## Drivers # Cmnd_Alias DRIVERS = /sbin/modprobe ###這個是我自己配置的命令別名,將mkdir和rm命令配置在一起分配給某個使用者 ##userdefin Cmnd_Alias OTHERS = /bin/mkdir, /bin/rm # Defaults specification # # Disable "ssh hostname sudo <cmd>", because it will show the password in clear. # You have to run "ssh -t hostname sudo <cmd>". # Defaults requiretty # # Refuse to run if unable to disable echo on the tty. This setting should also be # changed in order to be able to use sudo without a tty. See requiretty above. # Defaults !visiblepw # # Preserving HOME has security implications since many programs # use it when searching for configuration files. Note that HOME # is already set when the the env_reset option is enabled, so # this option is only effective for configurations where either # env_reset is disabled or HOME is present in the env_keep list. # Defaults always_set_home Defaults env_reset Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS" Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE" Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES" Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE" Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY" # # Adding HOME to env_keep may enable a user to run unrestricted # commands via sudo. # # Defaults env_keep += "HOME" Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin ## Next comes the main part: which users can run what software on ## which machines (the sudoers file can be shared between multiple ## systems). ## 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 ## Allows members of the 'sys' group to run networking, software, ## service management apps and more. # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS ###sys這個組中的使用者可以執行相關的命令組的許可權,多個命令組用逗號分隔 ## Allows people in group wheel to run all commands # %wheel ALL=(ALL) ALL ## Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL ## Allows members of the users group to mount and unmount the ## cdrom as root # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom ###users組的使用者可以執行掛載和解除安裝/mnt/cdrom目錄的許可權 ## Allows members of the users group to shutdown this system # %users localhost=/sbin/shutdown -h now ###users組的使用者可以執行關機命令 ## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment) #includedir /etc/sudoers.d 

案例

1.使用者別名組中的使用者可以在FILESERVERS這組host裡面呼叫root使用者執行OTHERS命令組的許可權,多個命令組用逗號分隔

ADMINS  FILESERVERS=(ALL)    NOPASSWD:OTHERS

2.使用者chenmh這個使用者可以在本機以呼叫root使用者來執行所有的命令

chenmh localhost=(root) NOPASSWORD:ALL

3.使用者chenmh可以呼叫root使用者建立目錄test,也只能建立test目錄

chenmh ALL=(ALL)    NOPASSWD: /bin/mkdir test

4.dev組的使用者可以執行關機shutdown命令

%dev ALL=(ALL)    NOPASSWD:/sbin/shutdown

快捷操作 

 1.檢視當前使用者具備的sudo許可權

sudo -l
User chenmh may run the following commands on this host:
    (root) NOPASSWD: /bin/mkdir, /bin/rm

總結

配置sudo記得使用visudo命令,如果配置錯誤了儲存的時候它會有提示。 

相關推薦

Linuxsudo配置用法

Linux環境:CentOS 6.7 結構說明 可以通過編輯檔案/etc/sudoers來配置,通常使用visudo命令來進行修改,因為如果你修改的格式不符合它會進行提示。接下來就通過一個格式來了解它 <user> <host> = [<op

Linuxsudo配置

列表 ron linu sudo oot iot inux null ins 轉載請註明: 淩雲物網智科嵌入式實驗室: http://iot-yun.com/ 郭文學<[email protected]> [guowenxue@centos6

Db2數據庫在Linux的安裝配置

fig 用戶密碼 oca authent linux token reat local ken 一.DB2數據庫的安裝和配置: 1.安裝完成後,需要增加三個操作系統的組和三個操作系統用戶,如下: groupadd -g 999 db2iadm1 #(管理實例的組) gro

Linux更換jdk配置環境變量

pat 出現 下載jdk cnblogs clas home log code 文件 不需要刪除舊的jdk,安裝新版本的jdk,再更新環境變量即可。 Linux下安裝jdk,步驟如下 1:下載jdk包:本章使用的為後綴為tar.gz的文件(不需要安裝),如jdk-

LinuxMongodb安裝啟動配置

ever and tpi efault osi title 客戶端 避免 輸入數據 1.下載安裝包 wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.2.tgz 下載完成後解壓縮壓縮包 tar

python在windowslinux的安裝配置

str net .net cat ria 技術 連接 top 成功 一、windows下安裝python3.6 安裝編輯器:Ecplise+pydev插件 Eclipse是寫JAVA的IDE, 這樣就可以通用了,學習代價小。 學會了Eclipse, 以後寫Pytho

Linux安裝配置JDKTomcat操作步驟

下面以JDK7和Tomcat7為例,介紹CentOS7.5下安裝配置JDK和Tomcat的操作步驟。 1、安裝JDK 1.1、檢視yum庫中都有哪些jdk版本 yum search java|grep jdk1.2、選擇指定的版本安裝 yum install java-1.7.0-openj

Linux更換jdk配置環境變數

不需要刪除舊的jdk,安裝新版本的jdk,再更新環境變數即可。 Linux下安裝jdk,步驟如下    1:下載jdk包:本章使用的為字尾為tar.gz的檔案(不需要安裝),如jdk-8u111-linux-x64.tar.gz    2: 把jdk檔案儲存至Li

linux主從同步redis的用法

mariadb其實就是mysql mysql已經被oracle收購,它即將閉源,馬上要開始收費了因此還想免費試用開源的資料庫mysql,就在centos7上,將mysql分支為mariadb 安裝mariadb,在centos7底下:linux軟體包的格式 mysql.rpm 1.通過yum去安裝 1

LinuxMongoDB安裝配置詳解

1、建立MongoDB的安裝路徑 在/usr/local/  建立資料夾mongoDB 2、上傳檔案到Linux上的/usr/local/source目錄下 3、解壓檔案 進入到/usr/local/source目錄,執行如下命令:  tar -zxvf m

Mysql 在 windows linux 的安裝配置

這篇文章講解 Mysql 在 Windows 下的手動安裝和在 Linux(CentOs) 下的下載、配置、連線。還包括預設字符集等的設定。 首先,都要執行移除資料庫的操作 mysqld --remov

linuxmongoDB安裝基本用法

安裝mongoDB 1、下載: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz 2、解壓: tar -zxvf mongodb-linux-x86_64-3

LinuxMongoDB安裝配置詳解(一)

一、MongoDB的安裝 1.下載安裝包 下載方式: curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.9.tgz 解壓縮壓縮包: tar zxvf mongodb-linux-x

LinuxMongodb安裝啟動配置,遠端訪問等

Linux下Mongodb安裝和啟動配置(儘量不要使用root使用者) 1、下載解壓mongodb 解壓mongodb tar -xvf mongodb-linux-x86_64-amazon-3.6.0.tgz 2、安裝準備

Linux自動化部署配置工具

轉自:http://bbs.chinaunix.net/thread-3731873-1-1.html 系統部署一、Kickstart主要針對Redhat和CentOS作業系統的安裝和初始配置,不過網路的配置仍然需要一臺一臺地進行。二、Cobbler與Kickstart類

linuxps命令 grep命令用法

inux上程序有5種狀態: 1. 執行(正在執行或在執行佇列中等待) 2. 中斷(休眠中, 受阻, 在等待某個條件的形成或接受到訊號) 3. 不可中斷(收到訊號不喚醒和不可執行, 程序必須等待直到有中斷髮生) 4. 僵死(程序已終止, 但程序描述符存在, 直到父程序

Linux安裝配置MysqlRedis

  Linux下安裝Mysql(Mariadb) linux下如何安裝軟體? 1. yum安裝軟體也得注意,一個是配置yum源  1.我們當前的是阿里雲的yum源(下載速度特別快)  通過 yum install mariadb  發現版本如下,版本特別低,且安裝

Linuxjava使用配置

一、檢視jdk安裝目錄 [[email protected] ~]# whereis java java: /usr/bin/java /usr/local/java #java執行路徑 [[email protected] ~]# which java

linux 檢視服務與服務的執行級別以及自動啟動相關的配置

#service --status-all (檢視所有service的狀態,但有可能service並沒有實現status介面) #chkconfig --list (檢視服務的啟動設定,以及在哪一個執行級別自動啟動與否) #runlevel (檢視當前的執行級別) 詳

Linux解析配置檔案GKeyFile讀取配置檔案

# this is just an example # there can be comments before the first group [First Group] Name=Key File Exampletthis value showsnesc