本节主要内容

  1. 理解用户和组的概念
  2. 用户管理
  3. 组管理
  4. 权限分配

1. 理解用户和组的概念

在第一讲中我们提到。linux是一种多任务、多用户的操作系统,在讲ls -l命令行我们看到例如以下文件具体信息:

  1. root@ubuntu:/home/xtwy# ls -l
  2. total 48
  3. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
  4. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
  5. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
  6. -rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
  7. -rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
  8. -rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
  9. drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
  10. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
  11. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
  12. drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
  13. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
  14. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos

这当中便涉及到用户和组的权限问题。在讲这部分内容之前呢,先来理解一下用户和组的概念

在Linux中,用户是能够获取系统资源的权限的集合,组是权限的容器。

Linux用户类型

部分内容来源于:http://www.2cto.com/os/201404/293172.html

用户类型 描写叙述
管理员 root 具有使用系统全部权限的用户,其UID 为0
普通用户 即一般用户,其使用系统的权限受限,其UID为500-60000之间.
系统用户 保障系统运行的用户,一般不提供password登录系统,其UID为1-499之间

与Linux用户信息相关的文件有两个,各自是/etc/passwd。 /etc/shadow

/etc/passwd文件内容例如以下:

  1. root@ubuntu:/home/xtwy# more /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. bin:x:2:2:bin:/bin:/bin/sh
  4. ........................
  5. xtwy:x:1000:1000:Ubuntu-10.04,,,:/home/xtwy:/bin/bash

/etc/passwd文件内容格式为:

  1. accountpasswordUID:GID:GECOS:diretory:shell

account: 用户名或帐号

password :用户password占位符

UID:用户的ID号

GID:用户所在组的ID号

GECOS:用户的具体信息(如姓名,年龄,电话等)

diretory:用户所的主文件夹

shell:用户所在的编程环境

/etc/shadow文件内容格式为:

  1. root:$1$.TZS2yur$uQ3.5XLbdEhLkak9HKqZx/:16042:0:99999:7:空白:空白:空白
  2. 1 2 3 4 5 6 7 8 9
  3. | | | | | | | | |--保留字段,眼下为空
  4. | | | | | | | |--用户过期日期(单位/天)。此字段指定了用户作废的天数(从1970年的11日至今天数)
  5. | | | | | | |--在口令过期之后多少天禁用此用户
  6. | | | | | |--提前多少天警告用户口令将过期
  7. | | | | |--两次改动口令间隔最多的天数
  8. | | | |--两次改动口令间隔最少的天数
  9. | | |--上次改动password的时间(单位/天),自19700101日至今天数
  10. | |--用户password(相应/etc/passwd文件内的password占位符),假设password加入“!!”表示禁用该用户
  11. |--用户名(相应/etc/passwd文件内的用户名)
  12. 注:当中字段45678的值为空时。账号可永久使用

Linux用户组类型

用户组类型 描写叙述
普通用户组 能够加入多个用户
系统组 一般加入一些系统用户
私有组(也称基本组) 当创建用户时,假设没有为其指明所属组,则就为其定义一个私有的用户组,起名称与用户名同名,当把其他用户加入到该组中,则其就变成了普通组

前面我提到,组是权限的容器,如普通用户 user1,user2, user3所属组group,则它们会继承组group的权限,与group相关的文件包含/etc/group /etc/gshadow

以下给出的是/etc/group文件内容及格式含义

  1. root:x:0:root
  2. | | | |--额外组(能够多个用“,”隔开)
  3. | | |--组IDGID
  4. | |--组password占位符
  5. |--组名

以下给出的是/etc/gshadow文件内容及格式含义

  1. root:空白:空白:root
  2. | | | |--以逗号分隔的小组成员
  3. | | |--以逗号分隔的组管理员
  4. | |--加密的password
  5. |--组名

2. 用户管理

Linux中的用户管理主要涉及到用户账号的加入、删除和改动。全部操作都影响/etc/passwd中的文件内容

(1)加入用户 useradd

  1. useradd [options] username
  2. options
  3. 1.-u UID
  4. 2.-g GID
  5. 3.-d :指定用户家文件夹。默认是/home/username
  6. 4.-s :指定用户所在的shell环境
  7. 5.-G:指定用户的附加组
  8. 样例:
  9. root@ubuntu:/home/xtwy# useradd -u 1988 -g 1000 john
  10. root@ubuntu:/home/xtwy# tail -1 /etc/passwd
  11. john:x:1988:1000::/home/john:/bin/sh
  12. 加入完毕后能够採用下列命令进行用户johnpassword的改动
  13. xtwy@ubuntu:~$ sudo passwd john
  14. [sudo] password for xtwy:
  15. Enter new UNIX password:
  16. Retype new UNIX password:
  17. passwd: password updated successfully

