Ubuntu server的防火墙配置问题,包括新用户添加、防火墙规则设定以及ssh的简单设置,写得比较简单,慢慢理解以后来填坑。主要的references见文末,另外说一句 Merry Xmas!

Change or update root pwd

修改或者初始化root的密码,如下

1
2
3
sudo passwd root
New password: xxxx
Retype new password: xxxx

Add a new user

Ubuntu server在安装好以后只有Root用户,为了方便管理,通常需要添加一个用户以方便管理,并且为了服务器的安全性,需要配置ssh的key only和防火墙。

1
2
3
4
5
6
7
8
9
10
11
sudo apt update # optional
sudo apt upgrade # optional
sudo apt install vim # install vim, optional
sudo adduser jason # Add a new user
sudo usermod -G root jason # Add user jason into Group root
# Add sudoer authority
sudo chmod u+w /etc/sudoers # Change sudoers from readonly to writtable
sudo vim /etc/sudoers
# Add the line below root ALL=(ALL:ALL) ALL
jason ALL=(ALL:ALL) ALL
sudo chmod u-w /etc/sudoers # Rechange mode of sudoers to readonly

SSH confiurations

ssh配置方便远程访问server,主要的配置包括端口修改、aurhorized_keys以及一些有助于安全性的配置。

1. 生成本机的Authentication key-pair
1
ssh-keygen -b 4096

选择默认的id_rsaid_rsa.pub存储密钥对,并且选择不设定密码。

2. 添加该用户的public key到server中
1
2
3
4
5
6
mkdir ~/.ssh
cd ~/.ssh
touch authorized_keys
vim authorized_keys
# Add your public key into this file like
ssh-rsa xxxxx user@xxx
3. 配置ssh端口及相应的安全选项

sshd的配置文件存放在/etc/ssh/sshd_config中,

1
2
3
4
5
6
7
vim /etc/ssh/sshd_config
# change port
Port 2333 # 默认的端口号为22,此处建议修改为其他值
# 禁止root登录
PermitRootLogin no
# 禁止密码验证
PasswordAuthentication no

4. 重启ssh服务

配置完成后,重启sshd服务,如下

1
sudo systemctl restart sshd

UFW configuration

详细的UFW配置参考这篇文章,下面列出我的配置并对解释。

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt install ufw
# allow all outgoing and deny all incoming
sudo ufw default allow outgoing
sudo ufw default deny incoming
# allow ssh
sudo ufw allow ssh
# allow tcp udp http
sudo ufw allow 80/tcp
sudo ufw allow http/tcp
sudo ufw allow 1725/udp
# enable ufw
sudo ufw enable

sudo ufw status查看ufw的运行状态和配置结果,若显示如下列表,则说明配置好了,

1
2
3
4
5
6
7
8
9
10
Status: active
To Action From
-- ------ ----
80/tcp ALLOW Anywhere
1725/udp ALLOW Anywhere
2333 ALLOW Anywhere
80/tcp (v6) ALLOW Anywhere (v6)
1725/udp (v6) ALLOW Anywhere (v6)
2333 (v6) ALLOW Anywhere (v6)

References