个人工具

“Quick HOWTO : Ch09 : Linux Users and Sudo/zh”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
完全以root用户登录
总结
 
第277行: 第277行:
 
= 总结 =
 
= 总结 =
  
知道怎样添加用户非常重要,不仅仅是他们可以登录我们的系统。大部分基于应用软件的服务器都是通过没有特权的用户帐号运行,例如MySQL数据库以用户mysql的身份运行,服务器应用程序Apache Web以用户apache的身份运行。这些帐号一般不会被自动建立,特别是当然这些软件是通过TAR文件安装的时候。<br>最后,sudo实际上提供了一种分散责任,系统管理多用户化的方法,。你甚至可以根据一些用户在组织中的角色让这些群体的用户的拥有局部的访问特权命令,这使得sudo成为任何公司的服务器管理和安全政策中非常有用的一部分。
+
知道怎样添加用户非常重要,不仅仅是他们可以登录我们的系统。大部分基于应用软件的服务器都是通过没有特权的用户帐号运行,例如MySQL数据库以用户mysql的身份运行,服务器应用程序Apache Web以用户apache的身份运行。这些帐号一般不会被自动建立,特别是当然这些软件是通过TAR文件安装的时候。<br>最后,sudo实际上提供了一种分散责任,系统管理多用户化的方法,。你甚至可以根据一些用户在组织中的角色让这些群体的用户拥有局部的访问特权命令,这使得sudo成为任何公司的服务器管理和安全政策中非常有用的一部分。
 
[[Category:Quick_HOWTO]]
 
[[Category:Quick_HOWTO]]

2011年4月13日 (三) 16:40的最新版本


简介

在我们开始之前,最好先讲一些基本的用户管理系统,这在以後的章节中是非常有用的。添加用户是管理 Linux 中非 常重要的一项操作。在这里你会看到几个为以後章节准备简单的例子。你不必精通这些命令,你只要对它们有印象就可以了。你可以用这个命令 man useradd 来获得 useradd 命令的帮助,或者用命令 man usermod 来进一步熟悉关于 usermod 命令来修改用户的帮助信息。

谁是超级用户?

超级用户被命名为 root,在 Linux 中超级用户可以不受任何限制地访问所有的系统资源和文件。超级用户有一个用户 ID,为 0,所有 Linux 应用软件都会将它视为具有最高权限的用户。你需要以 root 用户登录来为你的 Linux 服务器添加一个新的用户。

注意:当你安装 Ubuntu Linux 系统的时候,系统提示你创建的用户并不是 root 用户。root 会自动建立但是没有密码,所以最初你不能以 root 用户登录。用 sudo su 命令,第一个用户可以变成 root 用户,这在以后会论述。

怎样添加用户

添加用户需要一些计划,在你开始之前先浏览一下下面这几步:

1)按功能排列你要添加的用户所加入的组。在这个例子中有三个群组“parents”,“children"和“soho”。

Parents Children Soho

Paul Accounts Derek Jane Alice

2)在你的服务器中添加 Linux 群组:

[root@bigboy tmp]# groupadd parents
[root@bigboy tmp]# groupadd children
[root@bigboy tmp]# groupadd soho

3)添加 Linux 用户并且分配他们到他们各自的群组

[root@bigboy tmp]# useradd -g parents paul
[root@bigboy tmp]# useradd -g parents jane
[root@bigboy tmp]# useradd -g children derek
[root@bigboy tmp]# useradd -g children alice
[root@bigboy tmp]# useradd -g soho accounts
[root@bigboy tmp]# useradd -g soho sales

如果你不用 -g 指定群组,RedHat/Fedora Linux 会自动建立一个和你所添加的用户名字一样的群组;也就是用户私有组方案。每一个新用户在第一次登录的时候,都会被提示设置他们新的永久密码。

4)每一个用户的个人目录都被设定在 /home 目录下。目录的名字就是他们的用户名。

