Printing and writing to files
echo
is usually used for printing message in the terminal, much like the printing function in programming languages. echo
can also be used to write a string into a file if it is used together with a redirection operator. For example:
1 |
|
Redirect output of a program to a file
1 |
|
However, by doing the above no message will be output to the terminal (stdout
).
Redirect output of a program both to a file and stdout
2>&1
dumps the stderr
and stdout
streams. tee logs.txt
takes the stream it gets and writes it to the screen and to the file logs.txt
.
1 |
|
Users and groups in linux
确定你在Linux或类Unix系统中的角色(即你当前的用户身份和用户组)
使用 whoami
命令可以显示你当前的用户名
ID command
使用 id
命令可以查看你的用户ID、主组ID以及附加组信息。uid是用户ID。gid是主组ID。groups显示你所属的所有组,包括主组和附加组。
The id
command prints information about the specified user and its groups.
If the username is omitted it shows information for the current user.
1 |
|
To get information about a specific user
1 |
|
Groups
In order to list all groups in Linux
1 |
|
Like /etc/passwd
, there is one entry per line, all fields are separated by a colon :
. The first field is the name of group group_name
. If you run ls -l
command, you will see this name printed in the group field. The second field is the password, generally password is not used, hence it is empty/blank. It can store encrypted password. This is useful to implement privileged groups. The third field is the Group ID (GID), each user must be assigned a group ID. You can see this number in your /etc/passwd
file. The fourth field is the group List, it is a list of user names of users who are members of the group. The user names, must be separated by commas.
可以使用 groups
命令查看 你所属的所有组。
In order to list all groups the current user belongs to (the first group is the primary group)
1 |
|
To get a list of all groups a specific user belongs to, provide the username to the groups command as an argument
1 |
|
Users
In order to list all users in Linux
1 |
|
Each line corresponds to a user, for example,
1 |
|
where mike
is the user name or login name. x
is the encrypted password is stored in the /etc/shadow
file. 1242
is the UID (user ID number). 1308
is the primary GID (group ID number). Then it is GECOS. It may includes user’s full name (or application name, if the account is for a program), building and room number or contact person, office telephone number, home telephone number and any other contact information. /home/mike
is the home directory for the user. /bin/bash
is the login shell for the user. Pathnames of valid login shells comes from the /etc/shells
file.
Adding a new user
Example using adduser
1 |
|
-
--disabled-password
login only possible using SSH RSA keys -
--gecos
sets the gecos field for the new entry generated. -
--shell
sets the user’s login shell, rather than the default specified by the configuration file.
File permission
In Linux, access to the files is controlled by the operating system using file permissions, attributes, and ownership. Each file is owned by a particular user and a group and assigned with permission access rights for three different classes of users:
- The file owner
- The group members
- Others (everybody else)
There are three file permission types that apply to each user class. You can specify which users are allowed to read the file, write to the file, or execute the file. The same permission attributes apply for both files and directories with a different meaning:
-
The read permission The file is readable. For example, when the read permission is set, the user can open the file in a text editor. The directory’s contents can be viewed. The user can list files inside the directory with the
ls
command. - The write permission The file can be changed or modified. The directory’s contents can be altered. The user can create new files , delete existing files , move files , rename files ..etc.
-
The execute permission The file can be executed. The directory can be entered using the
cd
command.
File permissions can be viewed using the ls -l
command. For example:
1 |
|
1 |
|
The first character shows the file type. It can be a regular file -
, directory d
, a symbolic link l
, or any other special type of file. The next nine characters represent the file permissions, three triplets of three characters each. The first triplet shows the owner permissions, the second one group permissions, and the last triplet shows everybody else permissions.
File permission can be represented in a numeric or symbolic format. The permission number can consist of three or four digits, ranging from 0 to 7. When 3-digit number is used, the first digit represents the permissions of the file’s owner, the second one the file’s group and the last one all other users.
- r (read) = 4
- w (write) = 2
- x (execute) = 1
- no permissions = 0
The permission digit of a specific user class is the sum of the values of the permissions for that class. Each digit of the permission number may be a sum of 4, 2, 1 and 0:
- 0 (0+0+0) – No permission
- 1 (0+0+1) – Only execute permission
- 2 (0+2+0) – Only write permission
- 3 (0+2+1) – Write and execute permissions
- 4 (4+0+0) – Only read permission
- 5 (4+0+1) – Read and execute permission
- 6 (4+2+0) – Read and write permissions
- 7 (4+2+1) – Read, write, and execute permission
When a 4-digit number is used, the first digit is used for something else. We do not cover it in this memo.
chmod
allows changing the file permission.
1 |
|
Ownership
chown
changes the user and/or group ownership of each given file.
- If only an owner (a user name or numeric user ID) is given, that user is made the owner of each given file, and the files’ group is not changed.
- If the owner is followed by a colon and a group name (or numeric group ID), with no spaces between them, the group ownership of the files is changed as well.
- If a colon but no group name follows the user name, that user is made the owner of the files and the group of the files is changed to that user’s login group.
- If the colon and group are given, but the owner is omitted, only the group of the files is changed; in this case, chown performs the same function as
chgrp
. - If only a colon is given, or if the entire operand is empty, neither the owner nor the group is changed.
1 |
|
1 |
|
To change the group ownership of a file/directory Target
to the group GroupA
1 |
|