Home >Operation and Maintenance >Linux Operation and Maintenance >Organize a list of commonly used Linux commands (summary sharing)
This article brings you a basic and sufficient Linux command, which will cover various commands used in the series of blog building articles to facilitate query and learning. I hope it will be helpful to everyone.
File Owner (Owner)
When a user is created, Linux will create a The home directory, the path is /home/
Group
Each user has a user group, which is convenient for assigning permissions to a group of people when multiple people operate. When a user is created, a user group with the same name is automatically created.
If a user belongs to multiple groups at the same time, the user needs to switch between user groups to have the permissions of other user groups.
Others
Users who are neither the file owner nor a member of the group to which the file belongs are Others.
Super User (Root)
Root user is a special type of user who can access all files.
1. adduser to add user and passwd to change password
# 添加一个名为 git 的用户 adduser git # 设置 git 用户的密码 passed git
But because the created user has low permissions, sometimes we need to To elevate privileges, we can do this:
# 会打开 sudoers 配置文件 sudo visudo
Note that you are also editing the sudoers configuration file. Using this command will be safer than using sudo vim /etc/ sudoers. In addition to verifying the syntax, it will also Lock files during multi-user editing.
After opening the sudoers configuration file, we add this line of configuration:
# Allow git to run any commands anywhere git ALL=(ALL:ALL) ALL
Briefly explain this sentence git ALL=(ALL:ALL) ALL:
git represents the rules Applied user name
The first ALL indicates that the rule applies to all hosts
The second ALL indicates that the rule applies to all users
The third ALL indicates that the rule applies to all groups
The fourth ALL indicates that the rule applies to all commands
After we save and exit, the git user will obtain root permissions.
2. ls lists files and directories
ls lists files and directories
[root@iZ2ze learn-typescript.git]# ls branches config description HEAD hooks index info objects refs
ls -la by- a displays all files and directories (including hidden) and -l displays a detailed list:
[root@iZ2ze learn-typescript.git]# ls -la 总用量 20 drwxrwxr-x 7 git git 132 12月 15 12:33 . drwx------ 3 git git 127 12月 15 14:51 .. drwxrwxr-x 2 git git 6 12月 15 12:21 branches -rw-rw-r-- 1 git git 66 12月 15 12:21 config -rw-rw-r-- 1 git git 73 12月 15 12:21 description -rw-rw-r-- 1 git git 23 12月 15 12:21 HEAD drwxrwxr-x 2 git git 4096 12月 15 13:10 hooks -rw-rw-r-- 1 git git 217 12月 15 12:33 index drwxrwxr-x 2 git git 21 12月 15 12:21 info drwxrwxr-x 10 git git 90 12月 15 12:33 objects drwxrwxr-x 4 git git 31 12月 15 12:21 refs
Each line has 7 columns. We use branches as an example to explain the meaning of each column:
Focus on the content of column 1, taking drwxrwxr-x as an example. There are 10 digits in total. The first digit represents the file type, where - represents an ordinary file and d represents a directory file.
The 2nd to 4th digits represent owner permissions, where r represents read permissions, w represents write permissions, x represents executable permissions, - represents no permissions, and the 2nd to 5th digits are rwx, which represents all It can be read, written and executed.
The 5th to 7th digits represent group user permissions, here they are also rwx.
The 8th to 10th bits indicate other user permissions. Here is r-x, which indicates readable and executable permissions but no write permissions.
One more thing to add here:
The default permissions for a root user to create a folder are rwxr-xr-x:
[root@iZ2ze www]# mkdir test [root@iZ2ze www]# ls -l drwxr-xr-x 2 root root 6 12月 17 23:53 test
and the default permissions for a file to be created are rw- r--r--, note that creating a file will remove the x permission by default:
[root@iZ2ze www]# touch index.html [root@iZ2ze www]# ls -l -rw-r--r-- 1 root root 0 12月 17 23:54 index.html
This is why we sometimes need to add execution permissions after creating the file.
3. chown changes the file owner, and you can also change the file group at the same time
chown (change owner) Syntax:
# -R:递归更改文件属组 chown [–R] 属主名 文件名 chown [-R] 属主名:属组名 文件名
Change the owner of index.html to git:
[root@iZ2ze www]# chown git index.html [root@iZ2ze www]# ls - -rw-r--r-- 1 git root 0 12月 17 23:54 index.html
Change the owner and group of index.html to git:
[root@iZ2ze www]# chown git:git index.html [root@iZ2ze www]# ls -l -rw-r--r-- 1 git git 0 12月 17 23:54 index.html
4. chmod changes file permissions
In addition to using r w x, permissions can also be expressed as numbers. The corresponding relationship between arrays and letters is:
r:4
w:2
x:1
All such correspondences are mainly for the convenience of derivation. For example, if we want a file to be readable and writable, then we can easily set the permission to 6 (4 2). Similarly, if we know that a permission is 3, we can also deduce The exit permission is writable and executable, because only 2 1 can be equal to 3.
Let’s take a look at the specific syntax of chmod (change mode):
# -R:递归更改文件属组 chmod [-R] xyz 文件或目录
xyz represents the permissions of Owner, Group, and Others respectively. If we set the permissions of a file like this:
chomd 750 index.html
We can know that the Owner's permission is 7, which means it can read, write and execute, the Group's permission is 5, which means it can read and execute, and the Others' permission is 0, which means it cannot read, write and execute. The corresponding letters are: rwxr-x---.
In addition to this numerical method, there is also a way to change permissions using symbolic types:
在这种方式里,我们将三种身份 Owner、Group、Others,分别简写为 u(User)、g、o,用 a 表示所有身份,再使用 + - = 表示加入、去除、设定一个权限,r w x 则继续表示读,写,执行权限,举个例子:
chomd u+x,g-x,o-x index.html
意思就是 Owner 加上执行权限,Group 和 Others 去除执行权限。
当然我们也可以直接设定权限
chmod u=rwx,g=rx,o=r index.html
此时文件的权限就相当于 -rwxr-xr--。
此外,我们还可以省略不写 ugoa 这类身份内容,直接写:
chmod +x index.html
此时相当于使用了 a,会给所有身份添加执行权限。
5. su 切换身份
# 切换为 git 用户 su git
6. whoami 显示用户名
# whoami root
7. pwd 显示当前目录
[git@iZ2ze www]$ pwd /home/www
8. cd 切换工作目录
# 进入 /home/www/ cd /home/www # 进入自己的主目录 cd ~ # 进入当前目录的上上两层 : cd ../..
10. mkdir 创建目录
mkdir 创建目录:
mkdir new_folder
mkdir -p 递归创建目录:
mkdir -p one/two/three
11. touch 创建文件
用于修改文件或者目录的时间属性,当文件不存在,系统会创建空白文件
touch new_file
12. echo 打印输出
echo 是 Shell 命令,用于打印输出:
# 显示转义字符 echo "\"test content\""
创建或覆盖文件内容为 "test content":
echo "test content" > index.html
如果是想追加内容,就用 >> :
[root@iZ2ze www]# echo "test content" > index.html [root@iZ2ze www]# cat index.html test content [root@iZ2ze www]# echo "test content" >> index.html [root@iZ2ze www]# cat index.html test content test content
13. cat 连接文件并打印输出
查看文件内容:
cat ~/.ssh/id_rsa.pub
清空 index.html 内容:
cat /dev/null > index.html
把 index.html 的内容写入 second.html:
cat index.html > second.html
把 index.html 的内容追加写入 second.html:
cat index.html >> second.html
把 index.html 和 second.html 追加写入 third.html:
cat index.html second.html >> third.html
14. cp 复制文件或目录
将目录 website/ 下的所有文件复制到新目录 static 下:
# -r:若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件。 cp –r website/ static
15. mv 移动并重命名
文件改名:
mv index.html index2.html
隐藏文件:
# 文件名上加上 . mv index.html .index.html
移动文件:
# 仅仅移动 mv /home/www/index.html /home/static/ # 移动又重命名 mv /home/www/index.html /home/static/index2.html
批量移动:
mv /home/www/website/* /home/www/static
16. rm 删除一个文件或者目录
# 系统会询问 rm file # -f 表示直接删除 # -r 表示目录下的所有文件删除 # 删除当前目录下的所有文件及目录 rm -r * # 跑路 rm -rf /*
17. vi/vim
Linux 内建 vi 文书编辑器,Vim 是从 vi 发展出来的一个文本编辑器。
基本上 vi/vim 共分为三种模式,分别是命令模式(Command mode),输入模式(Insert mode)和底线命令模式(Last line mode)。我们边操作边介绍这三种模式:
我们执行 vim index.html,如果没有该文件,则会创建文件:
vim index.html
此时是命令模式,在命令模式下,输入的任何字符都会被视为命令,接下来几个常用的命令:
i 切换到输入模式。
x 删除当前光标所在处的字符。
: 切换到底线命令模式。
我们按下 i,便会进入输入模式
输入模式下,左下角有 -- INSERT -- 标志:
此时我们可以进行各种输入,当输入完毕后,按下 ESC 回到命令模式
此时左下角的 INSERT已经消失不见了,如果我们要保存退出,我们先输入 : ,进入底线命令模式
在底线命令模式中,常见的命令有
w 保存文件
q 退出程序
我们输入 wq,表示保存并退出,此时我们就会发现并创建了一个 HTML 文件。
18. ssh 远程连接工具
注意 ssh 监听是 22 端口。
其基本语法为:
ssh [OPTIONS] [-p PORT] [USER@]HOSTNAME [COMMAND]
监听端口示例:
ssh -p 300 git@8.8.8.8
打开调试模式:
# -v 冗详模式,打印关于运行情况的调试信息 ssh -v git@8.8.8.8
相关推荐:《Linux视频教程》
The above is the detailed content of Organize a list of commonly used Linux commands (summary sharing). For more information, please follow other related articles on the PHP Chinese website!