[root@bigboy tmp]# ll /home
drwxr-xr-x 2 root root 12288 Jul 24 20:04 lost+found
drwx------ 2 accounts soho 1024 Jul 24 20:33 accounts
drwx------ 2 alice children 1024 Jul 24 20:33 alice
drwx------ 2 derek children 1024 Jul 24 20:33 derek
drwx------ 2 jane parents 1024 Jul 24 20:33 jane
drwx------ 2 paul parents 1024 Jul 24 20:33 paul
drwx------ 2 sales soho 1024 Jul 24 20:33 sales
[root@bigboy tmp]# 

怎样更改密码

你需要为第一个帐户创建一个密码。这可以用 passwd 命令来完成。 系统会提示输入你的旧密码一次,输入你的新密码两次。

用 root 用户更改用户 paul 的密码

[root@bigboy root]# passwd paul
Changing password for user paul. New password:
Retype new password: passwd:
all authentication tokens updated successfully.
[root@bigboy root]#

用户以后可能会改他们自己的密码。这个例子说明用户 paul 改自己的密码没有特权。

[paul@bigboy paul]$ passwd
Changing password for paul Old password: your current password
Enter the new password (minimum of 5, maximum of 8 characters) 
Please use a combination of upper and lower case letters and numbers. 
New password: your new password
Re-enter new password: your new password
Password changed.
[paul@bigboy paul]$

怎样删除用户

userdel 命令可以用来把用户记录从目录 /etc/passwd 和登录进程所用到的目录 /etc/shadow 中删除。这个命令只有一个参数,就是你要删的用户名

该命令也有一个选项 -r用来将该用户在/home目录下所有的内容一起删除。用这个选项要小心。用户目录下的数据可能非常重要即使该用户已经离开你的公司。

[root@bigboy tmp]# userdel paul
[root@bigboy tmp]# userdel -r paul

怎样让群组知道用户属于哪个群组

以用户名作为 groups 命令的参数:

[root@bigboy root]# groups paul paul : parents
[root@bigboy root]#

怎样更改文件的所有权

你可以用chown命令来更改文件的所有权。第一个对象是该文件 期望的用户名和群组所有权被一个冒号(:)分开,后面是文件名。在下面的例子中,我们改变了名为text.txt的文件的所有权,从root用户和root群组拥有改为testuser用户和users群组拥有:

[root@bigboy tmp]# ll test.txt
-rw-r--r-- 1 root root 0 Nov 17 22:14 test.txt
[root@bigboy tmp]# chown testuser:users test.txt
[root@bigboy tmp]# ll test.txt
-rw-r--r-- 1 testuser users 0 Nov 17 22:14 test.txt
[root@bigboy tmp]#

您也可以使用chown命令后加上 -R 的参数,它能递归搜索目录并改变其权限。

用sudo命令

如果一台计算机需要同时由几个人来管理,最好不要让他们都用root帐号。因为如果每个人都用一样的权限,很难确定是谁什么时间什么位置做了什么。sudo命令就是用来解决这个难题的。


sudo命令允许在/etc/sudoers配置文件中所定义的用户拥有临时的权限来运行一些在正常情况下根据文件访问权限他们不能运行的命令。这些命令可以以root用户或者其他在/etc/sudoers配置文件中所定义的用户所运行。

如果你想运行有特权的命令你必须在这个命令前加上sudo。当运行带有sudo作前缀的命令时,系统在执行该命令之前,会提醒你输入你的密码。在五分钟内你也可以运行其他有特权的命令而不需要输入密码。所有的以sudo执行的命令都会被记录在日志文件/var/log/messages中。

简单的sudo例子

以下是几个运用sudo的简单的实例。

临时获得root用户权限

在这个例子中,用户bob试图浏览文件/etc/sudoers中的目录,而那是需要特权才能访问的。因为没有用sudo,该命令失败:

[bob@bigboy bob]$ more /etc/sudoers
/etc/sudoers: Permission denied
[bob@bigboy bob]$

这一次Bob用了sudo命令并且输入了他自己的密码,他成功了:

[bob@bigboy bob]$ sudo more /etc/sudoers
Password:
...
...
[bob@bigboy bob]$

以後的章节会讲到安装配置sudo命令的细节。

完全以root用户登录

