siccy1950 发表于 2016-9-10 19:21:57

Linux服务器权限管理之sudo高级应用

本帖最后由 siccy1950 于 2016-9-10 19:24 编辑

Linux服务器权限管理之sudo高级应用


    Linux系统修改权限与默认权限,它都是征对用户对于目录或文件的一些权限控制,那么其实真正从安全性角度上来考虑的话,是要控制用户一定执行命令的权限,也就是哪些用户可以执行哪些命令,不可以执行哪些命令,因此也就有了sudo这个应用
对于sudo提权,也就是修改/etc/sudoers的配置文件# ls -ll /etc/sudoers-r--r-----. 1 root root 5870 Aug 19 16:53 /etc/sudoers可以看出/etc/sudoers默认的权限是440(也是系统比较安全的权限设置),当然了超级管理员肯定是有权限修改其文件内容的,不然还不哦哦了...........。
一:直接修改/etc/sudoers文件的注意事项1、操作时最好用echo >>追加,不过cat sed同样也可以实现(不常用)2、修改完成后一定记得检查语法visudo -c3、确保/etc/sudoers默认的权限是440(防止权限误用)
4、及时验证修改的配置是否正确5、确保知道root密码,以便普通用户可以通过sudo su -命令切换
二:sudo的配置文件/etc/sudoers# cat /etc/sudoers# 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 Aliases## Groups of machines. You may prefer to use hostnames (perhaps using ## wildcards for entire domains) or IP addresses instead.# Host_Alias   FILESERVERS = fs1, fs2# 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修改时尽量复制系统的格式进行相关修改,防止配置错误,难以改正修改授权某用户权限成功后,切换到用户下面,用sudo -l 来查看自己拥有哪些权限$ sudo -l password for yuw001: Matching Defaults entries for yuw001 on this host:    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS    DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL    PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE",    env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES",    env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE",    env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin, logfile=/var/log/sudo.log
User yuw001 may run the following commands on this host:    (root) /bin/ping, /bin/hostname, /usr/bin/free, /sbin/route,    /bin/netstat使用命令时记得加上sudo $ hostnamelinuxhostname: you must be root to change the host name$ /bin/hostname linuxhostname: you must be root to change the host name$ sudo hostname linux #         退出重新登陆后发现主机名修改成功配置文件一行是一个规则,前面都会用#进行注释,用‘\’续行(换行)
三:配置文件中规则的分类1、别名类型别名类型分为以下几类a、Host_Alias(主机别名)生产环境中一般不会设置主机别名,一般主机别名不太常用root    ALL=(ALL)       ALL         第一个ALL就是主机别名的应用位置    b、User_Alias(用户别名)如果是表示用户组那么前面要加%root    ALL=(ALL)       ALL      root就是用户别名的应用位置User_Alias ADMINS = jsmith, mikem

c、Runas_Alias别名此别名是指定“用户身份”,即 sudo允许切换到的用户root    ALL=(ALL)       ALL      第二个(ALL)就是用户别名的应用位置Runas_AliasOP = root
d、Cmnd_Alias(命令别名)就是定义一个别名,它可以包含一堆命令的内容(一组相关命令的集合)root    ALL=(ALL)      ALL       第三个ALL就是用户别名的应用位置Cmnd_Alias DRIVERS = /sbin/modprobe
说明
[*]用户别名中的用户必须是系统真实存在的,书写时注意空格,用户别名具有特殊意义,用户别名必须使用大写
[*]命令别下的成员必须使用绝对路径,可以用‘\’换行
2、授权规则
授权规则就是执行的规则,授权中的所有ALL必须大写## Allow root to run any commands anywhere root       ALL=(ALL)       ALLyumw    ALL=(ALL)       /usr/sbin/useradd,/usr/sbin/userdel
###user group sa allow to run commands anywhereyuwALL=/usr/sbin*,/sbin*sa    ALL= /usr/sbin*,/sbin*,!/sbin/fdisk!表示禁止执行这个命令 $ sudo -lUser sa may run the following commands on this host:
    (root) /usr/bin*, (root) /sbin*, (root) !/sbin/fdisk$ sudo fdisk
Sorry, user sa is not allowed to execute '/sbin/fdisk' as root on linux.如果将配置做下修改###user group sa allow to run commands anywhereyuwALL=/usr/sbin*,/sbin*sa    ALL= !/sbin/fdisk ,/usr/sbin*,/sbin* $ sudo -lUser sa may run the following commands on this host:
    (root) /usr/bin*, (root) /sbin*, (root) !/sbin/fdisk# su - sa$ sudo fdisk password for sa: Usage:
fdisk <disk>    change partition table fdisk -l <disk> list partition table(s) fdisk -s <partition>      give partition size(s) in blocksOptions:
-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所以经测试结果表明,sa    ALL= !/sbin/fdisk ,/usr/sbin*,/sbin*命令执行的匹配规则是从后到前的,所以后面执行sudo fdisk不会提示权限不足的现像
来文来自于民工哥个人微信公众号平台——友侃有笑,如需转载,烦请注明原处更多相关内容,也可关注微信公众号平台——友侃有笑,大家一同提高LINUX技术水平

阿波罗888 发表于 2016-9-11 08:21:26

很好!!!

siccy1950 发表于 2018-1-8 12:32:30

阿波罗888 发表于 2016-9-11 08:21
很好!!!

更多内容请关注民工哥公众号 :民工哥Linux运维
页: [1]
查看完整版本: Linux服务器权限管理之sudo高级应用