本文记录了服务器常用环境的一些命令总结。
常用软件
vim
安装:
1 | apt-get install -y vim |
gcc
安装:
1 | apt-get -y install make gcc g++ zlib1g-dev libpcre3 libpcre3-dev |
模块安装:
1 | aptitude -y install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev |
时间同步
ntpdata
安装:
1 | apt-get install ntpdate |
校准时间:
1 | ntpdate 10.32.64.33 |
使用 Linux 定时任务定时校准时间,编辑定时任务:
1 | crontab -e |
定时校准时间:
1 | 0 12 * * * /usr/sbin/ntpdate 10.32.64.33 |
开机自启动
检查是否状态:
1 | systemctl status rc-local |
先检查是否有 /etc/rc.local
文件:
1 | cat /etc/rc.local |
如果没有,则添加:
1 | #!/bin/sh -e#!/bin/bash |
注意:第一行必须是 #!/bin/bash
,否则 执行不了 source /etc/profile
然后赋予权限:
1 | chmod +x /etc/rc.local |
启动 rc-local
服务:
1 | systemctl start rc-local |
可以在 /etc/rc.local 文件中添加开机后自动启动命令:
1 | /usr/local/nginx/sbin/nginx |
内核参数修改
在
/etc/sysctl.conf
中添加如下内容:1
2
3
4
5
6
7
8
9
10
11
12fs.file-max = 10000000
fs.nr_open = 10000000
net.core.default_qdisc = fq
net.ipv4.tcp_mem = 786432 1697152 1945728
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_congestion_control = bbr
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.netfilter.nf_conntrack_max=655350加载新的配置参数
1
sysctl -p
验证 BBR 算法是否生效
1
lsmod | grep bbr
结果会显示 tcp_bbr 加一串数字,说明 BBR 算法已启动。
验证其他修改值是否生效
1
2
3
4cat /proc/sys/net/netfilter/nf_conntrack_max
cat /proc/sys/net/ipv4/tcp_fin_timeout
cat /proc/sys/net/ipv4/tcp_tw_reuse
cat /proc/sys/net/ipv4/tcp_tw_recycle如果输出为修改的值说明已生效
在
/etc/security/limits.conf
中添加如下内容:1
2
3
4
5
6root soft nofile 1000000
root hard nofile 1000000
* soft nofile 1000000
* hard nofile 1000000
* soft nproc 131072
* hard nproc 131072大部分的 Linux 发行版这样修改之后重启即可生效。
验证是否生效:
1
ulimit -n
如果输出为 1000000 则表明已经修改完成。有时发现重启也无效,检查一下
/etc/profile
有一个ulimit -SHn 65536
,把它注释掉就行了。临时修改
1
ulimit -n 1000000
nginx 安装
安装依赖
1
2
3apt-get -y install make gcc g++ zlib1g-dev libpcre3 libpcre3-dev
aptitude -y install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev进入安装目录:
1
cd /usr/local
为了支持 http2,TLSv1.3,下载并解压 openssl 包:
1
2
3wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz
tar -zxvf openssl-1.1.1l.tar.gz下载安装 Brotli 压缩模块,可用于替换 GZIP
1
2
3
4
5git clone https://github.com/eustas/ngx_brotli.git
cd ngx_brotli
git submodule update --init --recursive注意:Brotli 压缩只能在https中生效,因为 在 http 请求中 request header 里的 Accept-Encoding: gzip, deflate 是没有 br 的。
下载并解压 Nginx
1
2
3wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz编译安装 Nginx
1
2
3
4
5cd nginx-1.20.1/
./configure --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --with-openssl=../openssl-1.1.1l --with-openssl-opt=enable-tls1_3 --add-module=../ngx_brotli
make && make install启动之前先参照模板按需配置好配置文件
启动 Nginx
1
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
验证 Nginx 已启动
1
ps -ef | grep nginx
在 /etc/rc.local 中添加
1
/usr/local/nginx/sbin/nginx
定时删除 180 天之前的日志文件:
1
0 2 1 */1 * find /var/log/nginx/ -type f -name '*.log' -mtime +180 -type f -delete
nginx.conf 模板
1 | #user www-data; |
自动构建脚本
定时任务:
1 | */1 * * * * /bin/bash /usr/websync/safety_transfer_formal/auto.sh >> /usr/websync/safety_transfer_formal/update.log |
构建内容:
1 | server_zip_path="/usr/websync/safety_transfer_formal" |
SSH免密
生成密钥
1
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
查看公钥
1
cat ~/.ssh/id_rsa.pub
认证。注意:如果 A 需要SSH免密登陆 B,需要将 A 的公钥写入 B 的 ~/.ssh/authorized_keys 文件中
1
2# 写入公钥
vim ~/.ssh/authorized_keys修改SSH配置
1
2
3vim /etc/ssh/sshd_config
# 允许 root 用户登录:PermitRootLogin yes
# 在AllowUsers 添加 root@10.104.122.26重载配置
1
/etc/init.d/ssh reload
pdsh安装
pdsh 默认不支持 SSH 修改端口。
下载
1
wget https://hub.fastgit.org/chaos/pdsh/releases/download/pdsh-2.34/pdsh-2.34.tar.gz
解压
1
tar -zxvf pdsh-2.34.tar.gz
编译
1
2
3cd pdsh-2.34/
./configure --without-rsh --with-ssh --with-machines=/etc/pdsh/machines --with-dshgroups --with-timeout=10
make && make install测试安装是否成功
1
pdsh -V
设置pdsh主机
1
2
3
4
5vim /etc/pdsh/machines
# 添加主机,例如:
clickhouse1
clickhouse2
clickhouse3测试使用
1
2pdsh -a free -m
pdsh -a "cd /usr/local && ls -l"
如果修改了 SSH 端口,可以修改 /etc/services 中SSH的端口。