0%

linux运维

缘起

记录一些常规操作

shell快捷键

快捷键用处
ctrl+a光标移动至行首
ctrl+e光标移动至行尾
Esc b左移一个单词(back)
Esc f右移一个单词(forward)

linux Commands

从服务器上下载和上传文件

安装:yum install lrzsz

下载:sz filename

上传文件到服务端:rz

参考文献

查看核心数

参考文献

新建用户

sudo adduser username

删除用户

sudo userdel username

将用户添加到sudo组

sudo adduser username sudo

显示端口占用

sudo netstat -tlpn

根据pid杀死进程

kill -9 pid

配置密匙

TODO

禁用22端口

TODO

df

df -hdf -kh的区别是什么

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
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 37G 325M 100% /
devtmpfs 909M 0 909M 0% /dev
tmpfs 920M 4.0K 920M 1% /dev/shm
tmpfs 920M 97M 823M 11% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 12K 184M 1% /run/user/0
tmpfs 184M 0 184M 0% /run/user/1003
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 37G 325M 100% /
devtmpfs 909M 0 909M 0% /dev
tmpfs 920M 4.0K 920M 1% /dev/shm
tmpfs 920M 97M 823M 11% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 12K 184M 1% /run/user/0
tmpfs 184M 0 184M 0% /run/user/1003
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -th
df: no file systems processed
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 40G 37G 325M 100% /
devtmpfs devtmpfs 909M 0 909M 0% /dev
tmpfs tmpfs 920M 4.0K 920M 1% /dev/shm
tmpfs tmpfs 920M 97M 823M 11% /run
tmpfs tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs tmpfs 184M 12K 184M 1% /run/user/0
tmpfs tmpfs 184M 0 184M 0% /run/user/1003
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/vda1 2621440 683809 1937631 27% /
devtmpfs 232620 324 232296 1% /dev
tmpfs 235277 2 235275 1% /dev/shm
tmpfs 235277 487 234790 1% /run
tmpfs 235277 16 235261 1% /sys/fs/cgroup
tmpfs 235277 5 235272 1% /run/user/0
tmpfs 235277 1 235276 1% /run/user/1003
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -ki
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/vda1 2621440 683809 1937631 27% /
devtmpfs 232620 324 232296 1% /dev
tmpfs 235277 2 235275 1% /dev/shm
tmpfs 235277 487 234790 1% /run
tmpfs 235277 16 235261 1% /sys/fs/cgroup
tmpfs 235277 5 235272 1% /run/user/0
tmpfs 235277 1 235276 1% /run/user/1003
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 41151808 38705684 332692 100% /
devtmpfs 930480 0 930480 0% /dev
tmpfs 941108 4 941104 1% /dev/shm
tmpfs 941108 98940 842168 11% /run
tmpfs 941108 0 941108 0% /sys/fs/cgroup
tmpfs 188224 12 188212 1% /run/user/0
tmpfs 188224 0 188224 0% /run/user/1003
(base) [root@izuf66ctwpabqmq17tt0r3z ~]# df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 37G 325M 100% /
devtmpfs 909M 0 909M 0% /dev
tmpfs 920M 4.0K 920M 1% /dev/shm
tmpfs 920M 97M 823M 11% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 12K 184M 1% /run/user/0
tmpfs 184M 0 184M 0% /run/user/1003
(base) [root@izuf66ctwpabqmq17tt0r3z ~]#

添加软链

ln -s [源文件或目录] [目标文件或目录]

zip文件

将 /home/html/ 这个目录下所有文件和文件夹打包为当前目录下的 html.zip:

1
zip -q -r html.zip /home/html

screen

添加一个新的screen

1
screen -S session_name

重新attach进之前的session

1
screen -r

关闭某个session

