Linux命令-sudo

sudo命令用于给普通用户提供额外权利来完成原本只有超级用户才有权限完成的任务,

格式:sudo [参数] 命令名称

sudo命令与su命令的区别是,su命令允许普通用户完全变更为超级管理员的身份,但

如此一来便增加了安全隐患,而使用sudo命令可以仅将特定的命令/程序执行权限赋予

指定的用户。

sudo命令的特色:

1:限制用户执行指定的命令
2:记录用户执行的每一条命令
3:验证过密码后5分钟(默认值)内无需再让用户验证密码,更加方便。

sudo程序的配置文件为/etc/sudoers,只有超级用户可以使用visudo编辑该文件。

实例1:使用visudo命令编辑sudo程序的配置文件,在第99行添加参数允许pentest用户

能够从任意主机执行任意命令的参数。

     1  ## Sudoers allows particular users to run various commands as
     2  ## the root user, without needing the root password.
     3  ##
     4  ## Examples are provided at the bottom of the file for collections
     5  ## of related commands, which can then be delegated out to particular
     6  ## users or groups.
     7  ##
     8  ## This file must be edited with the ‘visudo‘ command.
     9
    10  ## Host Aliases
    11  ## Groups of machines. You may prefer to use hostnames (perhaps using
    12  ## wildcards for entire domains) or IP addresses instead.
    13  # Host_Alias     FILESERVERS = fs1, fs2
    14  # Host_Alias     MAILSERVERS = smtp, smtp2
    15
    16  ## User Aliases
    17  ## These aren‘t often necessary, as you can use regular groups
    18  ## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
    19  ## rather than USERALIAS
    20  # User_Alias ADMINS = jsmith, mikem
    21
    22
    23  ## Command Aliases
    24  ## These are groups of related commands...
    25
    26  ## Networking
    27  # 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
    28
    29  ## Installation and management of software
    30  # Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
    31
    32  ## Services
    33  # Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable
    34
    35  ## Updating the locate database
    36  # Cmnd_Alias LOCATE = /usr/bin/updatedb
    37
    38  ## Storage
    39  # Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount
    40
    41  ## Delegating permissions
    42  # Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp
    43
    44  ## Processes
    45  # Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
    46
    47  ## Drivers
    48  # Cmnd_Alias DRIVERS = /sbin/modprobe
    49
    50  # Defaults specification
    51
    52  #
    53  # Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
    54  #         You have to run "ssh -t hostname sudo <cmd>".
    55  #
    56  Defaults    requiretty
    57
    58  #
    59  # Refuse to run if unable to disable echo on the tty. This setting should also be
    60  # changed in order to be able to use sudo without a tty. See requiretty above.
    61  #
    62  Defaults   !visiblepw
    63
    64  #
    65  # Preserving HOME has security implications since many programs
    66  # use it when searching for configuration files. Note that HOME
    67  # is already set when the the env_reset option is enabled, so
    68  # this option is only effective for configurations where either
    69  # env_reset is disabled or HOME is present in the env_keep list.
    70  #
    71  Defaults    always_set_home
    72
    73  Defaults    env_reset
    74  Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
    75  Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
    76  Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
    77  Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
    78  Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
    79
    80  #
    81  # Adding HOME to env_keep may enable a user to run unrestricted
    82  # commands via sudo.
    83  #
    84  # Defaults   env_keep += "HOME"
    85
    86  Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
    87
    88  ## Next comes the main part: which users can run what software on
    89  ## which machines (the sudoers file can be shared between multiple
    90  ## systems).
    91  ## Syntax:
    92  ##
    93  ##      user    MACHINE=COMMANDS
    94  ##
    95  ## The COMMANDS section may have other options added to it.
    96  ##
    97  ## Allow root to run any commands anywhere
    98  root    ALL=(ALL)       ALL
    99  pentest ALL=(ALL)       ALL
   100  ## Allows members of the ‘sys‘ group to run networking, software,
   101  ## service management apps and more.
   102  # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
   103
   104  ## Allows people in group wheel to run all commands
   105  %wheel  ALL=(ALL)       ALL
   106
   107  ## Same thing without a password
   108  # %wheel        ALL=(ALL)       NOPASSWD: ALL
   109
   110  ## Allows members of the users group to mount and unmount the
   111  ## cdrom as root
   112  # %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
   113
   114  ## Allows members of the users group to shutdown this system
   115  # %users  localhost=/sbin/shutdown -h now
   116
   117  ## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
   118  #includedir /etc/sudoers.d
[[email protected] ~]#

切换到pentest用户查看可以执行的命令,提示为ALL,即可以执行所有超级管理员的命令。

[[email protected] ~]# su - pentest
上一次登录:五 9月  9 13:29:34 CST 2016pts/1 上
[[email protected] ~]$ sudo -l
[sudo] password for pentest:
匹配此主机上 pentest 的默认条目:
    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

