目录
固定ip 中文字符集
#查看当前ip
ip addr
#sudo vim /etc/netplan/00-installer-config.yaml 打开配置文件,输入i编辑,配置完成后,按esc输入:wq保存退出
注意缩进
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
addresses: [192.168.16.30/24]
gateaway: 192.168.16.1
nameservices: [192.168.16.1,114.114.114.114]
version: 2
~
#启用配置
sudo netplan apply
#安装中文字符集
sudo apt install language-pack-zh-han* -y
#编辑 sudo vim /etc/default/locale 文件
webrx@us:~$ cat /etc/default/locale
LANG="zh_CN.UTF-8"
LANGUAGE="zh_CN:zh"
LC_NUMERIC="zh_CN"ip
LC_TIME="zh_CN"
LC_MONETARY="zh_CN"
LC_PAPER="zh_CN"
LC_NAME="zh_CN"
LC_ADDRESS="zh_CN"
LC_TELEPHONE="zh_CN"
LC_MEASUREMENT="zh_CN"
LC_IDENTIFICATION="zh_CN"
LC_ALL="zh_CN.UTF-8"
## 配置文件立即生效
$ source /etc/default/locale
webrx@us:~$ date
2021年 12月 10日 星期五 06:42:11 UTC
#设置时区
$ sudo tzselect
然后输入下面立即生效
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
版本信息
cat /proc/version
Linux version 5.4.0-91-generic (buildd@lcy01-amd64-017) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021
查看系统的位是32位还是64位
webrx@us:~$ getconf LONG_BIT
64
webrx@us:~$ uname -m
x86_64
切换窗口
`ctrl+alt + f1 f2 f3 f4 f5 f6 6个终端窗口`
Linux重启命令:
1、reboot
2、shutdown -r now 立刻重启(root用户使用)
3、shutdown -r 10 过10分钟自动重启(root用户使用)
4、shutdown -r 20:35 在时间为20:35时候重启(root用户使用)
如果是通过shutdown命令设置重启的话,可以用shutdown -c命令取消重启
Linux关机命令:
1、halt 立刻关机
2、poweroff 立刻关机
3、shutdown -h now 立刻关机(root用户使用)
4、shutdown -h 10 10分钟后自动关机
如果是通过shutdown命令设置关机的话,可以用shutdown -c命令取消重启
# 重新启动
$ sudo reboot
# 切换root账号
$ sudo su
# 切换webrx账号
$ su webrx
$ cd默认回到家目录
# 立即关机
$ sudo poweroff
#关闭系统
$ sudo halt
#关闭系统并关闭电源
$ sudo halt -p
#关闭系统,但不留下纪录
$ sudo halt -d
##系统五分钟后关机
$ sudo shutdown +5 “This System will be shutdown in 5 minutes”
##取消关机
sudo shutdown -c
建立目录
#建立目录
mkdir a bc ccc
直接建立多级目录
mkdir user/xy/java/aa -p
使用tree命令查看
tree user
user
└── xy
└── java
└── aa
删除目录
#删除目录
rmdir只能删除空目录
rmdir a
tt目录里面不能有东西
rmdir user/xx/java/tt -p
推荐使用 rm -rf 强制递归不提示删除目录 (忽略内容) r代表递归 f代表不提示
rm -rf a1
查看目录
#查看目录
ls -a
只显示目录
webrx@us:~$ ls -F | grep /$
a/
aa/
aaa/
cc/
只显示目录(d开头)
webrx@us:~$ ls -al | grep "^d"
drwxr-xr-x 8 webrx webrx 4096 12月 6 11:46 .
drwxr-xr-x 3 root root 4096 11月 30 18:03 ..
drwxrwxr-x 3 webrx webrx 4096 12月 3 20:51 a
drwxrwxr-x 2 webrx webrx 4096 12月 3 20:51 aa
drwxrwxr-x 3 webrx webrx 4096 12月 3 20:52 aaa
drwx------ 2 webrx webrx 4096 11月 30 18:52 .cache
drwxrwxr-x 2 webrx webrx 4096 12月 3 20:51 cc
drwxrwxr-x 3 webrx webrx 4096 12月 2 20:58 .java
只显示文件
webrx@us:~$ ls -al | grep "^-"
-rw------- 1 webrx webrx 2811 12月 7 11:19 .bash_history
-rw-r--r-- 1 webrx webrx 220 2月 25 2020 .bash_logout
-rw-r--r-- 1 webrx webrx 3771 2月 25 2020 .bashrc
-rw-rw-r-- 1 webrx webrx 461 12月 2 21:03 Demo.class
-rw-r--r-- 1 root root 274 12月 2 09:57 Demo.java
-rw-r--r-- 1 root root 2186 12月 6 11:46 exit
-rw-rw-r-- 1 webrx webrx 180723427 10月 6 00:06 jdk-17_linux-x64_bin.tar.gz
-rw-rw-r-- 1 webrx webrx 538037 12月 3 15:36 linux.txt
-rw-r--r-- 1 webrx webrx 807 2月 25 2020 .profile
-rw------- 1 webrx webrx 15 12月 6 11:38 .rediscli_history
-rw-r--r-- 1 webrx webrx 0 12月 1 20:09 .sudo_as_admin_successful
-rw------- 1 webrx webrx 12288 12月 2 09:04 .swp
-rw-r--r-- 1 root root 61 12月 2 09:24 user.txt
-rw------- 1 webrx webrx 583 12月 2 09:05 .viminfo
查找只显示当前目录
find . -type d
.
./a
./a/b
./a/b/x
./cc
./aa
./aaa
./aaa/bbb
./aaa/bbb/ccc
./.cache
./.java
./.java/.userPrefs
./.java/.userPrefs/tool
./.java/.userPrefs/tool/JShell
tree 命令
webrx@us:~$ tree
.
├── a
│ └── b
│ └── x
├── aa
├── aaa
│ └── bbb
│ └── ccc
├── cc
├── Demo.class
├── Demo.java
├── exit
├── jdk-17_linux-x64_bin.tar.gz
├── linux.txt
└── user.txt
8 directories, 6 files
webrx@us:~$ tree ~
/home/webrx
├── a
│ └── b
│ └── x
├── aa
├── aaa
│ └── bbb
│ └── ccc
├── cc
├── Demo.class
├── Demo.java
├── exit
├── jdk-17_linux-x64_bin.tar.gz
├── linux.txt
└── user.txt
8 directories, 6 files
webrx@us:~$ tree /usr/local/jdk-17.0.1 -L 1
/usr/local/jdk-17.0.1
├── bin
├── conf
├── include
├── jmods
├── legal
├── lib
├── LICENSE -> legal/java.base/LICENSE
├── man
└── release
7 directories, 2 files
复制目录
#复制 cp
. 代表当前目录 ..上一级目录 ~家目录 cd回家目录 pwd输出当前工作的目录
ebrx@us:~/aa$ ls ../
a aa aaa cc Demo.class Demo.java exit jdk-17_linux-x64_bin.tar.gz linux.txt user.txt
将家目录的 user.txt 和 上一级目录的linux.txt 拷贝到当前目录
webrx@us:~/aa$ cp ~/user.txt ../linux.txt .
webrx@us:~/aa$ ls
linux.txt user.txt
将cc目录复制到aa
webrx@us:~$ cp -r cc aa
webrx@us:~$ tree
.
├── a
│ └── b
│ └── x
├── aa
│ ├── cc
│ ├── linux.txt
│ └── user.txt
├── aaa
│ └── bbb
│ └── ccc
├── cc
├── Demo.class
├── Demo.java
├── exit
├── jdk-17_linux-x64_bin.tar.gz
├── linux.txt
└── user.txt
9 directories, 8 files
把aa目录下的所有内容拷贝到aaa下
webrx@us:~$ cp -r aa/* aaa
webrx@us:~$ tree
.
├── a
│ └── b
│ └── x
├── aa
│ ├── cc
│ ├── linux.txt
│ └── user.txt
├── aaa
│ ├── a
│ │ └── b
│ │ └── x
│ ├── bbb
│ │ └── ccc
│ ├── cc
│ ├── linux.txt
│ └── user.txt
├── cc
├── Demo.class
├── Demo.java
├── exit
├── jdk-17_linux-x64_bin.tar.gz
├── linux.txt
└── user.txt
移动剪切粘贴 mv
webrx@us:~$ tree aaa
aaa
├── a
│ └── b
│ └── x
├── bbb
│ └── ccc
├── cc
├── linux.txt
└── user.txt
6 directories, 2 files
webrx@us:~$ cd aaa
webrx@us:~/aaa$ mv a bbb
webrx@us:~/aaa$ tree
.
├── bbb
│ ├── a
│ │ └── b
│ │ └── x
│ └── ccc
├── cc
├── linux.txt
└── user.txt
目录修改名称
webrx@us:~/aaa$ ls
bbb cc linux.txt user.txt
webrx@us:~/aaa$ mv user.txt Demo.java
webrx@us:~/aaa$ ls
bbb cc Demo.java linux.txt
webrx@us:~/aaa$
查找文件
将查找的问件复制到指定的目录下
webrx@us:~$ find redis-6.2.6/ -name redis.conf | xargs cp -t .
webrx@us:~$ ls
a aaa Demo.class exit linux.txt redis-6.2.6.tar.gz user.txt
aa cc Demo.java jdk-17_linux-x64_bin.tar.gz redis-6.2.6 redis.conf
查看文件
#查看文件最后15行
cat -n redis.conf | tail -n 15
2037 # Set bio threads to cpu affinity 1,3:
2038 # bio_cpulist 1,3
2039 #
2040 # Set aof rewrite child process to cpu affinity 8,9,10,11:
2041 # aof_rewrite_cpulist 8-11
2042 #
2043 # Set bgsave child process to cpu affinity 1,10,11
2044 # bgsave_cpulist 1,10-11
2045
2046 # In some cases redis will emit warnings and even refuse to start if it detects
2047 # that the system is in bad state, it is possible to suppress these warnings
2048 # by setting the following config which takes a space delimited list of warnings
2049 # to suppress
2050 #
2051 # ignore-warnings ARM64-COW-BUG
#查看文件不显示空行和注释行(带行号)
webrx@us:~$ grep -Ev '^$|^#' redis.conf | -n
webrx@us:~$ grep -Ev '^$|^#' redis.conf | nl
#输出内容到redisok.conf
webrx@us:~$ grep -Ev '^$|^#' redis.conf | cat -n > redisok.conf
查看port开头的内容
webrx@us:~$ grep '^port.*' redis.conf
port 6379
#grep命令
##### 查看文件技巧,只看内容,不看空行和注释
$grep -Ev '^$|^#' redis.conf | cat -n
$grep -Ev '^$|^#' redis.conf | nl
$grep -Ev '^$|^#' redis.conf
$grep -Ev '^$|^#' redis.conf | cat -n > redisok.conf
#### 查看redis.conf 文件指定的内容
webrx@us:~$ grep -E '^port.*$|^d.*$|^log.*$|^pro.*$' redis.conf
protected-mode yes
port 6379
daemonize no
loglevel notice
logfile ""
databases 16
proc-title-template "{title} {listen-addr} {server-mode}"
dbfilename dump.rdb
dir ./
disable-thp yes
dynamic-hz yes
建立文件
#建立空文件
webrx@us:~$ touch at.txt
# vim y
输入i
输入内容....
按esc 输入wq 保存并退出 建立文件y
#将内容输出到文件中
webrx@us:~$ echo hello > at.txt
webrx@us:~$ cat at.txt
hello
#将内容追加到文件尾
webrx@us:~$ date | xargs echo >> at.txtt
webrx@us:~$ cat at.txt
hello
Date
2021年 12月 09日 星期四 15:16:19 CST
#计算器
webrx@us:~$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3*3
9
3*9
27
quit
#在D开头的那一行前添加 内容
webrx@us:~$ sed -i '/D/i 222java' at.txt
webrx@us:~$ cat at.txt
hello
222java
Date
2021年 12月 09日 星期四 15:16:19 CST
2021年 12月 09日 星期四 15:29:10 CST
#在D开头的那一行后面添加内容
webrx@us:~$ sed -i '/D/a 3333python' at.txt
webrx@us:~$ cat at.txt
hello
222java
Date
3333python
2021年 12月 09日 星期四 15:16:19 CST
2021年 12月 09日 星期四 15:29:10 CST
webrx@us:~$ ^C
webrx@us:~$
#在第五行 添加内容
webrx@us:~$ sed -i '4 a 999999999999' y
webrx@us:~$ cat y -n
1 3898893
2 java
3 php
4 sys
5 999999999999
6 5555555555555555
7 23232323232
删除文档的第一行 sed -i '1d' <file>
2.删除文档的最后一行 sed -i '$d' <file>
3.在文档指定行中增加一行 例如文档如下: echo "1"; echo "2"; echo...
4.删除文件中的第k行,例如k=3 sed -i '3d' <file>
删除复制文件
#删除文件rm
$ rm a.txt bbb clear My.html User.java
#复制文件 cp
webrx@us:~$ sudo cp user.txt /usr
webrx@us:~$ ls /usr
bin config games include lib lib32 lib64 libexec libx32 local sbin share src user.txt
webrx@us:~$ sudo mv user.txt .
#移动文件 mv
webrx@us:~$ sudo mv linux.txt /usr
webrx@us:~$ ls /usr
bin games lib lib64 libx32 local share user.txt
config include lib32 libexec linux.txt sbin src
#把us.txt移动到usr目录下并且改名字
webrx@us:~$ ls
a at.txt Hello.class jdk-17_linux-x64_bin.tar.gz y
aa bashrc Hello.java us.txt
webrx@us:~$ sudo mv us.txt /usr/uuuu.txt
[sudo] webrx 的密码:
webrx@us:~$ ls
a at.txt Hello.class jdk-17_linux-x64_bin.tar.gz
aa bashrc Hello.java y
webrx@us:~$ cd /usr
webrx@us:/usr$ ls
bin games lib lib64 libx32 sbin src
config include lib32 libexec local share uuuu.txt
别名
# alias语法
语法:alias [name=”value”]
alias为当前用户提供啦有效的别名列表,注意:等号前后没有空格。
#alias永久化
在我们的主目录下(/home/user)有一个.bashrc的文件,我们可以通过vim .bashrc去编辑这个文件,添加任何我们想要的别名,如:alias p=’pwd’
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias c='clear'
文件目录查找命令
#查找yaml结尾的文件
sudo find / -name *.yaml
#在家目录下查找redis.conf
webrx@us:~$ sudo find ~ -name redis.conf
# 没有通配符,可以不加单引号
sudo find ./ -name 1.txt
# 根据文件大小查找,查找大小等于2M的文件
sudo find ./ -size 2M
#根据文件大小查找,查找大小大于2M的文件
sudo find ./ -size +2M
### 将查找的文件 并复制到指定的目录下
webrx@us:~$ find redis-6.2.6/ -name redis.conf | xargs cp -t .
webrx@us:~$ ls
a abc b Demo.class Demo.java jdk-17_linux-x64_bin.tar.gz linux.txt redis-6.2.6 redis-6.2.6.tar.gz redis.conf user.txt
webrx@us:~$
ufw防火墙
# 安装防火墙 ufw
sudo apt install ufw -y
# 卸载防火墙
sudo apt remove ufw -y
# 启动防火墙
sudo ufw enable
# 禁用防火墙
sudo ufw disable
# 临时停用防火墙
sudo systemctl stop ufw
启用防火墙后ssh远程连接会断开,使用命令允许连接
sudo ufw allow ssh
可以看到已经对22端口放行
#查看防火墙状态
webrx@us:~$ sudo ufw status
[sudo] webrx 的密码:
状态: 激活
至 动作 来自
- -- --
22/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
#还可以添加8080端口 就可以查看tomcat主页
sudo ufw allow 8080
webrx@us:~$ sudo ufw status
状态: 激活
至 动作 来自
- -- --
22/tcp ALLOW Anywhere
8080 ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
# 允许80 - 90端口可以访问 80 81 82 83 ... 90
sudo ufw allow 80:90/tcp
sudo ufw allow from 192.168.11.166
sudo ufw allow from 192.168.11.166 to any port 22
# 允许1.0-1.24之间可以访问
sudo ufw allow from 192.168.1.0/24 to any port 3306
#-----------------------禁止访问
sudo ufw deny from 23.24.25.0/24
# --------------------------删除规则
webrx@us:~$ sudo ufw delete allow 22
规则已删除
规则已删除 (v6)
查看端口占用
webrx@us:~$ sudo lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 5142 mysql 24u IPv4 77906 0t0 TCP localhost:mysql (LISTEN)
webrx@us:~$ sudo lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 9323 tomcat 37u IPv6 103080 0t0 TCP *:http-alt (LISTEN)
安装net-tools
webrx@us:~$ sudo netstat -apn | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 9323/java
webrx@us:~$ sudo netstat -apn | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 9323/java
webrx@us:~$ ^C
webrx@us:~$ sudo netstat -apn | grep 3306
tcp 0 0 127.0.0.1:33060 0.0.0.0:* LISTEN 5142/mysqld
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 5142/mysqld
webrx@us:~$ sudo netstat -apn | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 6784/redis-server 1
tcp6 0 0 ::1:6379 :::* LISTEN 6784/redis-server 1
webrx@us:~$
服务进程查看
## 查看服务状态
systemctl status docker.service
service docker status
#服务的停止/开启/重启/开机自动运行/开机不自动运行
service docker stop/start/restart/enable/disable
systemctl stop/restart/start/enable/disable docker.service
root@us:~# service docker status
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-12-20 20:50:14 CST; 9min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 36124 (dockerd)
Tasks: 8
Memory: 28.3M
CGroup: /system.slice/docker.service
└─36124 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.so>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.178749441+08:00" level=warn>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.180274638+08:00" level=warn>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.180646763+08:00" level=warn>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.181089215+08:00" level=info>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.797822597+08:00" level=info>
12月 20 20:50:13 us dockerd[36124]: time="2021-12-20T20:50:13.627027795+08:00" level=info>
12月 20 20:50:14 us dockerd[36124]: time="2021-12-20T20:50:14.065046555+08:00" level=info>
12月 20 20:50:14 us dockerd[36124]: time="2021-12-20T20:50:14.069916825+08:00" level=info>
12月 20 20:50:14 us systemd[1]: Started Docker Application Container Engine.
12月 20 20:50:14 us dockerd[36124]: time="2021-12-20T20:50:14.669693658+08:00" level=info>
root@us:~# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-12-20 20:50:14 CST; 11min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 36124 (dockerd)
Tasks: 8
Memory: 28.1M
CGroup: /system.slice/docker.service
└─36124 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.178749441+08:00" level=warning>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.180274638+08:00" level=warning>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.180646763+08:00" level=warning>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.181089215+08:00" level=info ms>
12月 20 20:50:11 us dockerd[36124]: time="2021-12-20T20:50:11.797822597+08:00" level=info ms>
12月 20 20:50:13 us dockerd[36124]: time="2021-12-20T20:50:13.627027795+08:00" level=info ms>
12月 20 20:50:14 us dockerd[36124]: time="2021-12-20T20:50:14.065046555+08:00" level=info ms>
12月 20 20:50:14 us dockerd[36124]: time="2021-12-20T20:50:14.069916825+08:00" level=info ms>
12月 20 20:50:14 us systemd[1]: Started Docker Application Container Engine.
12月 20 20:50:14 us dockerd[36124]: time="2021-12-20T20:50:14.669693658+08:00" level=info ms>
lines 1-21/21 (END)