Luke 发布的文章

1 背景

实验室服务器经常有人提交大量任务,导致内存占满后系统卡死,不得不重启。通过使用earlyoom在内存耗尽前查杀大内存进程,避免系统崩溃。

2 做法

1、安装earlyoom

sudo apt install earlyoom

2、编辑配置文件

sudo vim /etc/default/earlyoom

将其中Available minimum memory部分修改为EARLYOOM_ARGS="-m 6,5 -s 100,100",表示内存余量低于5%就查杀大内存进程,同时忽略swap的占用情况。

# Default settings for earlyoom. This file is sourced by /bin/sh from
# /etc/init.d/earlyoom or by systemd from earlyoom.service.

# Options to pass to earlyoom
EARLYOOM_ARGS="-r 3600"

# Examples:

# Print memory report every second instead of every minute
# EARLYOOM_ARGS="-r 1"

# Available minimum memory 5%
# EARLYOOM_ARGS="-m 5"
EARLYOOM_ARGS="-m 6,5 -s 100,100"
# Available minimum memory 15% and free minimum swap 5%
# EARLYOOM_ARGS="-m 15 -s 5"

# Avoid killing processes whose name matches this regexp
# EARLYOOM_ARGS="--avoid '(^|/)(init|X|sshd|firefox)$'"

# See more at `earlyoom -h'
3、重启服务

sudo systemctl restart earlyoom

## 注意

1 背景

服务器经常遇到恶意爆破。使用fail2ban自动封禁多次密码输入错误的IP,降低服务器被攻破的风险。

2 做法

1、安装fail2ban。

sudo apt update
sudo apt install fail2ban

2、配置fail2ban

sudo vim /etc/fail2ban/jail.local

然后输入以下内容:

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 10  # findtime时间内最大尝试次数
bantime = 86400  # 超过最大尝试次数后封禁时间,以秒为单位
findtime = 600  # 设置监测时间,以秒为单位。600表示统计连续10分钟内密码输入错误的次数

3、重启fail2ban

sudo systemctl restart fail2ban

4、查看状态

sudo fail2ban-client status sshd

注意

如果不正正常运行,执行以下命令

sudo apt-get update && sudo apt-get install -y rsyslog  # Debian/Ubuntu
sudo systemctl enable --now rsyslog
# 等 1~2 分钟让日志开始写入,再继续

然后再次重启fail2ban即可恢复正常。

1 背景

ssh服务默认端口为22,经常被攻击。通过修改端口,一定程度上减轻对服务器被攻破的风险。

2 做法

1、运行以下命令备份sshd服务配置文件。

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config_bak

2、修改sshd服务的端口号。
运行以下命令编辑sshd_config配置文件。

sudo vim /etc/ssh/sshd_config

将其中的Port 22注释掉,改为Port 1234
3、重启ssh服务。

sudo systemctl restart sshd

3 注意

为了防止意外,务必在确认能够使用新端口连接后,再关闭当前的远程连接,以免造成无法远程连接的意外。

1、安装 libpam-google-authenticator

sudo apt update
sudo apt install libpam-google-authenticator -y

2、为每个用户生成 TOTP 密钥

google-authenticator

3、回答显示的问题,默认y就可以。
4、使用微软或者谷歌验证器扫码。
5、拍照记录应急验证码。
6、配置 PAM 模块启用 TOTP

sudo nano /etc/pam.d/sshd

在文件最上方添加以下内容:

auth required pam_google_authenticator.so nullok

如果必须使用两步验证,则:

auth required pam_google_authenticator.so

7、编辑ssh配置。

sudo nano /etc/ssh/sshd_config

修改以下内容:

ChallengeResponseAuthentication yes
UsePAM yes
PasswordAuthentication yes  # 如使用公钥登录可为 no
KbdInteractiveAuthentication yes  # 这一行必不可少,否则不能使用xshell,termius等

8、重启ssh服务,使上述配置生效。

sudo systemctl restart ssh

背景

screen是常用的会话管理工具,然而已经过时,在RHEL中甚至不再提供。tmux是更现代化的会话管理工具,因此决定从screen迁往tmux。下面记录基本的常用操作。

常用操作

1、新建会话

tmux new -s session_name

2、开始记录日志

script -f file_name

3、断开会话

先按ctrl+b,然后再按d

4、返回会话

tmux attach -t session_name

5、查看已经存在的会话

tmux ls

6、向上翻阅日志

先按ctrl+b,然后按[

7、取消翻阅日志

按下ESC键或者q键

8、tmux前缀

ctrl+b

9、上下切分窗格

先按前缀键,然后再按引号

10、左右切分窗格

先按前缀键,再按百分号键

11、退出窗格

先按前缀键,然后x
或者直接ctrl+D

12、退出窗口

先按前缀键,然后再按&
或者直接ctrl+D