su命令允许一般的用户转变成root用户,只要他知道root用户的密码。而一个用户以sudo权限运行su命令时也可以变成root用户,不过这时他们只需要知道他们自己的密码,而不是root用户的密码,就像你在这里看到的。

someuser@u-bigboy:~$ sudo su -
Password:
root@u-bigboy:~#

一些系统管理员允许用sudo命令来获得root权限操作他们自己的帐户而不需要输入密码。

以後的章节会讲到怎样使sudo su命令失效以及怎样用sudo命令而不需要输入密码。

下载安装sudo命令包

很幸运在RedHat/Fedora中sudo命令包是被默认安装的,也就不需要再安装了。

visudo命令是一个模仿vi编辑器的文字编辑器,vi编辑器是用来编辑配置文件/etc/sudoers的。我们不建议你们其它文字编辑器来修改sudo参数,因为sudoers文件在不同版本的Linux有不有同的目录。visudo用的命令和vi编辑器一样,visudo命令毫无疑问必须以root用户运行:

[root@aqua tmp]# visudo

文件/etc/sudoers

文件/etc/sudoers包含所有的运行sudo需要的配置和许可参数。在用visudo编辑该文件时有很多建议需要遵循。

表9-1所示的是文件/etc/sudodrs的大概格式:

表9-1 文件/etc/sudoers的格式

General sudoers File Record Format

usernames/group servername = (usernames command can be run as) command

几条编辑该文件的建议:

  • Groups are the same as user groups and are differentiated from regular users by a % at the beginning. The Linux user group "users" would be represented by %users.
  • You can have multiple usernames per line separated by commas.
  • Multiple commands also can be separated by commas. Spaces are considered part of the command.
  • The keyword ALL can mean all usernames, groups, commands and servers.
  • If you run out of space on a line, you can end it with a back slash (\) and continue on the next line.
  • sudo assumes that the sudoers file will be used network wide, and therefore offers the option to specify the names of servers which will be using it in the servername position in Table 9-1. In most cases, the file is used by only one server and the keyword ALL suffices for the server name.
  • The NOPASSWD keyword provides access without prompting for your password.


简单的/etc/sudoers例子

本节介绍关于怎样用sudo作用来做一些经常需要的的工作。


特殊用户拥有所有权限

你可以通过添加以下来使用户bob,bunny能够通过sudo命令来获得运行所有命令的权力

bob, bunny ALL=(ALL) ALL


这通常来说不是个好办法,因为这会让用户bob和bunny能通过用sudo su命令而让他们永久地成为root权限从而体现不出使用sudo命令的特点。下面的例子说明了怎样用sudoers文件中的别名来避免这种可能。

让特定的用户获得特定的文件访问权

下面的输入会让用户peter和所有组的管理员获得所有位于/sbin及/usr/sbin目录下的文件访问权,还有运行位于/usr/local/apps/check.pl的命令,注意“/”在指定目录位置时的用法

peter, %operator ALL= /sbin/, /usr/sbin, /usr/local/apps/check.pl

Notice also that the lack of any username entries within parentheses () after the = sign prevents the users from running the commands automatically masquerading as another user. This is explained further in the next example.

同时也要注意等号=后面的括号()之内没有输入任何用户名的话,可以自动阻止用户伪装成其他用户运行命令。这会在下面的示例里更加详细的解释。

Granting Access to Specific Files as Another User

赋予其他用户访问特定文件的权利

The sudo -u entry allows allows you to execute a command as if you were another user, but first you have to be granted this privilege in the sudoers file.

输入sudo -u可以让你变成另一个用户执行命令,但是首先你在sudoers文件中得被允许。

This feature can be convenient for programmers who sometimes need to kill processes related to projects they are working on. For example, programmer peter is on the team developing a financial package that runs a program called monthend as user accounts. From time to time the application fails, requiring "peter" to stop it with the /bin/kill, /usr/bin/kill or /usr/bin/pkill commands but only as user "accounts". The sudoers entry would look like this:

peter ALL=(accounts) /bin/kill, /usr/bin/kill /usr/bin/pkill

