CentOS7

CentOS7初始化

CentOS8

CentOS8初始化

1. root 登录

2. 创建新用户

  1. # 创建用户
  2. adduser zhx
  3. # 为用户设置密码
  4. passwd zhx
  5. # 输入密码并确认

3. 授予管理权限

  1. # 设置用户权限 可以使用 sudo
  2. usermod -aG wheel zhx

4. 设置基本的防火墙

说明:有些虚拟机平台,平台自己配置了外网的防火墙规则,需要自己手动在平台上修改,需要注意。

4.1 安装防火墙

  1. # 安装防火墙
  2. dnf install firewalld -y
  3. # 防火墙默认允许 ssh 连接, 开启防火墙
  4. systemctl start firewalld
  5. # 检查防火墙状态
  6. systemctl status firewalld

image.png

  1. # 设置开机自启
  2. systemctl enable firewalld
  3. # 再查询状态
  4. systemctl status firewalld

image.png
说明:图中标记的enable 和 active,表明它既是激活的,也是启用的,这意味着如果服务器重新启动,它将默认启动。

4.2 获取和设置防火墙的策略信息

  1. # 让我们列出已经允许的服务
  2. firewall-cmd --permanent --list-all

image.png

  1. # 查看可以通过名称启用的其他服务
  2. firewall-cmd --get-services

image.png

  1. # 要添加一个应该被允许的服务,添加http服务,并允许传入的TCP通信流到端口80
  2. firewall-cmd --permanent --add-service=http
  3. # 重新加载防火墙,使设置生效
  4. firewall-cmd --reload

image.png

5. 为常规用户启用外部访问

  1. # 密码验证登录的情况,使用新用户 ssh 登录
  2. ssh sammy@your_server_ip

6. 禁止 root 远程登录,修改ssh连接端口

配置文档
配置 ssh

6.1 修改ssh配置文件

  1. # 修改ssh配置文件sshd_config
  2. vi /etc/ssh/sshd_config

image.png
说明:Port 默认22,PermitRootLogin 默认yes

6.2 将自定义端口SSH服务添加到firewalld并重新加载

  1. # 复制一份配置文件
  2. cp -av /usr/lib/firewalld/services/ssh.xml /usr/lib/firewalld/services/ssh-custom.xml
  3. # 修改配置
  4. vi /usr/lib/firewalld/services/ssh-custom.xml

image.png
image.png
“port”=”22222”

  1. # 删除原有ssh 22 port
  2. firewall-cmd --permanent --remove-service='ssh'
  3. # 启用自定义配置 22222 port
  4. firewall-cmd --permanent --add-service='ssh-custom'
  5. # 重新加载防火墙
  6. firewall-cmd --reload
  7. # 查看防火墙配置
  8. firewall-cmd --list-all

image.png

6.3 重新启动SSH服务,检查SSH服务是否已经开始监听新端口

  1. # 查询 ssh 监听端口
  2. netstat -plunt | grep sshd
  3. # 查询ssh服务状态
  4. systemctl status sshd
  5. # 重启ssh 服务
  6. systemctl restart sshd
  7. # 查询 ssh 监听端口
  8. netstat -plunt | grep sshd
  9. # 查询ssh服务状态
  10. systemctl status sshd

image.png