Sudo是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,减少了root用户的登陆和管理时间,提高了安全性,Sudo不是对shell的一个代替,它是面向每个命令的。

Linux系统的修改权限与默认权限,它都是针对用户对于目录或文件的一些权限控制,那么其实真正从安全性角度上来考虑的话,是要控制用户一定执行命令的权限,也就是哪些用户可以执行哪些命令,不可以执行哪些命令,因此也就有了sudo这个应用

对于sudo提权,也就是修改/etc/sudoers的配置文件

[root@Centos ~]# 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 -c

3、确保/etc/sudoers默认的权限是440(防止权限误用)

4、及时验证修改的配置是否正确

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

修改时尽量复制系统的格式进行相关修改,防止配置错误,难以改正

修改授权某用户权限成功后,切换到用户下面,用sudo -l来查看自己拥有哪些权限

[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

使用命令时记得加上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 ~]#           退出重新登陆后发现主机名修改成功

配置文件一行是一个规则,前面都会用#进行注释,用‘\’续行(换行)

三、配置文件中规则的分类
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_Alias  OP = 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)       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

!表示禁止执行这个命令

[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

所以经测试结果表明,sa ALL= !/sbin/fdisk,/usr/sbin*,/sbin*命令执行的匹配规则是从后到前的,所以后面执行sudo fdisk不会提示权限不足的现像。

本文转载地址:https://www.linuxprobe.com/linux-sudo-use.html

Linux服务器权限管理之sudo高级应用的更多相关文章

  1. Linux中权限管理之sudo权限

    1.suodo的操作对象是系统命令 2.root把本来只能是超级用户执行的命令赋予普通用户执行 3.设置sudo权限 命令:visudo 找到: ## Allow root to run any co ...

  2. windows下运行的linux服务器批量管理工具(带UI界面)

    产生背景: 由于做服务器运维方面的工作,需要一人对近千台LINUX服务器进行统一集中的管理,如同时批量对LINUX服务器执行相关的指令.同时批量对LINUX服务器upload程序包.同时批量对LINU ...

  3. linux用户管理,linux用户口令管理,linux用户组管理,linux用户权限管理详解

    linux用户管理 http://www.qq210.com/shoutu/android 用户账号的添加(新加用户需添加用户口令) :增加用户账号就是在/etc/passwd文件中为新用户增加一条记 ...

  4. Gerrit服务器权限管理

    Gerrit服务器权限管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Gerrit权限概述 1>.对象 Gerrit识别单个或多个人员集合. Gerrit不允许使用单 ...

  5. Linux目录权限管理

    Linux目录权限管理   实验目标: 通过本实验掌握centos7/rhel7目录权限的管理.包括配置目录的所属组.SGID.读/写/执行权限等. 实验步骤: 1.创建目录/home/instruc ...

  6. Linux系统文件权限管理(6)

    Linux操作系统是多任务(Multi-tasks)多用户(Multi-users)分时操作系统,linux操作系统的用户就是让我们登录到linux的权限,每当我们使用用户名登录操作系统时,linux ...

  7. Linux之权限管理

    一.文件基本权限 1) 基本权限的修改 第一位"-"为文件类型(-代表文件:d代表目录:l代表软链接文件即快捷方式),后面每3位一组. -rw-r--r-- rw-   u所有者 ...

  8. Linux的权限管理操作-Linux从入门到精通第七天(非原创)

    文章大纲 一.权限概述二.权限设置三.属主与属组设置四.扩展五.学习资料下载六.参考文章 一.权限概述 总述:Linux系统一般将文件可存/取访问的身份分为3个类别:owner.group.other ...

  9. Linux 文件权限管理

    1.文件权限的概述 在Linux系统下,使用权限来保护资源的安全将是一种不错的选择.系统中每个文件的权限都有可读(r).可写(w)和可执行(x)这三种权限,它们分别对应权限数值4.2 和1.系统为每个 ...

随机推荐

  1. HTML基础-DAY2

    表单标签form 功能:表单用于向服务器传输数据,从而实现用户与Web服务器的交互 表单能够包含input系列标签,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含textarea.sel ...

  2. webstorm2018最新激活码license server

    2018.7.5最新激活码: license server:https://s.tuzhihao.com:666/ 以后持续更新....

  3. ubuntu16.04查看软件的安装位置

    以chromium-browser为例 find命令 totoro@SWH:~$ sudo find / -name chromium-browser /usr/lib/chromium-browse ...

  4. windows svn 客户端连不上linux svn server

    采坑记录:linux服务器上svn://127.0.0.1可以正常使用,windows客户端远程连接不上,说明是端口号的问题. linux正常配置了iptables开启了3690端口,连接不上. 干脆 ...

  5. [POI2015]Logistyka

    [POI2015]Logistyka 题目大意: 一个长度为\(n(n\le10^6)\)的数列\(A_i\),初始全为\(0\).操作共\(m(m\le10^6)\)次,包含以下两种: 将\(A_x ...

  6. Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题

    A. Little Pony and Expected Maximum Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  7. 推荐C#网站、书籍、资源

    推荐博客: 极简的随笔 http://www.cnblogs.com/guwei4037/p/3499135.html 见证大牛成长之路的专栏 http://blog.csdn.net/shanyon ...

  8. 小程序swiper 快速滑动闪屏

    bindchange: function(e){ if(e.detail.source == "touch") { this.setData({ current: current ...

  9. Redis在Windows+linux平台下的安装配置(转)

    window平台Redis安装 下载地址: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload Redis文件夹有以下几个文 ...

  10. QT 安装 4.8.7 on solaris 10

    1.  下载 QT 4.8.7: http://download.qt.io/official_releases/qt/4.8/4.8.7/qt-everywhere-opensource-src-4 ...