注销用户,又一次到登录界面时,便会出现john用户的登录选项

(2)改动用户 usermod

  1. usermod [options] username
  2. options
  3. 1.-u UID
  4. 2.-g GID
  5. 3.-d :指定用户家文件夹,默认是/home/username
  6. -m 与-b 一起用表示把用户家文件夹的内容也移走
  7. 4.-s :指定用户所在的shell环境
  8. 5.-G:指定用户的附加组
  9. 使用演示样例:
  10. //将前面的/bin/sh。改/bin/bash
  11. root@ubuntu:/home/john# usermod -s /bin/bash john

(3)删除用户 userdel

删除用户时,用户不能处于登录状态:

  1. userdel [options]username
  2. options
  3. 1.-r :连同主文件夹一起删除
  4. 使用演示样例:
  5. root@ubuntu:/home/xtwy# userdel -r john
  6. root@ubuntu:/home/xtwy# cd ..
  7. root@ubuntu:/home# ls

3. 组管理

前面我们知道,组是权限的集合。在linux系统中。每一个用户都有一个用户组,没有指定时都默觉得私有组,私有组名同用户名一致,建立用户组的优点是系统能对一个用户组中的全部用户的操作权限进行集中管理。

组管理涉及组的加入、删除和改动。组的添加、删除和改动实际上就对/etc/group文件的更新。

(1)新增用户组

  1. groupadd [options] 用户组名
  2. options
  3. 1 -g GID:指定新用户组的组标识号(GID)。
  4. 使用演示样例:
  5. //创建用户组前的/etc/group文件内容
  6. oot@ubuntu:/home# tail /etc/group
  7. .....................
  8. xtwy:x:1000:
  9. sambashare:x:122:xtwy
  10. //使用默认添加用户组
  11. root@ubuntu:/home# groupadd john
  12. //加入用户组后得到的/etc/group文件内容
  13. //能够看到默认用户组gid是自增长的
  14. root@ubuntu:/home# tail /etc/group
  15. .................
  16. xtwy:x:1000:
  17. sambashare:x:122:xtwy
  18. john:x:1001:
  19. //指定gid
  20. root@ubuntu:/home# groupadd -g 1011 john1
  21. root@ubuntu:/home# tail /etc/group
  22. ................
  23. john:x:1001:
  24. john1:x:1011:

(2)改动用户组

  1. groupmod [options] 用户组名
  2. options
  3. 1 -g GID: 为用户组指定新的组标识号。
  4. 2 -n : 将用户组的名字改为新名字
  5. 使用演示样例:
  6. root@ubuntu:/home# groupmod -g 1002 -n john2 john1
  7. root@ubuntu:/home# tail /etc/group
  8. .....................
  9. john:x:1001:
  10. john2:x:1002:

(3)删除用户组

  1. groupdel 用户组名
  2. 使用演示样例:
  3. root@ubuntu:/home# groupdel john2
  4. root@ubuntu:/home# tail /etc/group
  5. ....................
  6. xtwy:x:1000:
  7. sambashare:x:122:xtwy
  8. john:x:1001:

4. 权限分配 ##

(1)权限说明

在本节第一节。我们提到ls -l命令显示的文件或文件夹具体信息具有例如以下格式

  1. root@ubuntu:/home/xtwy# ls -l
  2. total 48
  3. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
  4. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
  5. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
  6. -rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
  7. -rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
  8. -rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
  9. drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature

如今我们对ls -l显示的内容进行分解,首先来看前半部分



首先是文件类型,-表示文本文件,d表示文件夹,除此之外还有下列几种文件(不常见):

code File Type
- Standard file
d Standard directory
l Symbolic link (a shortcut to another file)
s Socket (a file designed to send and receive data over a network)
c Character device (a hardware device driver, usually found in /dev)
b Block device (a hardware device driver, usually found in /dev)

后面紧跟着的是用户权限、组权限及其他权限,当中r表示读权限,w表示写权限,x表示可运行权限,

再后面的数字表示的是链接数,这个放到下一节中介绍

紧接着是文件或文件夹的所属者,所属用户组,文件大小(字节数),文件最后訪问时间。文件名称。