User peter is allowed to stop the monthend process with this command:

[peter@bigboy peter]# sudo -u accounts pkill monthend


这种特性对那些有时需要杀死他们手头上的项目相关的进程的程序员来说是很方便的。例如,程序员peter所在的小组正在开发一个用户“accounts”使用的(非管理员使用)金融软件包“Monthend”。这个程序总是一再出错,需要“peter”用只能由用户“accounts”运行的/bill/kill,/usr/bin/kill或/usr/bin/pkill命令来终止这个程序。那么sudoers文件应该应该是以下内容:

peter ALL=(accounts) /bin/kill, /usr/bin/kill /usr/bin/pkill

用户“peter”现在可以用以下命令终结monthend进程:

[peter@bigboy peter]# sudo -u accounts pkill monthend

Granting Access Without Needing Passwords

This example allows all users in the group operator to execute all the commands in the /sbin directory without the need for entering a password. This has the added advantage of being more convenient to the user:

%operator ALL= NOPASSWD: /sbin/

赋予无需密码的访问权

这个例子可以让所所有在operator组中的用户运行所有在/sbin目录下的命令而无需密码。这对用户来说更加方便:

%operator ALL= NOPASSWD: /sbin/

Using Aliases in the sudoers File

Sometimes you'll need to assign random groupings of users from various departments very similar sets of privileges. The sudoers file allows users to be grouped according to function with the group and then being assigned a nickname or alias which is used throughout the rest of the file. Groupings of commands can also be assigned aliases too.

In the next example, users peter, bob and bunny and all the users in the operator group are made part of the user alias ADMINS. All the command shell programs are then assigned to the command alias SHELLS. Users ADMINS are then denied the option of running any SHELLS commands and su:


Cmnd_Alias SHELLS = /usr/bin/sh, /usr/bin/csh, \
 /usr/bin/ksh, /usr/local/bin/tcsh, \
 /usr/bin/rsh, /usr/local/bin/zsh
 
 
User_Alias ADMINS = peter, bob, bunny, %operator
ADMINS ALL = !/usr/bin/su, !SHELLS

User_Alias ADMINS = peter, bob, bunny, %operator ADMINS ALL = !/usr/bin/su, !SHELLS

This attempts to ensure that users don't permanently su to become root, or enter command shells that bypass sudo's command logging. It doesn't prevent them from copying the files to other locations to be run. The advantage of this is that it helps to create an audit trail, but the restrictions can be enforced only as part of the company's overall security policy.

Other Examples

You can view a comprehensive list of /etc/sudoers file options by issuing the command man sudoers.

Using syslog To Track All sudo Commands

All sudo commands are logged in the log file /var/log/messages which can be very helpful in determining how user error may have contributed to a problem. All the sudo log entries have the word sudo in them, so you can easily get a thread of commands used by using the grep command to selectively filter the output accordingly.

Here is sample output from a user bob failing to enter their correct sudo password when issuing a command, immediately followed by the successful execution of the command /bin/more sudoers.

[root@bigboy tmp]# grep sudo /var/log/messages
Nov 18 22:50:30 bigboy sudo(pam_unix)[26812]: authentication failure; logname=bob uid=0 euid=0 tty=pts/0 ruser= rhost= user=bob
Nov 18 22:51:25 bigboy sudo: bob : TTY=pts/0 ; PWD=/etc ; USER=root ; COMMAND=/bin/more sudoers
[root@bigboy tmp]#

总结

知道怎样添加用户非常重要,不仅仅是他们可以登录我们的系统。大部分基于应用软件的服务器都是通过没有特权的用户帐号运行,例如MySQL数据库以用户mysql的身份运行,服务器应用程序Apache Web以用户apache的身份运行。这些帐号一般不会被自动建立,特别是当然这些软件是通过TAR文件安装的时候。
最后,sudo实际上提供了一种分散责任,系统管理多用户化的方法,。你甚至可以根据一些用户在组织中的角色让这些群体的用户拥有局部的访问特权命令,这使得sudo成为任何公司的服务器管理和安全政策中非常有用的一部分。