The learning content comes from the linux tutorial of the laboratory building, Mofan python: linux tutorial, CSDN
1. Linux user management
1. View users
who am i 或者who mom likes
who -a: print all printable who -d: print dead processes who -m: same as who am i; who mom likes
who -q: print the current user number and username who -u: print the current user information who -r: print the current running level
2. Create a user
root
The account has the supremacy of the entire system
su, su- and sudo: su <user>
you can switch to the user user; do <cmd>
you can run the cmd command at the privilege level; su - <user>
the command is also a switch user
Create a new user called lilei: sudo adduser lilei, and also create a home directory for the new user.
Switch login user: sudo -l lilei
Exiting the current user is the same as exiting the terminal, you can use exit
commands or use shortcut keys Ctrl+d
3.用户组
在 Linux 里面如何知道自己属于哪些用户组呢?
1.使用groups命令
group shiyanlou==>shiyanlou(用户名):shiyanlou(所属组)
2. 查看/etc/group文件
cat /etc/group |sort (表示输出按字典序排序)
cat /etc/group |grep -E "shiyanlou"==>shiyanlou:x:5000
内容包括用户组(Group)、用户组口令、GID 及该用户组所包含的用户(User),每个用户组一条记录, 'x'表示不可见
Add other users to sudo user group: su -l lilei
usermod
A user group can be added to a user with the command:
sudo usermod -G sudo lilei
groups lilei
==>lilei:lilei sudo
2. Linux file permissions
1. ls -l: View file permissions
Everything is a file in Linux
A directory must have both read and execute permissions to be opened, and a directory must have write permissions to allow other files to be created in it.
- Number of links: The number of file names linked to the inode node where the file is located
- file size: the file size in units of inode node size
Show all hidden files except '.' (current directory), '..' parent directory: ls -a
View the full properties of a directory instead of displaying the file properties in the directory: ls -dl < directory name>
Display all file sizes in a way that normal humans can understand: ls -AsSh
Examples are as follows:
$ ls -l
total 16
----rw-r-- 1 morvan morvan 34 Oct 12 09:51 t1.py -rw----r-- 1 morvan morvan 80 Oct 12 09:57 t2.py -rw-rw-r-- 1 morvan morvan 12 Oct 12 09:56 t3 -rwxrw-r-- 1 morvan morvan 55 Oct 13 17:28 t.py
Here, like -rw-rw-r--
this, is the description of the permissions. The details are shown in the figure below. In the figure below, the string of characters has to be split into 4 parts,
Type
: There are many kinds (the most common ones are-
for files,d
for folders, and othersl
,n
... This kind of thing, if you really encounter it yourself, just search it online, and say too many things at one time that you can't remember).User
: The three spaces that follow are what can be done with the identity of the User (r
read;w
write;x
execute;-
cannot complete an operation).Group
: There may be one or more users in a group, and the style of these permissions is the same as that of User.Others
: Permissions for people other than User and Group.
If you have no idea about User, group, and others, please add it here. User generally refers to you, the person who is using the computer. Group is a collection of Users. When a new User is first created, he is also for this User creates a Group with the same name as User, and this new Group only has this User. Generally speaking, computers in a corporate department can be placed in a Group, and share some shared files and permissions. Others is the exception to the above People other than User and Group mentioned.
Well, with these understandings, let's take the above as an t1.py
example. We can ----rw-r--
split it into -
(this is a file), ---
(this user does not have any permissions), rw-
(this group can read and write), r--
(others can only read ).
If I double click on this, this t1.py
pops up a window saying our permissions.
2. Change file owner
Create a file: $sudo chown shiyanlou ii; change the file belonging to lilei to belong to shiyanlou
3. Modify file permissions
The three sets of permissions for each file (owner, user group, other users, remember that this order is certain) corresponds to this "rwx", which is a '7'
From the above digital permissions can be deduced that "r=4, w=2, x=1" users have the highest permissions that can be written and executed.
r: represents read permission, w: represents write permission, x: represents executable permission
- Degree Permission (r)
- Permission binary 100, converted to decimal 4;
- write permission (w)
- Permission binary 010, converted to decimal 2;
- execute permission (x)
- Permission binary 001, converted to decimal 1;
From the above it can be deduced as follows:
For rwx then 4+2+1=7
For rw- then 4+2+0=6
For rw then 4+0+1=5 For
r- then 4+0+0=4
For -wx Then 0+2+1=3
if you want -w- then 0+2+0=2
if you want - -x then 0+0+1=1
if you want - - - then 0+0+0=0
Note: " - " means no permission
chmod 700 t1.py; Now no one except the owner has permission to the t1.py file.
(2) Addition and subtraction assignment operations
g''o' and 'u' respectively represent group, others, user, '+', '-' respectively represent adding and removing corresponding permissions.
The usual modification is
$ chmod [谁][怎么修改] [哪个文件]
For the simplest example, now t1.py
, ----rw-r--
if we want you (user) to have the ability to read. The following changes will do.
$ chmod u+r t1.py
$ ls -l -r--rw-r-- 1 kumata kumata 34 Apr 12 09:51 t1.py
The image here is u+r
very vivid, User + read, to modify t1.py. So our modified form can be summarized as follows.
[Who]
u
: for User modificationg
: for group modificationo
: Modified for Othersa
: (all) for everyone to modify
[How to modify]
+
,-
,=
: form of action, plus, minus, equal to certain permissionsr
,w
,x
or multiple permissions together, such asrx
[which file]
- The file on which the operation is applied, which can be multiple
Here are a few more examples to consolidate.
-rw----r-- 1 kumata kumata 80 Apr 12 09:57 t2.py
-rw-rw-r-- 1 kumata kumata 12 Apr 12 09:56 t3
-rwxrw-r-- 1 kumata kumata 55 Apr 13 17:28 t.py
$ chmod u-r t2.py $ ls -l t2.py --w----r-- 1 kumata kumata 80 Apr 12 09:57 t2.py $ chmod g+x-w t3 $ ls -l t3 --w-r-xr-- 1 kumata kumata 12 Apr 12 09:56 t3