程序员笔记

想起来啥,就写啥,略显凌乱~

Linux

– 常用命令

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
# 自动补全命令
yum install -y bash-completion

# 查看系统内存
free -h

# 查看系统版本信息
cat /etc/redhat-release
cat /proc/version
uname -a

# 查看服务器是物理机还是虚拟机
dmidecode -s system-manufacturer

# 安装htop
yum install epel-release -y
yum -y install htop

# 安装常用命令
yum -y install telnet net-tools sysstat

# Linux进程查看器
htop

# 显示cpu的相关信息
lscpu

# 查看CPU信息
cat /proc/cpuinfo

# 查看机器一共几个cpu
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

# 查看单个cpu的物理核数
cat /proc/cpuinfo| grep "cpu cores"| uniq

# 查看总逻辑核数
cat /proc/cpuinfo| grep "processor"| wc -l

# 查看SELinux工作模式
getenforce
sestatus

# 永久关闭selinux
vi /etc/selinux/config
# 关闭:SELINUX=disabled
# 宽容模式:SELINUX=permissive
# 强制模式:SELINUX=enforcing

# SELinux工作模式临时切换(重启失效)
#切换成宽容模式
setenforce 0
#切换成强制模式
setenforce 1

# 指定目录或文件以外的文件全部移动到新的目录
rsync -av --remove-source-files --exclude='_install' --exclude='1.sh' --exclude='oper' --exclude='oper_bk' --exclude='oper_bk_0721' --exclude='oper_bk0801' --exclude='r_bk' /dockerdata/nginx/html/api/ ~/test/


# linux 下 取进程占用 cpu 最高的前10个进程
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head
ps auxw|head -1;ps auxw|sort -rn -k3|head -10

# linux 下 取进程占用内存(MEM)最高的前10个进程
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head
ps auxw|head -1;ps auxw|sort -rn -k4|head -10

# 虚拟内存使用最多的前10个进程
ps auxw|head -1;ps auxw|sort -rn -k5|head -10

# snmp v3测试连接
snmpwalk -v3 -u snmpv3User -l authNoPriv -a MD5 -A zmsj1234 192.168.101.7

# snmp v2测试连接
snmpwalk -v2c -c 团体字 ip

1
2
3
4
5
6
7
8
9
10
11
# 尽可能使用内存而不用swap 
echo "vm.swappiness=1">>/etc/sysctl.conf
sysctl -p

# 刷新SWAP
# 刷新一次SWAP(将SWAP里的数据转储回内存,并清空SWAP里的数据)
swapoff -a && swapon -a

# 清理缓存
sync
echo 3> /proc/sys/vm/drop_caches

– 防火墙

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
# 查看防火墙状态
systemctl status firewalld

# 停止防火墙
systemctl stop firewalld
# 开机不启动
systemctl disable firewalld

# 开启防火墙
systemctl start firewalld
# 开机启动
systemctl enable firewalld

# 重启防火墙
systemctl restart firewalld


# 开放指定端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent

# 不中断服务的重新加载防火墙
firewall-cmd --reload

# 查询哪些端口时开启的
firewall-cmd --list-port

– 查看网络连接、端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//端口
netstat -tunlp |grep xxx 查看网络连接
-t (tcp) 仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化为数字
-l 仅列出在Listen(监听)的服务状态
-p 显示建立相关链接的程序名

-a (all) 显示所有选项,默认不显示LISTEN相关。
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令

lsof -i:端口号
用于查看某一端口的占用情况,比如查看9092端口使用情况,lsof -i:9095

– 设置linux默认不启动图形界面

1
systemctl get-default

返回结果:
multi-user.target 相当于init 3,命令行模式;
graphical.target 相当于init 5,图形界面模式。

1
2
3
4
5
# 设置开机默认启动命令行模式:
systemctl set-default multi-user.target

# 设置开机默认启动图形界面:
systemctl set-default graphical.target

– Linux启动提示“/dev/mapper/ao-root:unexpected inconsistency;RUN fsck MANUALLY”解决办法

1
2
# 在修复模式下输入以下命令后重启
fsck -y /dev/mapper/ao-root # 具体分区信息

Docker

- Docker安装

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
# 安装依赖
yum -y install yum-utils device-mapper-persistent-data lvm2

# 设置阿里镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 更新yum索引
sudo yum makecache fast

# 安装docker-ce社区版
sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 下载离线包
# yum install --downloadonly --downloaddir=./ docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 安装好以后启动docker 并设置开机自启
systemctl start docker
systemctl enable docker

# 查看安装状态
docker info

# 查看Docker运行状态
systemctl status docker

# 重新启动Docker服务
systemctl restart docker

# 停止docker服务
systemctl stop docker

配置镜像加速器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sudo mkdir -p /etc/docker

tee /etc/docker/daemon.json << EOF
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
EOF

sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://uxs8f4rt.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 迁移docker安装位置

systemctl stop docker

# 复制Docker根目录
cp -a /var/lib/docker /www

# 编辑Docker的配置文件
vi /etc/docker/daemon.json
# 添加 "data-root": "/www/docker"
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"data-root": "/www/docker"
}

systemctl daemon-reload
systemctl start docker

- Docker Compose安装

1
2
3
4
5
6
7
8
# 下载 Docker Compose 的二进制文件。可以使用以下命令来下载最新版本的 Docker Compose:
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 赋予 Docker Compose 可执行权限
chmod +x /usr/local/bin/docker-compose

# 验证 Docker Compose 是否安装成功:
docker-compose --version

- Docker使用

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
# 下载镜像
docker pull centos:7.9.2009

# 浏览镜像文件
docker images

# 删除镜像文件
docker image rm hello-world

