Linux Box 7: user and rights management

Users and user groups

1 Linux Users

Linux is a multi-user operating system, different users have different permissions, you can view and manipulate different files. Ubuntu has three users:

1 first-time users created; 2 root user; 3 ordinary users;

User rights for the first time create more than the average user, but no more than the root user.

linux users logged in / etc / passwd this document; linux user passwords recorded in the / etc / shadow this document; each user has an ID, called the UID.

// 查看用户记录
ding@ding-ubuntu:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
	......
gdm:x:121:125:Gnome Display Manager:/var/lib/gdm3:/bin/false
ding:x:1000:1000:ding,,,:/home/ding:/bin/bash

2 Linux User Group

For administrative purposes, the user group. So that you can set up a non-person in this group can not access certain files. Each user can belong to a plurality of different groups.

Users and User Group Description:

User : home with you, brother, sister individual, each person has their own room, you three are the user, you can not just shuffle of someone else's room;

User Groups : You are a family of three, that is, belong to the same user group, the three of you can share a kitchen, study space;

therefore:

Users and groups present is to control access to the file, each group of users has an ID, the GID is called, the user group information is stored in / etc / group file. As follows:

ding@ding-ubuntu:/etc$ cat group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
sudo:x:27:ding		// ding 为 sudo 这个用户组的成员
	......
// ding为用户组名,x为密码信息,1000为用户组ID,最后的:后为用户组成员
ding:x:1000:
sambashare:x:126:ding

Can be seen, multiple users can belong to user groups.

3 Create a user group

A graphical user interface installation

Installation tools: sudo apt-get install gnome-system-tools

2 command line to create users and user groups

Add user

添加用户:adduser 用户名
用户查询:finger 用户名
修改用户密码:passwd 用户名
删除用户:deluser 用户名

Adding users and user testing queries:

// 创建用户
ding@ding-ubuntu:~$ sudo adduser ding_u2
[sudo] ding 的密码: 
正在添加用户"ding_u2"...
正在添加新组"ding_u2" (1001)...
正在添加新用户"ding_u2" (1001) 到组"ding_u2"...
创建主目录"/home/ding_u2"...
正在从"/etc/skel"复制文件...
输入新的 UNIX 密码: 
重新输入新的 UNIX 密码: 
passwd:已成功更新密码
正在改变 ding_u2 的用户信息
请输入新值,或直接敲回车键以使用默认值
	全名 []: 
	房间号码 []: 
	工作电话 []: 
	家庭电话 []: 
	其它 []: 
这些信息是否正确? [Y/n] y
// 查询用户信息
ding@ding-ubuntu:~$ finger ding_u2
Login: ding_u2        			Name: 
Directory: /home/ding_u2            	Shell: /bin/bash
Never logged in.
No mail.
No Plan.

Add User Group

添加用户组:addgroup 用户组名
显示组内用户名:groups 用户组名
删除用户组:delgroup 用户组名

Adding user groups and delete a user group test:

// 添加用户组
ding@ding-ubuntu:/home$ sudo addgroup ding_g2
正在添加组"ding_g2" (GID 1001)...
完成。
// 查看 ding 用户组内用户
ding@ding-ubuntu:/home$ groups ding 	// 查看 ding 这个用户组
ding : ding adm cdrom sudo dip plugdev lpadmin sambashare
// 查看刚创建的用户组的用户
ding@ding-ubuntu:/home$ sudo groups ding_g2
groups: "ding_g2": no such user		// 没有用户,因为刚创建完还没添加
// 删除用户组
ding@ding-ubuntu:/home$ sudo delgroup ding_g2
正在删除组 'ding_g2'...
完成。

 

authority management

1 file privilege allows users to different users or groups of users to have a file, the file permissions are divided into three types:

r: Read; w: Write; x: executable;

-rw-r - r-- is the file permissions, the first indicating the file type, and the remaining three each represents a set of permissions. Respectively the owner permission , the owner's group permissions , other permissions .

Binary file permissions can be expressed: r = 4; w = 2; x = 1;

Permissions test.c file:

ding@ding-ubuntu:~$ ls -l test.c 
-rw-r--r-- 1 ding ding 121 2月   8 21:34 test.c

权限说明:
所属用户拥有读写权限无可执行权限;
组内其他用户拥有读权限 无写权限 无可执行权限;
其他用户仅有可读权限;

用户和用户组说明:
第一个 ding 表示所属用户;
第二个 ding 表示所属用户组;

Look at a file system privileges Description:

ding@ding-ubuntu:/dev$ ls -l vcsu
crw-rw---- 1 root tty 7, 64 2月   8 21:26 vcsu

权限说明:
	所属用户为 root 用户,拥有 读写权限 无可执行权限;
	所属用户组为 tty,即 tty 这个组内的所有成员都有 读写权限 无可执行权限;
	其他用户为 ---,对 vcsu 文件无任何权限;

2 Linux file permissions to modify

Modify file permissions command: chmod

Users modify the file belongs: chown

Modify the file permissions test:

ding@ding-ubuntu:~$ ls -l test.c 	// 查看 test.c 文件的权限
-rw-r--r-- 1 ding ding 113 2月   8 23:43 test.c
// 所属用户为ding,用户组为ding
ding@ding-ubuntu:~$ cat test.c 		// 源文件
/* 测试文件 */

#include <stdio.h>

int main(int argc, char argv[])
{
	printf("hello world\n");
	return 0;
}
ding@ding-ubuntu:~$ gcc test.c -o test	// 使用 gcc 编译 test.c
ding@ding-ubuntu:~$ ls -l test*			// 查看 test可执行文件和 test.c 的权限
-rwxr-xr-x 1 ding ding 8296 2月   8 23:49 test		// test文件可执行
-rw-r--r-- 1 ding ding  113 2月   8 23:43 test.c
ding@ding-ubuntu:~$ ./test 				// test 执行结果
hello world
// 修改权限:可读可写,不可执行
ding@ding-ubuntu:~$ chmod 664 test
ding@ding-ubuntu:~$ ls -l test*
-rw-rw-r-- 1 ding ding 8296 2月   8 23:49 test		// 权限改变
-rw-r--r-- 1 ding ding  113 2月   8 23:43 test.c
ding@ding-ubuntu:~$ ./test
bash: ./test: 权限不够					// 当前 test 文件因权限不够无法执行

Modify the file belongs to a user, the command format is as follows:

sudo chown 修改用户:修改组 文件

Modify test files users and groups:

ding@ding-ubuntu:~$ ls -l test		// test属于ding用户,所属组为ding
-rwxrwxr-- 1 ding ding 8296 2月   8 23:49 test
// 用户修改为ding,所属组不变
ding@ding-ubuntu:~$ sudo chown root:ding test	
[sudo] ding 的密码: 
ding@ding-ubuntu:~$ ls -l test
-rwxrwxr-- 1 root ding 8296 2月   8 23:49 test		// 用户成功变为root
ding@ding-ubuntu:~$ sudo chown ding:root test
// 用户变为ding,所属组变为root
ding@ding-ubuntu:~$ ls -l test
-rwxrwxr-- 1 ding root 8296 2月   8 23:49 test		// 权限结果

 

Published 184 original articles · won praise 100 · views 70000 +

Guess you like

Origin blog.csdn.net/dingyc_ee/article/details/104223744