用户 pentest 可以在该主机上运行以下命令:
    (ALL) ALL

使用普通ls命令来查看/root文件夹出现“权限不够”,然后使用sudo ls后便可以查看了。

[[email protected] ~]$ ls /root/
ls: 无法打开目录/root/: 权限不够
[[email protected] ~]$ sudo ls /root/
[sudo] password for pentest:
anaconda-ks.cfg       testA  testC  公共  视频  文档  音乐
initial-setup-ks.cfg  testB  yum    模板  图片  下载  桌面
[[email protected] ~]$

实例2:仅允许pentest以root身份执行cat命令

pentest用户先以普通权限cat文件/etc/shadow发现权限不够[[email protected] ~]$ cat /etc/shadow
cat: /etc/shadow: 权限不够

切换至root用户给予pentest用户cat权限
[[email protected] ~]$ su - root
密码:
上一次登录:五 9月  9 14:12:10 CST 2016pts/1 上
[[email protected] ~]# visudo
[[email protected] ~]# su - pentest
上一次登录:五 9月  9 14:12:30 CST 2016pts/1、 上

赋予执行cat权限[[email protected] ~]# visudoroot    ALL=(ALL)       ALLpentest ALL=(root)      /bin/cat

继续使用普通cat确认是否可以查看/etc/shadow提示权限不够
[[email protected] ~]$ cat /etc/shadow
cat: /etc/shadow: 权限不够

使用sudo cat查看/etc/shadow发现可以查看了。
[[email protected] ~]$ sudo cat /etc/shadow
root:$6$Y6LHG5EEAGs3JMUM$jcEE.RZgMF9mO/xiPVA522l1Ek8JZ2Nkl.9nCBuiUWAH/.F84Kj6XyNxbuecW1M4BNGpryB/10Ncp.EGu9VhZ/::0:99999:7:::
bin:*:16579:0:99999:7:::
daemon:*:16579:0:99999:7:::
adm:*:16579:0:99999:7:::
lp:*:16579:0:99999:7:::
sync:*:16579:0:99999:7:::
shutdown:*:16579:0:99999:7:::
halt:*:16579:0:99999:7:::
mail:*:16579:0:99999:7:::
operator:*:16579:0:99999:7:::
games:*:16579:0:99999:7:::
ftp:*:16579:0:99999:7:::
nobody:*:16579:0:99999:7:::
avahi-autoipd:!!:17050::::::
ods:!!:17050::::::
pegasus:!!:17050::::::
systemd-bus-proxy:!!:17050::::::
systemd-network:!!:17050::::::
dbus:!!:17050::::::
polkitd:!!:17050::::::
sssd:!!:17050::::::
colord:!!:17050::::::
apache:!!:17050::::::
tss:!!:17050::::::
unbound:!!:17050::::::
usbmuxd:!!:17050::::::
abrt:!!:17050::::::
amandabackup:!!:17050::::::
saslauth:!!:17050::::::
libstoragemgmt:!!:17050::::::
geoclue:!!:17050::::::
memcached:!!:17050::::::
rpc:!!:17050:0:99999:7:::
postfix:!!:17050::::::
setroubleshoot:!!:17050::::::
rtkit:!!:17050::::::
chrony:!!:17050::::::
mysql:!!:17050::::::
qemu:!!:17050::::::
ntp:!!:17050::::::
rpcuser:!!:17050::::::
nfsnobody:!!:17050::::::
radvd:!!:17050::::::
named:!!:17050::::::
pcp:!!:17050::::::
pulse:!!:17050::::::
hsqldb:!!:17050::::::
tomcat:!!:17050::::::
pkiuser:!!:17050::::::
gdm:!!:17050::::::
gnome-initial-setup:!!:17050::::::
avahi:!!:17050::::::
postgres:!!:17050::::::
dovecot:!!:17050::::::
dovenull:!!:17050::::::
sshd:!!:17050::::::
oprofile:!!:17050::::::
tcpdump:!!:17050::::::
pentest:$6$6U3Z2n.sd63M32ZS$tzQJg852/1G3Mw7uv1.Ipbh.lOusvfd47Ih52xxku7okBBb/nu.Vn5V4mB50SSCMfaspqeGSDLcPM7XdgLE2w/::0:99999:7:::
[[email protected] ~]$
时间: 2024-08-30 02:53:07

Linux命令-sudo的相关文章

Linux 命令 sudo