以下具体说明:

  1. //hello1.txt是一个普通文件
  2. //root用户具有读写权限。但不能运行
  3. //用户组root具有读权限,无写和运行权限
  4. //其他用户仅仅有读权限。无写和运行权限
  5. -rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
  6. //以下的代码演示了前面的内容
  7. xtwy@ubuntu:~$ ls
  8. Desktop Downloads hello1.txt literature Pictures Templates
  9. Documents examples.desktop hello.txt Music Public Videos
  10. xtwy@ubuntu:~$ more hello1.txt
  11. hello linux
  12. hello linux linux
  13. xtwy@ubuntu:~$ echo "test permission" >> hello1.txt
  14. bash: hello1.txt: Permission denied
  1. //Desktop是一个文件夹
  2. //用户xtwy具有读写和运行权限,这里面的运行权限x表示能够訪问文件夹
  3. //用用组xtwy具有读和运行权限。无写权限
  4. //其他用户具有读和运行权限,无写权限
  5. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop

(2)改动文件或文件夹权限

1 添加权限

  1. //chmod命令,a表示全部。包含用户、组及其他用户都有添加写权限
  2. root@ubuntu:/home/xtwy# chmod a+w hello1.txt
  3. root@ubuntu:/home/xtwy# ls -l
  4. total 48
  5. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
  6. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
  7. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
  8. -rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
  9. -rw-rw-rw- 1 root root 30 2015-08-22 17:28 hello1.txt
  10. -rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
  11. drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
  12. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
  13. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
  14. drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
  15. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
  16. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos

2 减小权限

  1. //减小权限,用减号表示
  2. root@ubuntu:/home/xtwy# chmod a-w hello1.txt
  3. root@ubuntu:/home/xtwy# ls -l
  4. total 48
  5. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
  6. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
  7. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
  8. -rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
  9. -r--r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
  10. -rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
  11. drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
  12. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
  13. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
  14. drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
  15. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
  16. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos

3 给当前用户添加权限

  1. //不加all表示作用于当前用户
  2. root@ubuntu:/home/xtwy# chmod +w hello1.txt
  3. root@ubuntu:/home/xtwy# ls -l
  4. total 48
  5. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
  6. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
  7. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
  8. -rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
  9. -rw-r--r-- 1 root root 30 2015-08-22 17:28 hello1.txt
  10. -rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
  11. drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
  12. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
  13. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
  14. drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
  15. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
  16. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos

4 灵活设置权限,採用数字方式

读、写、运行三项权限能够用数字表示。就是r=4,w=2,x=1

  1. //用户具有读写权限
  2. //用户组和其他用户具有运行权限。无读写权限
  3. root@ubuntu:/home/xtwy# chmod 611 hello1.txt
  4. root@ubuntu:/home/xtwy# ls -l
  5. total 48
  6. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Desktop
  7. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Documents
  8. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Downloads
  9. -rw-r--r-- 1 xtwy xtwy 179 2015-08-20 21:53 examples.desktop
  10. -rw---x--x 1 root root 30 2015-08-22 17:28 hello1.txt
  11. -rw-r--r-- 1 root root 48 2015-08-22 17:29 hello.txt
  12. drwxr-xr-x 3 root root 4096 2015-08-22 16:51 literature
  13. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Music
  14. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Pictures
  15. drwxr-xr-x 3 xtwy xtwy 4096 2015-08-22 15:52 Public
  16. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Templates
  17. drwxr-xr-x 2 xtwy xtwy 4096 2015-08-20 23:31 Videos

5 改变用户 chown

将root拥有改xtwy用户拥有

  1. -rw---x--x 1 root root 30 2015-08-22 17:28 hello1.txt
  2. root@ubuntu:/home/xtwy# chown xtwy hello1.txt
  3. -rw---x--x 1 xtwy root 30 2015-08-22 17:28 hello1.txt
  4. //改动完毕后能够对文件里的内容进行改动
  5. root@ubuntu:/home/xtwy# su xtwy
  6. xtwy@ubuntu:~$ echo "test permissions" >> hello1.txt

6 改变用户组 chgrp

  1. -rw---x--x 1 xtwy root 30 2015-08-22 17:28 hello1.txt
  2. xtwy@ubuntu:~$ chgrp xtwy hello1.txt
  3. -rw---x--x 1 xtwy xtwy 47 2015-08-22 23:43 hello1.txt

加入公众微信号,能够了解很多其他最新Spark、Scala相关技术资讯

