2023-12-06    2023-12-26    951 字  2 分钟

当我们安装完操作系统后,通常会做一些初始化的操作才会将服务器投入到生产环境中使用。

在使用云环境时,我们也可以将做完初始化的操作系统封装为镜像,后续通过自己封装的镜像开通服务器,就可以直接投入到生产环境中使用。

环境

1
2
[root@template ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

系统初始化

更新系统上所有的应用程序,有些程序可能存在漏洞,可通过升级解决。但是此操作只推荐在干净的操作系统上执行,已经运行业务的系统不推荐更新所有应用程序。

1
[root@template ~]# yum update

禁用 selinuxfirewalld

1
2
3
[root@template ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@template ~]# systemctl stop firewalld
[root@template ~]# systemctl disable firewalld

安装epel源。

1
[root@template ~]# yum install epel-release -y

安装必要的软件。

1
[root@template ~]# yum install vim wget bash-completion net-tools telnet tree nmap sysstat lrzsz dos2unix rsync git gcc -y

配置时区,我公司使用UTC时区,修改命令如下。

1
[root@template ~]# timedatectl set-timezone UTC

若使用北京时间命令如下。

1
[root@template ~]# ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

时间同步。

1
2
3
[root@template ~]# yum install chrony -y
[root@template ~]# systemctl start chronyd
[root@template ~]# systemctl enable chronyd

文件句柄数配置。修改/etc/security/limits.conf文件,追加如下内容。

1
2
3
4
5
6
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
* hard noproc 65535
* soft noproc 65535

初始化数据盘。

1
2
3
4
5
6
[root@template ~]# fdisk /dev/vdb
[root@template ~]# mkfs.xfs /dev/vdb1
[root@template ~]# mkdir /data
[root@template ~]# echo "/dev/vdb1 /data   xfs     defaults 0 0" >> /etc/fstab
[root@template ~]# mount -a
[root@template ~]# df -h

重启服务器。

1
reboot

系统用户

为了系统的安全性,我们通常不会使用root登录到操作系统,这里我们可以创建一个ec2-user的用户,用此用户管理系统。

添加管理用户,并添加密钥。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
useradd ec2-user
mkdir -p /home/ec2-user/.ssh/
cat >> /home/ec2-user/.ssh/authorized_keys << EOF
# 此处为ec2-user的公钥
ssh-rsa AAAA
EOF
chown ec2-user:ec2-user /home/ec2-user/.ssh
chown ec2-user:ec2-user /home/ec2-user/.ssh/authorized_keys
chmod 700 /home/ec2-user/.ssh
chmod 600 /home/ec2-user/.ssh/*

ec2-user用户授予所有命令的权限。

1
echo "ec2-user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

禁止使用root用户登录系统和禁止使用通过密码登录系统。

修改/etc/ssh/sshd_config文件,具体修改内容如下。

1
2
PasswordAuthentication no
PermitRootLogin no

华为云还需修改/etc/cloud/cloud.cfg文件,将ssh_pwauth的值修改为false。否则用此服务器生成的镜像开通新的服务器后,PasswordAuthentication还会变为yes

重启sshd服务。

安装常用服务

我们使用的是Prometheus监控,主机只需要启动exporter即可。

安装基础监控:点击跳转

安装进程监控:点击跳转

注意事项

创建镜像前务必检查:

  1. 检查SSH用户的信任关系,谨慎选择要注入的SSH公钥。
  2. 是否需要注释掉crontab
  3. 时区配置必须符合业务需求。
  4. 清空历史命令。

image-20231028232834657