CentOS7
CentOS8
1. root 登录
2. 创建新用户
# 创建用户
adduser zhx
# 为用户设置密码
passwd zhx
# 输入密码并确认
3. 授予管理权限
# 设置用户权限 可以使用 sudo
usermod -aG wheel zhx
4. 设置基本的防火墙
说明:有些虚拟机平台,平台自己配置了外网的防火墙规则,需要自己手动在平台上修改,需要注意。
4.1 安装防火墙
# 安装防火墙
dnf install firewalld -y
# 防火墙默认允许 ssh 连接, 开启防火墙
systemctl start firewalld
# 检查防火墙状态
systemctl status firewalld
# 设置开机自启
systemctl enable firewalld
# 再查询状态
systemctl status firewalld
说明:图中标记的enable 和 active,表明它既是激活的,也是启用的,这意味着如果服务器重新启动,它将默认启动。
4.2 获取和设置防火墙的策略信息
# 让我们列出已经允许的服务
firewall-cmd --permanent --list-all
# 查看可以通过名称启用的其他服务
firewall-cmd --get-services
# 要添加一个应该被允许的服务,添加http服务,并允许传入的TCP通信流到端口80
firewall-cmd --permanent --add-service=http
# 重新加载防火墙,使设置生效
firewall-cmd --reload
5. 为常规用户启用外部访问
# 密码验证登录的情况,使用新用户 ssh 登录
ssh sammy@your_server_ip
6. 禁止 root 远程登录,修改ssh连接端口
6.1 修改ssh配置文件
# 修改ssh配置文件sshd_config
vi /etc/ssh/sshd_config
说明:Port 默认22,PermitRootLogin 默认yes
6.2 将自定义端口SSH服务添加到firewalld并重新加载
# 复制一份配置文件
cp -av /usr/lib/firewalld/services/ssh.xml /usr/lib/firewalld/services/ssh-custom.xml
# 修改配置
vi /usr/lib/firewalld/services/ssh-custom.xml
“port”=”22222”
# 删除原有ssh 22 port
firewall-cmd --permanent --remove-service='ssh'
# 启用自定义配置 22222 port
firewall-cmd --permanent --add-service='ssh-custom'
# 重新加载防火墙
firewall-cmd --reload
# 查看防火墙配置
firewall-cmd --list-all
6.3 重新启动SSH服务,检查SSH服务是否已经开始监听新端口
# 查询 ssh 监听端口
netstat -plunt | grep sshd
# 查询ssh服务状态
systemctl status sshd
# 重启ssh 服务
systemctl restart sshd
# 查询 ssh 监听端口
netstat -plunt | grep sshd
# 查询ssh服务状态
systemctl status sshd