Spark修炼之道(基础篇)——Linux大数据开发基础:第三节:用户和组的更多相关文章

  1. Spark修炼之道(基础篇)——Linux大数据开发基础:第二节:Linux文件系统、文件夹(一)

    本节主要内容 怎样获取帮助文档 Linux文件系统简单介绍 文件夹操作 訪问权限 1. 怎样获取帮助文档 在实际工作过程其中,常常会忘记命令的使用方式.比如ls命令后面能够跟哪些參数,此时能够使用ma ...

  2. Spark修炼之道——Spark学习路线、课程大纲

    课程内容 Spark修炼之道(基础篇)--Linux基础(15讲).Akka分布式编程(8讲) Spark修炼之道(进阶篇)--Spark入门到精通(30讲) Spark修炼之道(实战篇)--Spar ...

  3. 大数据开发-Spark-拷问灵魂的5个问题

    1.Spark计算依赖内存,如果目前只有10g内存,但是需要将500G的文件排序并输出,需要如何操作? ①.把磁盘上的500G数据分割为100块(chunks),每份5GB.(注意,要留一些系统空间! ...

  4. 高手养成计划基础篇-Linux第二季

    高手养成计划基础篇-Linux第二季   本文来源:i春秋社区-分享你的技术,为安全加点温度   前言 前面我们学习了文件处理命令和文件搜索命令,简单的了解了一下Linux,但是仅仅了解这样还不行,遇 ...

  5. Linux 网络协议栈开发基础篇—— 网桥br0

    一.桥接的概念 简单来说,桥接就是把一台机器上的若干个网络接口"连接"起来.其结果是,其中一个网口收到的报文会被复制给其他网口并发送出去.以使得网口之间的报文能够互相转发. 交换机 ...

  6. 一篇了解大数据架构及Hadoop生态圈

    一篇了解大数据架构及Hadoop生态圈 阅读建议,有一定基础的阅读顺序为1,2,3,4节,没有基础的阅读顺序为2,3,4,1节. 第一节 集群规划 大数据集群规划(以CDH集群为例),参考链接: ht ...

  7. 大数据入门基础系列之Hadoop1.X、Hadoop2.X和Hadoop3.X的多维度区别详解(博主推荐)

    不多说,直接上干货! 在前面的博文里,我已经介绍了 大数据入门基础系列之Linux操作系统简介与选择 大数据入门基础系列之虚拟机的下载.安装详解 大数据入门基础系列之Linux的安装详解 大数据入门基 ...

  8. 大数据开发实战:Spark Streaming流计算开发

    1.背景介绍 Storm以及离线数据平台的MapReduce和Hive构成了Hadoop生态对实时和离线数据处理的一套完整处理解决方案.除了此套解决方案之外,还有一种非常流行的而且完整的离线和 实时数 ...

  9. Spark 介绍(基于内存计算的大数据并行计算框架)

    Spark 介绍(基于内存计算的大数据并行计算框架)  Hadoop与Spark 行业广泛使用Hadoop来分析他们的数据集.原因是Hadoop框架基于一个简单的编程模型(MapReduce),它支持 ...

随机推荐

  1. eclipse中的乱码问题

    在新建项目或导入工程时常常遇到的问题: 1.导入工程后,Java文件中文乱码 项目右键--Properties--Resource(快捷键Alt+Enter),在Text file encoding中 ...

  2. WebService学习总结(5)——WebService常见开发框架比较

    在SOA领域,我们认为Web Service是SOA体系的构建单元(building block).对于服务开发人员来说,AXIS和CXF一定都不会陌生.这两个产品都是Apache孵化器下面的Web ...

  3. 机房收费 & 廊院食堂

    做机房收费系统时.常常想这个一般用户指的是谁?我当初以为是学生......可能是被数据库中的student带跑偏了...... 事实上把我们的系统联系一下实际,就会非常easy想到一般用户指的是谁的位 ...

  4. java中volatile关键字的含义--volatile并不能做到线程安全

    在Java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言是支持多线程的,为了解决线程并发的问题,在语 ...

  5. 【Educational Codeforces Round 31 B】Japanese Crosswords Strike Back

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 所有数字的和加上n-1,如果为x则唯一,否则不唯一 [代码] #include <bits/stdc++.h> usin ...

  6. [Angular] AuthService and AngularFire integration

    Config AngularFire, we need database and auth module from firebase. import {NgModule} from '@angular ...

  7. Oracle批量插入在C#中的应用

    public void SetUserReportResult(int[] reportId, bool isReceive, string result) { if (reportId == nul ...

  8. mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure

    err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...

  9. ES权威指南1

    Elasticsearch学习笔记 一 本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws. 本文参考和学习资料 <ES权威指南> ...

  10. eclipse插件安装验证及问题处理

    eclipse插件安装验异常时可看当前workspace下面的.metadata/.log文件,找到具体的问题来处理.一般常用到插件安装不成功的原因如下: 1.jar包冲突: 2.jar包依赖的jav ...