Linux教程
blog 2019-09-02
Linux
# Linux教学
# Linux 指令教学
| 命令 | 说明 | 语法 | 参数 | 参数说明 |
|---|---|---|---|---|
| ls | 显示文件和目录列表 | ls [-alrtAFR] [name...] | ||
| -l | 列出文件的详细信息 | |||
| -a | 列出当前目录所有文件,包含隐藏文件 | |||
| mkdir | 创建目录 | mkdir [-p] dirName | ||
| -p | 父目录不存在情况下先生成父目录 | |||
| cd | 切换目录 | cd [dirName] | ||
| touch | 生成一个空文件 | |||
| echo | 生成一个带内容文件 | echo abcd > 1.txt,echo 1234 >> 1.txt | ||
| cat | 显示文本文件内容 | cat [-AbeEnstTuv] [--help] [--version] fileName | ||
| cp | 复制文件或目录 | cp [options] source dest | ||
| rm | 删除文件 | rm [options] name... | ||
| -f | 强制删除文件或目录 | |||
| -r | 同时删除该目录下的所有文件 | |||
| mv | 移动文件或目录 | mv [options] source dest | ||
| find | 在文件系统中查找指定的文件 | |||
| -name | 文件名 | |||
| grep | 在指定的文本文件中查找指定的字符串 | |||
| tree | 用于以树状图列出目录的内容 | |||
| pwd | 显示当前工作目录 | |||
| ln | 建立软链接 | |||
| more | 分页显示文本文件内容 | |||
| head | 显示文件开头内容 | |||
| tail | 显示文件结尾内容 | |||
| -f | 跟踪输出 |
| stat | 显示指定文件的相关信息,比ls命令显示内容更多 |
|---|---|
| who | 显示在线登录用户 |
| hostname | 显示主机名称 |
| uname | 显示系统信息 |
| top | 显示当前系统中耗费资源最多的进程 |
| ps | 显示瞬间的进程状态 |
| du | 显示指定的文件(目录)已使用的磁盘空间的总量 |
| df | 显示文件系统磁盘空间的使用情况 |
| free | 显示当前内存和交换空间的使用情况 |
| ifconfig | 显示网络接口信息 |
| ping | 测试网络的连通性 |
| netstat | 显示网络状态信息 |
| clear | 清屏 |
| kill | 杀死一个进程 |
| 命令 | 语法 | 参数 | 参数说明 |
|---|---|---|---|
| tar | tar [-cxzjvf] 压缩打包文档的名称 欲打包目录 | ||
| -c | 建立一个归档文件的参数指令 | ||
| -x | 解开一个归档文件的参数指令 | ||
| -z | 是否需要用 gzip 压缩 | ||
| -j | 是否需要用 bzip2 压缩 | ||
| -v | 压缩的过程中显示文件 | ||
| -f | 使用档名,在 f 之后要立即接档名 | ||
| -tf | 查看归档文件里面的文件 |
tar -zcvf test.tar.gz test\
tar -zxvf test.tar.gz
启动 tomcat 进入tomcat/bin startup.sh 关闭 shutdown.sh
启动mysql service mysql start 关闭 service mysql stop
# 开关机命令
#立刻进行关机
shutdown –h now
#现在重新启动计算机
shutdown –r now
#现在重新启动计算机
reboot
#切换用户
su -
#修改用户密码
passwd
#用户注销
logout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 常用快捷命令
#补全
tab
#清屏,类似clear命令
ctrl + l
#查找历史命令(history)
ctrl + r
#终止
ctrl+c
#删除此处至末尾所有内容
ctrl+k
#删除此处至开始所有内容
ctrl+u
#查看端口占用(list open files)
lsof -i:8000
#查看指定的端口号的进程情况
netstat -tunlp
netstat -tunlp | grep 8000
#关闭端口
kill -9 PID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 文件内容查看
Linux系统中使用以下命令来查看文件的内容:
cat由第一行开始显示文件内容tac从最后一行开始显示,可以看出 tac 是 cat 的倒著写!nl显示的时候,顺道输出行号!more一页一页的显示文件内容less与more类似,但是比 more 更好的是,他可以往前翻页!head只看头几行tail只看尾巴几行
# find命令
语法
find path -option [ -print ] [ -exec -ok command ] {} \;
参数说明 :
find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。
expression 中可使用的选项有二三十个之多,在此只介绍最常用的部份。
-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
-amin n : 在过去 n 分钟内被读取过
-anewer file : 比文件 file 更晚被读取过的文件
-atime n : 在过去n天内被读取过的文件
-cmin n : 在过去 n 分钟内被修改过
-cnewer file :比文件 file 更新的文件
-ctime n : 在过去n天内被修改过的文件
-empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name
-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写
-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
-size n : 文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。-type c : 文件类型是 c 的文件。
d: 目录
c: 字型装置文件
b: 区块装置文件
p: 具名贮列
f: 一般文件
l: 符号连结
s: socket
-pid n : process id 是 n 的文件
你可以使用 ( ) 将运算式分隔,并使用下列运算。
exp1 -and exp2
! expr
-not expr
exp1 -or exp2
exp1, exp2
实例
将目前目录及其子目录下所有延伸档名是 c 的文件列出来。
# find . -name "*.c"
将目前目录其其下子目录中所有一般文件列出
# find . -type f
将目前目录及其子目录下所有最近 20 天内更新过的文件列出
# find . -ctime -20
查找/var/log目录中更改时间在7日以前的普通文件,并在删除之前询问它们:
# find /var/log -type f -mtime +7 -ok rm {} \;
查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件:
# find . -type f -perm 644 -exec ls -l {} \;
为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径:
# find / -type f -size 0 -exec ls -l {} \;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# CentOS显示ipv4
#切换到网卡目录
cd /etc/sysconfig/network-scripts/
#查看目录文件 配置文件名字可能不同
ls
#修改配置文件
vim ifcfh-ens33
#输入i 修改
ONBOOT=no 改成 yes
#修改关闭
:wq
#重启网卡服务
/etc/init.d/network restart
#查看ip
ifconfig
ip addr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# CentOS更改为阿里源
#切换到yum目录
cd /etc/yum.repos.d
#修改CentOS-Base.repo
vim CentOS-Base.repo
#修改镜像
baseurl=https://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/
#修改CentOS-AppStream.repo
vim CentOS-AppStream.repo
#修改镜像
baseurl=https://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/
#清理yum缓存,使设置生效
yum clean all
#将服务器上的软件包信息缓存到本地,以提高搜索安装软件的速度
yum makecache
#更新本地yum
yum -y update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Ubuntu更改阿里源
# 修改源文件
vim /etc/apt/sources.list
1
# 把文件内容替换成如下
deb http://mirrors.aliyun.com/debian wheezy main contrib non-free
deb-src http://mirrors.aliyun.com/debian wheezy main contrib non-free
deb http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free
deb http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free
1
2
3
4
5
6
2
3
4
5
6
# 更新源
apt-get update
1
# XShell安装地址
https://www.netsarang.com/zh/free-for-home-school/
1
# 防火墙
#查看防火墙服务状态
systemctl status firewalld
# 开启防火墙
systemctl start firewalld
# 重启防火墙
systemctl restart firewalld
# 关闭防火墙
systemctl stop firewalld
#查看防火墙规则
firewall-cmd --list-all
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=8080/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;
#开机自启
systemctl enable firewalld.service
#关闭开机自启
systemctl disable firewalld.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Linux 修改镜像源
# Ubuntu
# 首先编辑sources.list这个文件
vi /etc/apt/sources.list
1
# 把sources.list文件内容替换成如下
#ali源
deb http://mirrors.aliyun.com/debian wheezy main contrib non-free
deb-src http://mirrors.aliyun.com/debian wheezy main contrib non-free
deb http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free
deb http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free
1
2
3
4
5
6
7
2
3
4
5
6
7
#清华镜像源
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 接下来执行以下命令,就完成阿里云kali linux镜像源了
apt-get update
1
# Centos
# 首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo
`[root@localhost ~]``# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup`
1
# 下载ailiyun的yum源配置文件到/etc/yum.repos.d/
CentOS7
`[root@localhost ~]``# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo`
1
# 运行yum makecache生成缓存
[root@localhost ~]``# yum makecache
# 这时候再更新系统就会看到以下mirrors.aliyun.com信息
[root@localhost ~]``# yum -y update
已加载插件:fastestmirror, refresh-packagekit, security
设置更新进程Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
1
2
3
4
5
6
2
3
4
5
6
# SSH
# 更新源列表
打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"-->回车,就可以了。
# 安装openssh-server
在终端中输入: sudo apt-get install openssh-server
# 扩展阅读
# YAML 配置文件语言
YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便。YAML 语言的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。它的基本语法规则如下:
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用 TAB 键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
# 表示注释,从这个字符一直到行尾,都会被解析器忽略。YAML 支持的数据结构有三种:
- 对象: 键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 数组: 一组按次序排列的值,又称为序列(sequence) / 列表(list)
- 纯量(scalars): 单个的、不可再分的值
# YAML 对象
对象的一组键值对,使用冒号结构表示
animal: pets
1
# YAML 数组
一组连词线开头的行,构成一个数组
- Cat
- Dog
- Goldfish
1
2
3
2
3
数据结构的子成员是一个数组,则可以在该项下面缩进一个空格
- Array
- Cat
- Dog
- Goldfish
1
2
3
4
2
3
4
# YAML 复合结构
对象和数组可以结合使用,形成复合结构
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# YAML 纯量
纯量是最基本的、不可再分的值。以下数据类型都属于 JavaScript 的纯量
- 字符串
- 布尔值
- 整数
- 浮点数
- Null
- 时间
- 日期
# 修改 IP 和 DNS
课程演示会采用多虚拟机模拟分布式场景,为防止 IP 冲突,无法联网等问题,需要预先设置好主机名、IP、DNS 配置
# 修改主机名
- 修改 cloud.cfg 防止重启后主机名还原
vi /etc/cloud/cloud.cfg
# 该配置默认为 false,修改为 true 即可
preserve_hostname: true
1
2
3
2
3
- 修改主机名
# 修改主机名
hostnamectl set-hostname 【deployment】
# 配置 hosts
cat >> /etc/hosts << EOF
192.168.12.130
【deployment】
EOF
1
2
3
4
5
6
7
2
3
4
5
6
7
# 修改 IP
编辑 vi /etc/netplan/50-cloud-init.yaml 配置文件,修改内容如下
network:
ethernets:
ens33:
addresses: [192.168.12.130/24]
gateway4: 192.168.12.2
nameservers:
addresses: [192.168.12.2]
version: 2
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
使用 netplan apply 命令让配置生效
# 修改 DNS
# 取消 DNS 行注释,并增加 DNS 配置如:114.114.114.114,修改后重启下计算机
vi /etc/systemd/resolved.conf
1
2
2
# 查询端口是否被占用
netstat -anp |grep 8081
1