Gojira 哥斯拉Gojira 哥斯拉

哥斯拉爱好者的日常
生活相当无趣 我得找点乐子

Linux关于su,sudo,/etc/sudoers的一些事+禁止root用户ssh登录

gojira.net

sudo是Linux平台上的一个非常有用的工具,它允许系统管理员分配给普通用户一些合理的“权限”,让他们执行一些只有超级用户或其他 特许用户才能完成的任务,比如:运行一些像mount,halt,su之类的命令,或者编辑一些系统配置文件,像/etc/mtab,/etc /samba/smb.conf等。这样以来,就不仅减少了root用户的登陆次数和管理时间,也提高了系统安全性。
为了服务器的安全,为了不再发生rm -rf /这样的低级错误导致的悲剧,以后还是别用root用户登陆管理服务器了,所以有了这篇文章。
以下测试、代码、命令全都基于centos6.x下面,其他版本的linux未测试,应该差不多,如有区别请自己测试好再拿到服务器上用。
先丢一个简单直接可以用的命令出来,作用:在centos中启用sudo,添加一个普通用户并sudo权限赋给他,且使用sudo的时候不用输入密码,并禁止root帐号直接登录。

[php]
# 先增加一个普通权限的用户,并设置密码为FCBUCOM789(请按需修改)
useradd fcbucom
echo 'FCBUCOM789' |passwd fcbucom --stdin

# 修改/etc/sudoers,启用sudo,新增一个普通用户fcbucom
sed -i '/root\tALL=(ALL) \tALL/a\fcbucom\tALL=(ALL) \tNOPASSWD: ALL' /etc/sudoers

# 编辑/etc/ssh/sshd_config 禁止root用户登录
sed -i "s@PermitRootLogin yes@#PermitRootLogin yes@g" /etc/ssh/sshd_config
sed -i '/PermitRootLogin yes/a\PermitRootLogin no' /etc/ssh/sshd_config

# 重启sshd
service sshd reload
[/php]

如果你是个懒人,那么看到这里就已经足够啦。

Linux系统中,有时候普通用户有些事情是不能做的,除非是root用户才能做到。这时就需要用su命令临时切换到root身份来做事了。

