zoukankan      html  css  js  c++  java
  • sudo的使用

    su:
    语法:
    su [OPTION]... [-] [USER [ARG]...]
    简介:
    切换到指定的user上
    常用选项
    (1) -,-l,--login :不引用当前shell的环境变量,创建一个全新的,包含了user参数的HOME,SHELL,LOGNAME,PATH
    (2)-c,--command=command:
    使用指定的用户执行command
    [python@dcx ~]$ su - root -c "alias" #执行单条数据
    Password: 
    alias cp='cp -i'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    [python@dcx ~]$ su - root -c "alias;ls -l" #执行多条数据
    Password: 
    alias cp='cp -i'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    total 112
    -rw-------. 1 root root  1592 Sep 15 01:15 anaconda-ks.cfg
    drwxr-xr-x. 2 root root  4096 Oct 28 02:04 Desktop
    drwxr-xr-x. 2 root root  4096 Sep 15 02:59 Documents
    drwxr-xr-x. 2 root root  4096 Sep 15 02:59 Downloads
    -rw-r--r--. 1 root root   775 Jan  5 15:49 fstab
    -rw-r--r--. 1 root root 50968 Sep 15 01:15 install.log
    -rw-r--r--. 1 root root 11504 Sep 15 01:13 install.log.syslog
    drwxr-xr-x. 2 root root  4096 Sep 15 02:59 Music
    drwxr-xr-x. 2 root root  4096 Sep 15 02:59 Pictures
    drwxr-xr-x. 2 root root  4096 Sep 15 02:59 Public
    -rw-r--r--. 1 root root   333 Nov  9 19:58 route.rule
    drwxr-xr-x. 2 root root  4096 Sep 15 02:59 Templates
    drwxr-xr-x. 2 root root  4096 Sep 15 02:59 Videos

    sudo:
    能够让获得授权的用户以另外一个用户的身份运行指定的命令;(不单纯的指root)
    授权机制:授权文件 /etc/sudoers
    ## user MACHINE=COMMANDS
    ##
    ## The COMMANDS section may have other options added to it.
    ##
    ## Allow root to run any commands anywhere 
    root ALL=(ALL) ALL
    who where=(whom) commands#
    Who:运行命令的用户 user
    where:通过哪些主机 host
    Whom:以哪个用户的身份 runas
    command:运行哪些命令 command
    root ALL=(ALL) ALL
    %wheel ALL=(ALL) ALL
    %wheel:%后面表示一个组,也就是说wheel这个组的所有用户可以使用sudo,执行任何人的用户
    举个栗子
    [root@dcx ~]# useradd dingcx
    [root@dcx ~]# passwd dingcx
    更改用户 dingcx 的密码 。
    新的 密码:
    无效的密码: 过于简单
    重新输入新的 密码:
    passwd: 所有的身份验证令牌已经成功更新。
    [root@dcx ~]# su - dingcx
    [dingcx@dcx ~]$ fdisk -l
    Cannot open /dev/sda
    Cannot open /dev/mapper/vg_dcx-lv_root
    Cannot open /dev/mapper/vg_dcx-lv_swap
    [dingcx@dcx ~]$ sudo fdisk -l #如果没有指定-u的话,默认就是切换到root权限下了

    We trust you have received the usual lecture from the local System
    Administrator. It usually boils down to these three things:

        #1) Respect the privacy of others.
        #2) Think before you type.
        #3) With great power comes great responsibility.

    [sudo] password for dingcx: 
    dingcx is not in the sudoers file.  This incident will be reported.
    #此时会告诉你,没有访问权限,下面将dingcx加入到wheel组中
    [root@dcx ~]# usermod -a -G  wheel dingcx
    [root@dcx ~]# 
    [root@dcx ~]# id dingcx
    uid=502(dingcx) gid=502(dingcx) 组=502(dingcx),10(wheel)
    [dingcx@dcx ~]$ sudo fdisk -l #成功了
    [sudo] password for dingcx: 

    Disk /dev/sda: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x000d6d9b
    [dingcx@dcx ~]$ sudo su - #这样就可以不用在输入root密码情况下切换到root用户下
    [root@dcx ~]# fdisk -l

    Disk /dev/sda: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x000d6d9b
    #

    编译此文件的专用命令:visudo,这个比直接vim /etc/sudoers更好的就是,可以验证语法(可以通过man sudoers来查看具体的使用方法)
    users:
    username
    #uid
    %groupname
    %#gid
    user_alias
    支持将多个用户定义为一组用户,称之为用户别名,即user_alias;

    hosts:
    ip
    hostname
    NetAddr
    host_alias

    runas:
    ...
    runas_alias

    commands:
    command
    directory
    sudoedit:特殊权限,可用于向其它用户授予sudo权限;
    cmnd_alias

    上面四个字段都可以定义别名,其方法为:
    ALIAS_TYPE NAME=item1, item2, item3, ...
    NAME:别名名称,必须使用全大写字符;
    ALIAS_TYPE:
    User_Alias
    Host_Alias
    Runas_Alias
    Cmnd_Alias

    例如:
    User_Alias NETADMIN=dingcx, dcx
    Cmnd_Alias NETCMND=/sbin/ip, /sbin/ifconfig, /sbin/route

    NETADMIN localhost=(root) NETCMND
    例如:
    [root@dcx ~]# visudo
    ## rather than USERALIAS
    User_Alias NETADMINS = jsmith, mikem, dingcx

    ## 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, /sbin/fdisk,/bin/ls

    NETADMINS ALL=(root) NETWORKING
    [root@dcx ~]# gpasswd -d dingcx wheel #将dingcx从wheel组中删除
    正在将用户“dingcx”从“wheel”组中删除


    sudo命令:
    检票机制:能记录成功认证结果一段时间,默认为5分钟;

    以sudo的方式来运行指定的命令;
    sudo [options] COMMAND

    -l[l] command 列出用户能执行的命令
    -k 清除此前缓存用户成功认证结果;
    -u 指定用户来执行命令,不指定的话,是root,是否有指定用户权限执行的,就要看runas里面是会否有了

    /etc/sudoers应用示例:

    Cmnd_Alias USERADMINCMNDS = /usr/sbin/useradd, /usr/sbin/usermod, /usr/bin/passwd [a-z]*, !/usr/bin/passwd root

    User_Alias USERADMIN = bob, alice

    USERADMIN ALL=(root) USERADMINCMNDS

    常用标签:
    NOPASSWD:
    PASSWD:

  • 相关阅读:
    Objective-C语言的对象、功能和方法
    【Swift语言】可选类型
    静态库调用中“unrecognized selector sent to instance”错误
    Xcode的坑
    Xcode6中变量初始化的问题
    iOS数据存储的方式总结
    KVO
    linux一些工具的安装(三)
    linux一些工具的安装(二)
    linux的基本操作(一)
  • 原文地址:https://www.cnblogs.com/dingcx/p/12155745.html
Copyright © 2011-2022 走看看