# 创建并启动容器(Container)
docker run -it --name centos7.9 -p 8888:8888 -p 80:80 centos:7.9.2009 bash

# 查看docker运行中的容器
docker ps

# 查看docker中的所有容器
docker ps -a

# 停止运行的容器
docker container stop 802

# 重新启动容器
docker container restart 802

# 进入(exec)指定容器(Container)
docker exec -it 802 bash

# 删除(rm)容器(Container)
docker container rm 802

# 如果删除运行中的容器,需要添加 -f 参数执行强制删除,例如:
docker container rm -f 802


# docker字符集
yum reinstall glibc-common -y

localedef -i zh_CN -f UTF-8 zh_CN.UTF-8
echo LANG="zh_CN.UTF-8" > /etc/locale.conf

Python

- python安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 安装依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel

# 下载python安装包
wget http://npm.taobao.org/mirrors/python/3.9.16/Python-3.9.16.tgz

# 解压
tar -zxvf Python-3.9.16.tgz
cd Python-3.9.16

# 配置安装目录并编译安装
./configure prefix=/usr/local/python3
make && make install

# 创建软链接
ln -s /usr/local/python3/bin/python3.9 /usr/bin/python39
ln -s /usr/local/python3/bin/pip3.9 /usr/bin/pip39

# 更换pip源
pip39 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

#更新pip
python39 -m pip install --upgrade pip

Nginx

- ssl证书生成配置

1
2
3
4
5
6
# ssl配置
mkdir -p /etc/nginx/ssl/
openssl req -x509 -nodes -days 10000 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
# nginx配置文件添加下面的语句
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

- nginx限制访问host

1
2
3
4
# 使用if语句判断域名是否为当前域名
if ($host != "116.63.163.39") {
return 403; # 如果不是 正确主机,则返回403 Forbidden
}

- nginx常用配置

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
85
86
87
88
89
server
{
listen 80;
listen 443 ssl http2 ;
server_name www.yangchao.me;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/web;

# 常见漏扫请求头,根据实际情况选择性添加,有些请求头影响调用,需要测试

# 指定了网页的嵌入策略,要求所有嵌入的内容都采用同源策略。可选参数包括 require-corp, unsafe-none 和 none。
add_header Cross-Origin-Embedder-Policy require-corp;
# 设置了跨源打开策略,限制了页面可以打开哪些源的窗口。# 可选参数包括 same-origin, same-origin-allow-popups 和 unsafe-none。
add_header Cross-Origin-Opener-Policy same-origin;
# 确定了跨域资源共享策略,仅允许同源的资源请求。
add_header Cross-Origin-Resource-Policy same-origin;
# 指定了清除网站数据的内容,包括缓存和cookies。可以指定需要清除的数据类型,如 cache, cookies, storage, executionContexts, plugins 等。
add_header Clear-Site-Data "cache, cookies";
# 定义了内容安全策略,限制了从哪些来源加载资源。如 default-src, script-src, style-src, img-src, connect-src, font-src, object-src, media-src, frame-src, child-src 等。
add_header Content-Security-Policy "default-src 'self'";
# 设置了引用者策略,对跨源请求仅发送源的信息。可选参数包括 no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin 和 unsafe-url。
add_header Referrer-Policy strict-origin-when-cross-origin;
# 强制使用 HTTPS 连接,保证数据传输的安全性。
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
# 防止浏览器对响应的 MIME 类型进行嗅探。
add_header X-Content-Type-Options "nosniff";
# 禁止浏览器打开下载的文件,提高安全性。
add_header X-Download-Options noopen;
# 明确指定跨域策略为 none,禁止 Flash 跨域访问。
add_header X-Permitted-Cross-Domain-Policies none;
# 设置了 X-Frame-Options 头部,防止页面被嵌套到其他网站的 <frame>, <iframe>, <embed> 或者 <object> 中。可选参数包括 DENY, SAMEORIGIN 和 ALLOW-FROM uri。
add_header X-Frame-Options "SAMEORIGIN";
# 启用浏览器的内置 XSS 过滤器,防止跨站脚本攻击。
add_header X-XSS-Protection "1; mode=block";

# 强制跳转https
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}

# ssl证书配置
ssl_certificate /www/server/panel/vhost/cert/web/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/web/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
#add_header Strict-Transport-Security "max-age=31536000"; # 上面加过这个请求头了
error_page 497 https://$host$request_uri;

#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
{
return 404;
}

# 配置反向代理
location ^~ /
{
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
# proxy_hide_header Upgrade;

add_header X-Cache $upstream_cache_status;

#设置 Nginx 缓存
set $static_fileG0ZQTXEv 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
set $static_fileG0ZQTXEv 1;
expires 1m;
}
if ( $static_fileG0ZQTXEv = 0 )
{
add_header Cache-Control no-cache;
}
}


access_log /var/log/nginx/web.log;
error_log /var/log/nginx/web.error.log;
}

- supervisor守护nginx

1
2
3
4
5
6
7
8
9
[program:nginx]
command=/usr/bin/nginx -g 'daemon off;'
directory=/usr/bin/
autostart=true
autorestart=true
redirect_stderr=true
priority=10
stdout_logfile=/var/log/supervisor/nginx.log
stderr_logfile=/var/log/supervisor/nginx.err.log

MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# mysql 连接错误码1130(not allowed to connect to this MySQL server)解决

mysql -u root -p

GRANT ALL PRIVILEGES ON *.* TO `root`@`%` IDENTIFIED BY `password` WITH GRANT OPTION;
FLUSH PRIVILEGES;



use mysql;
update user set Host='%' where User='root';
FLUSH PRIVILEGES;

SELECT user, host FROM mysql.user;

程序员笔记
http://www.yangchao.me/posts/999/
作者
小不点
发布于
2024年4月26日
许可协议