su:substitute['sʌbstɪtjuːt]代替 user

su 的语法为:

su [OPTION选项参数] [用户]

-, -l, --login 登录并改变到所切换的用户环境

-c, --commmand=COMMAND 执行一个命令,然后退出所切换到的用户环境;

用su命令切换用户后,可以用 exit 命令或快捷键[Ctrl+D]可返回原登录用户。

例子:

su 在不加任何参数,默认为切换到root用户,但没有转到root用户家目录下,也就是说这时虽然是切换为root用户了,但并没有改变root登录环境;用户默认的登录环境,可以在/etc/passwd 中查得到,包括家目录,SHELL定义等;

su 加参数 - ,su - 表示默认切换到root用户,并且改变到root用户的环境 (简单的说,这个时候你就是root);具体说明请移步至:http://linux.fcbu.com/linux-su-and-other-su.htm

用su是可以切换用户身份,如果每个普通用户都能切换到root身份,如果某个用户不小心泄漏了root的密码,那岂不是系统非常的不安全?没有错,为了改进这个问题,产生了sudo这个命令。使用sudo执行一个root才能执行的命令是可以办到的,但是需要输入密码,这个密码并不是root的密码而是用户自己的密码。默认只有root用户能使用sudo命令,普通用户想要使用sudo,是需要root预先设定的,即,使用visudo命令去编辑相关的配置文件/etc/sudoers。如果没有visudo这个命令,请使用 "yum install -y sudo" 安装。

默认root能够sudo是因为这个文件中有一行” root ALL=(ALL) ALL”

sudo 的适用条件:

由于su对切换到超级权限用户root后,权限的无限制性,所以su并不能担任多个管理员所管理的系统。如果用su来切换到超级用户来管理系统,也不能明 确哪些工作是由哪个管理员进行的操作。特别是对于服务器的管理有多人参与管理时,最好是针对每个管理员的技术特长和管理范围,并且有针对性的下放给权限, 并且约定其使用哪些工具来完成与其相关的工作,这时我们就有必要用到 sudo。

通过sudo,我们能把某些超级权限有针对性的下放,并且不需要普通用户知道root密码,所以sudo 相对于权限无限制性的su来说,还是比较安全的,所以sudo 也能被称为受限制的su ;另外sudo 是需要授权许可的,所以也被称为授权许可的su;

sudo 执行命令的流程:是当前用户切换到root(或其它指定切换到的用户),然后以root(或其它指定的切换到的用户)身份执行命令,执行完成后,直接退回到当前用户;而这些的前提是要通过sudo的配置文件/etc/sudoers来进行授权;

从编写 sudo 配置文件/etc/sudoers开始

sudo的配置文件是/etc/sudoers ,我们可以用他的专用编辑工具visudo ,此工具的好处是在添加规则不太准确时,保存退出时会提示给我们错误信息;配置好后,可以用切换到您授权的用户下,通过sudo -l 来查看哪些命令是可以执行或禁止的;

/etc/sudoers 文件中每行算一个规则,前面带有#号可以当作是说明的内容,并不执行;如果规则很长,一行列不下时,可以用\号来续行,这样看来一个规则也可以拥有多个行;

/etc/sudoers 的规则可分为两类;一类是别名定义,另一类是授权规则;别名定义并不是必须的,但授权规则是必须的;

sudo授权规则(sudoers配置):

授权用户 主机=命令动作

这三个要素缺一不可,但在动作之前也可以指定切换到特定用户下,在这里指定切换的用户要用( )号括起来,如果不需要密码直接运行命令的,应该加NOPASSWD:参数,但这些可以省略;举例说明;

sudoers的默认配置:

[php]
## 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

## 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

# 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

## 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

## Allows members of the users group to shutdown this system
# %users localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
[/php]

1. 最简单的配置,让普通用户fcbucom具有root的所有权限

执行visudo之后,可以看见缺省只有一条配置:

root    ALL=(ALL) ALL

那么你就在下边再加一条配置:

fcbucom ALL=(ALL) ALL

这样,普通用户fcbucom就能够执行root权限的所有命令

以fcbucom用户登录之后,执行:

sudo su -

然后输入fcbucom用户自己的密码,就可以切换成root用户了

2. 让普通用户fcbucom只能在某几台服务器上,执行root能执行的某些命令

首先需要配置一些Alias,这样在下面配置权限时,会方便一些,不用写大段大段的配置。Alias主要分成4种

Host_Alias

Cmnd_Alias

User_Alias

Runas_Alias

1) 配置Host_Alias:就是主机的列表

Host_Alias      HOST_FLAG = hostname1, hostname2, hostname3

2) 配置Cmnd_Alias:就是允许执行的命令的列表,命令前加上!表示不能执行此命令.

命令一定要使用绝对路径,避免其他目录的同名命令被执行,造成安全隐患 ,因此使用的时候也是使用绝对路径!

Cmnd_Alias      COMMAND_FLAG = command1, command2, command3 ,!command4

3) 配置User_Alias:就是具有sudo权限的用户的列表

User_Alias USER_FLAG = user1, user2, user3

4) 配置Runas_Alias:就是用户以什么身份执行(例如root,或者oracle)的列表

Runas_Alias RUNAS_FLAG = operator1, operator2, operator3

5) 配置权限

配置权限的格式如下:

USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG

如果不需要密码验证的话,则按照这样的格式来配置

USER_FLAG HOST_FLAG=(RUNAS_FLAG) NOPASSWD: COMMAND_FLAG

GOJIRA.NET原创文章未经允许不得转载! 当前页面:Gojira 哥斯拉 » Linux关于su,sudo,/etc/sudoers的一些事+禁止root用户ssh登录

评论