1
screen -XS [session # you want to quit] quit

tar

解压tar.xz: tar -xf

解压*.tar.gz: tar -zxvf 压缩文件名.tar.gz

查看文件夹大小

1
2
3
du -sh

# 634M .

echo

1
2
3
4
5
6
# 追加内容
echo 'xxx' >> a.txt

# 覆盖写
echo 'xxx' > a.txt

rm

1
2
3
# 删除指定后缀的文件

find . -name "*.h5" | xargs rm -rf

grep

如果对应日志过于长,想要了解匹配内容的概览,可以使用正则截断
grep -noE '你想匹配的内容.{100}' xxxx.log

  • -n 显示行号
  • -o 只显示匹配到的内容
  • -E 支持正则表达式
  • . 匹配任意字符
  • {100} 匹配规则出现100次

如果特定的行过于长,可以管道到less来翻页查看

grep '匹配到这一行的内容非常长' xxxx.log | less

  • b 向上翻一页
  • d 向后翻半页
  • h 显示帮助界面
  • Q 退出less 命令
  • u 向前滚动半页
  • y 向前滚动一行
  • G - 移动到最后一行
  • g - 移动到第一行

grep文件带通配符,同时grep多个文件

grep -R '关键字' ./20230215-* | grep '关键字2'

grep -v剔除某个关键字

grep -v “需要被排除的关键字”

zgrep

如果日志被打包成gz格式之后,如果想要查询对应的日志可以通过zgrep xxx.gz

zcat

Zcat是一个命令行实用程序,用于查看压缩文件的内容。它将压缩文件扩展为标准输出,允许您查看内容。

代码详情
1
2
$ zcat /var/log/syslog.2.gz | tail -1
Aug 24 07:09:02 myhost rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="796" x-info="http://www.rsyslog.com"] rsyslogd was HUPed

参考文献

centos相关

屏蔽ip

总有脚本小子到处扫端口,把应用都扫坏了,所以有了这个需求

1
firewall-cmd --list-rich-rules # 查看屏蔽的ip

添加ip

1
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=43.229.53.61 reject"

重启防火墙生效
1
firewall-cmd --reload

一条长命令整合上面的操作
1
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=162.142.125.54 reject" && firewall-cmd --reload && firewall-cmd --list-rich-rules

ubuntu相关

很多机器学习的环境都是基于ubuntu的,所以把一台云服务器的系统换成了ubuntu

文件颜色的意义

  • 蓝色:文件夹
  • 绿色:表示可执行文件
  • 红色:表示tar压缩文件
  • 黄色:表示设备文件,在/dev下全是这类文件
  • 浅蓝色:表示链接文件
  • 白色文件:一般性文件,如文本文件,配置文件,源码文件

查看ubuntu版本

在ubuntu上安装v2ray

tx云的网络太拉跨了,装包和git clone什么的都要半天,没有proxy根本过不去日子

去repo的release下载并放到服务器解压

支线1:v2ray.service启动(systemctl start v2ray不起来)不起来

出现了两个bug

  1. cp: cannot create regular file '/usr/lib/systemd/v2ray.service': Permission deni
  2. Unit v2ray.service could not be found.

解决cannot create regular file

cp命令前加sudo

解决Unit v2ray.service could not be found.

将v2ray.service复制到/etc/systemd/system/文件夹下

支线2:v2ray启动不起来


TODO
service文件里面写的v2ray和json地址和我之前写的bash小脚本的位置不太一样

ubuntu添加service

clash for linux

群晖

配置使用密匙登录

参考文献

参考文献

参考文献

设置开机自启

参考文献

sed、grep和awk之间的区别

参考文献

What are the differences among grep, awk & sed?

What is the difference between sed and awk?

sed:Readability of scripts can be difficult. Mathematical operations are extraordinarily awkward at best.I would tend to use sed where there are patterns in the text.

awk:I would use awk when the text looks more like rows and columns or, as awk refers to them “records” and “fields”.

For me the rule to separate them is: Use sed to automate tasks you would do otherwise in a text editor manually. That’s why it is called stream editor. (You can use the same commands to edit text in vim). Use awk if you want to analyze text, meaning counting fields, calculate totals, extract and reorganize structures etc.

curl的使用

参考文献