命令行星期二——第七部分
命令行 命令行星期二
嗨，极客们，我已经检查了我们的课程，今天我们在八章课程里的第七部分。这就是说下周二我们还有一次课程，但也是最后一次“命令行星期二”课程。所以，今天，我们讨论一些关于**权限**的问题！


![](/data/attachment/album/201408/11/235128hqheadegvigp1h11.png)


众所周知，一台机器可能会有多名用户共同使用。为了保证用户之间互不干扰，就需要分配不同的权限。我们已经讨论了文件权限的问题，你可以轻轻[点此](1)回顾一下：


![](/data/attachment/album/201408/11/235130y4t71nm7nm1o48pm.png)


### chmod


**chmod**命令被用来修改目录或者文件的权限。为了修改权限，首先你得输入chmod命令，之后你得输入规范的权限修改，然后你就可以把目录或者文件的权限修改成你想要的。这可以采用多种方式完成，但是肖茨先生喜欢八进制表示法。


把权限想象成一系列的位，每一个不为空的权限槽就是1，空的权限槽就是0。举个例子：



```
rwx = 111
rw- = 110

```

等等


看一下二进制表示法：



```
rwx = 111 --> in binary = 7
rw- = 110 --> in binary = 6
r-x = 101 --> in binary = 5
r-- = 100 --> in binary = 4

```

现在，如果你想把一个文件的权限改为：文件所有者和文件所有组拥有读，写，执行权限，但是其他用户没有该文件任何权限，我们可以这样做：



```
chmod 770 example_file

```

### su 和 sudo


有些时候普通用户需要成为超级用户，这样才能完成一些任务（通常是一些安装软件任务）。为了暂时的获取超级用户权限，我们可以使用**su**程序，输入：



```
su

```

输入你的超级用户密码，你就成为超级用户了。但是，警告：别忘记用一会之后（不需要的话）就退出超级用户模式。


在openSUSE和Ubuntu也其它的一些可选工具，它叫做sudo，sudo只是在某些方面和su不同，它是分配给制定用户的特殊命令，不像su，你可以用你自己用户的密码执行sudo，而不需要超级用户密码，举个例子：



```
sudo zypper in goodiegoodie

```

### 修改文件和组所有权


想要改变文件的所有者，你可以在超级用户模式下运行**chown**，举个例子，如果我想把文件的所有权从‘nenad’改为‘suse’，我可以这样做：



```
su
[输入密码]
chown suse example_file

```

同样地，我也可以改变组的所有权，使用稍有不同的**chgrp**命令，很简单：



```
chgrp suse_group example_file

```

### 下一次


正如我所阐述的，下一次我们就会结束这个系列。在此之后，相信你会做出自己的选择，和我一起徜徉在终端的黑暗之中，只有一个绿色的闪烁光标作为灯塔。在那之前，极客们，记住


....享受更多的乐趣！




---


via: <https://news.opensuse.org/2014/08/05/command-line-tuesdays-part-seven/>


作者：[Nenad Latinović](https://news.opensuse.org/author/holden87/) 译者：[su-kaiyao](https://github.com/su-kaiyao) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译，[Linux中国](http://linux.cn/) 荣誉推出


OK
# Command Line Tuesdays - Part Seven

5. Aug 2014 | Nenad Latinović | No License

Heya geekos. I’ve checked the ‘curriculum’, and we’re at part 7 of 8 as of today. Which means there will be one more - and sadly final - CLT next tuesday. So for today, let’s deal with some **permissions!**

As we all know, we can have many users using one machine. To protect the users from each other, permissions have been devised. And we have already discussed file permissions, so let’s refresh our memories with a single[ click](https://news.opensuse.org/2014/07/01/command-line-tuesdays-part-three/).

##### chmod

The** chmod** command is used for changing permissions on a directory or a file. To use it, you first type the chmod command, after that you type the permissions specification, and after that the file or directory you’d like to change the permissions of. It can be done in more way, but mr Shotts focuses on the *octal* _notation _method.

Imagine permissions as a series of bits. For every permission slot that’s not empty, there’s a 1, and for every empty one there’s a 0. For example:

rwx = 111 rw- = 110 etc.

And to see how it looks in binary:

rwx = 111 ---> in binary = 7 rw- = 110 ---> in binary = 6 r-x = 101 ---> in binary = 5 r-- = 100 ---> in binary = 4

Now, if we would like to have a file with read, write and executing permissions for the file owner and for the group owner of the file, but make it unavailable to all other users, we do:

chmod 770 example_file

…where example_file is any file you’d like to try this command on. So, you always have to enter three separate digits, for three separate groups known already from our second lesson. The same can be done for directories.

##### su and sudo

It is sometimes needed for a user to become a super user, so he can accomplish a task (usually something like installing software, for example). For temporary accessing to the super user mode, there’s a program called** su**, or substitute user. You just have to type in

su

and type your superuser password, and you’re in. However, a word of warning: don’t remember to log out and use it for a short period of time.

Also there’s an option probably more used in openSUSE and Ubuntu, and it’s called **sudo**. Sudo is only different in the aspect, that it’s a special command that’s allocated to one specific user. So unlike su, with sudo you can use your user password instead of the superuser’s password. Example:

sudo zypper in goodiegoodie

##### Changing file and group ownership

To change the owner of the file, you have to run** chown** as a superuser. For example, if I’d want to change ownership from ‘nenad’ to ‘suse’, I do it this way:

su [enter password] chown suse example_file

I can also accomplish the same with changing group ownership, but with a slightly different command **chgrp**. Easy peasy:

chgrp suse_group example_file

…and that’s it.

##### Next time

As I already stated, next time we’ll have a photo-finish of sorts. And after that, you’ll be on your own (along with me, wandering through the terminal’s darkness, with only a blinking green cursor as a lighthouse :) ). Until then geekos, remember to…

##### …have a lot of fun!

**Categories:**
[
Distribution
](/category/distribution)

**Tags:**