sudo 这个命令. 是为了 让 普通用户 ,也能够以root的身份来运行 操作, 而这些普通用户 又不须要知道root的password. 在 sudo 运行命令的时候. 仅仅须要 输入自己的password ,让sudo知道 .你是这个普通本人,那么就能够以root运行操作了. 当然,这个须要配置一下 sudo ,将 这个普通用户 增加sudo 同意的范围. 用户能够通过sudo -v来查看自己是否是在sudoers 之中. 配置sudo 用 visudo 增加 fupeng ALL=(AL

Linux命令sudo实现集权(提权)管理,防止超级权限泛滥

sudo小结1.别名要用大写2.使用"\"换行3.使用白名单策略,尽量不要赋予ALL权限(建议先关后开)4.禁止的权限放在最后,允许的权限放在前面,sudoers配置文件的权限匹配时从后到前的5.成员必须是存在的(用户必须存在)6."!"表示禁止权限7.命令.组员.组等,用","分隔开(英文逗号)8.组前面一定要带"%" su命令缺点1.普通用户需要知道root密码,并且切换到root用户下,才能使用root权限(超级权限)2

sudo命令:解决使用Linux命令行时出现的错误提示

你在使用 Linux 命令行时曾经得到过"拒绝访问(Permission denied)"的错误提示吗?这可能是因为你正在尝试执行一个需要 root 权限的操作.例如,下面的截图展示了当我尝试复制一个二进制文件到一个系统目录时产生的错误. shell 的拒绝访问 那么该怎么解决这个错误?很简单,使用 sudo 命令. 用 sudo 运行命令 用户运行此命令后会被提示输入他们(自己)的登录密码.一旦输入了正确的密码,操作将会成功执行. 毫无疑问,sudo 是任何在 Linux 上使用命令

Linux学习总结(六)-su命令 sudo 命令 限制root远程登录

root 用户拥有至高无上的权利,那么我们运维人员是不是直接在root用户下处理所有问题呢?答案是否定的,权力越大,责任越大,人是会犯错的,因此我们要在不影响我们的工作情况下,尽量限制我们的权力,以免误操作引发灾难 一 su 命令 ---账户之间的切换 普通用户切root用户或者普通用户之间的切换都需要对方输入对方账户登录密码,root用户进普通用户不需要密码验证,这也是权力的体现.命令格式: su username su - username 如果不带用户名,默认root用户不带 - 的切换是

Ubuntu Linux使用sudo命令搭建java环境

搬运stackoverflow 注意,以下所有命令需要在root权限下执行 1. 在Ubuntu下打开终端命令或用ssh连接到linux. 2. 更新仓库(只有Ubuntu17.4及以下系统可用): sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-get update 3. 依次输入以下命令 sudo apt-get install openjdk-8-jdk sudo apt-get install openjdk-8-source #安装

9个使用时必须时刻警惕的Linux命令

Linux shell/terminal命令非常强大,即使一个简单的命令就可能导致文件夹.文件或者路径文件夹等被删除.为了避免这样的事情发生,我们应该时刻注意PHP代码&命令,今天为大家带来9个必须时刻警惕的Linux命令&代码. Linux shell/terminal 命令非常强大,即使一个简单的命令就可能导致文件夹.文件或者路径文件夹等被删除. 在一些情况下,Linux 甚至不会询问你而直接执行命令,导致你丢失各种数据信息. 一般来说在 Web 上推荐新的 Linux 用户执行这些命

linux命令学习笔记

操作文件和目录: copy: $ cp file1 file2 $ cp -r dir1 dir2 move: $ mv file .. $ mv file dir/ rename: $ mv file1 file2 $ mv dir1 dir2 # dir2如果存在,则为移动操作 remove: $ rm file $ rm -r dir 创建文件: $ touch a.txt $ >a.txt 创建目录: $ mkdir dir 查看文件: #一般 $ cat file #查看文件类型: $

cv:显示Linux命令运行进度

cv: 显示 cp.mv 等命令的进度 2014-07-14 By toy Posted in Apps Edit on GitHub 在 Linux 系统中 , 大多数命令从来都是信奉 “ 沉默是金 ” 的准则 , 所以当我们利用 cp 复制文件的时候并不能看到所谓的进度条 . 如果你在意这一点 , 那么不妨来用用 cv. cv 是 Coreutils Viewer, 它能够显示传输数据的进度 , 包括百分比 . 大小 . 以及速率等信息 .cv 支持 coreutils 中的基本命令 , 比

自学Linux命令的四种方法

自学Linux命令的四种方法 导读 童鞋们刚接触linux时,在学习过程中中会遇到不少问题,学习linux摸不着头脑,那么下面介绍四种linux的学习方法,特别适合新手. 方法一:终端"每日提示" 在.bashrc中(/home/.bashrc)增加如下一行: echo "Did you know that:"; whatis$(ls /bin | shuf -n 1) 你只需要增加这行就够了!如果你想让它更娱乐化一些,你可以安装cowsay.Ubuntu/Debi