Homelab (4): Linux 服务器基础环境准备

本文最后更新于:2 年前

服务器系统安装的是CentOS7, 这篇文章主要记录一下服务器上系统环境与基础软件的准备工作。

安装系统

系统安装的教程很多,这里不多写。

关闭 SELinux

SELinux (Security Enhanced Linux) , 安全加强Linux。可以简单理解为通过一些安全策略,限制访问权限,避免资源误用,在《鸟哥的Linux私房菜》

对 SELinux 有详细的介绍。

不过,这东西虽然安全,设置起来比较麻烦,不是我折腾的方向,且为了避免出现一些奇怪的问题,我直接关了。。。

  • 查看 SELinux 是否开启;

    1
    2
    3
    4
    $ getenforce
    Enforcing
    # Enforcing 开启
    # Disabled 关闭
  • 永久关闭,修改配置文件/etc/selinux/config

    1
    2
    3
    $ vim /etc/selinux/config

    SELINUX=disabled

    服务器重启后生效。

  • 临时关闭,重启后恢复;

    1
    $ setenforce 0

开启防火墙

为了安全,防火墙肯定需要开启的。CentOS7 默认的防火墙软件是 firewalld, 管理命令是 firewall-cmd

这两篇文章对 firewalld 有一些详细的介绍:

日常我们只需要知道这几个命令就够了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 看防火墙是否开启, 如下我这就是默认开启了
$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2021-08-13 01:42:54 CST; 1 weeks 1 days ago
...

# 设置自动开启
$ systemctl enable firewalld

# 开启防火墙
$ systemctl start firewalld
# 关闭防火墙
$ systemctl stop firewalld
# 关闭自启动
$ systemctl disable firewalld

# 查看默认区域的详情
$ firewall-cmd --list-all

# 永久开放端口 8888
$ firewall-cmd --permanent --zone=public -add-port=8888/tcp

# 开启后,reload一下
$ firewall-cmd --reload

设置最大文件打开数

Linux 最大文件打开数是操作系统对一个进程打开的文件句柄梳理的限制,有时我们遇到Can't open so many files类似的错误,一般都是这个有限制。

可以通过 ulimit -n 查看, 一般默认是 1024。

1
2
$ ulimit -n
1024

上面这个是对用户级别限制的,还有一个对系统的总限制的, 通过下面命令可以查看:

1
2
$ cat /proc/sys/fs/file-max
6469558

如下:我们直接把一个进程的最大文件打开数设置到 65535。

1
2
3
4
$ vim /etc/security/limits.conf

* soft nofile 65535
* hard nofile 65535
  • * 表示所有用户;
  • soft nofile 指最大打开的文件数软限制,也就是达到这个数量会告警;
  • hard nofile 指最大打开的文件数硬限制,就是实际的限制。

把非root用户的线程数量的限制也放开。

1
2
3
$ vim /etc/security/limits.d/20-nproc.conf
* soft nproc 65535
root soft nproc unlimited

接着把系统的也调大。

1
2
3
4
5
6
7
# 临时生效
$ vim /proc/sys/fs/file-max
6553560

# 永久生效(重启后生效)
$ vim /etc/sysctl.conf
fs.file-max = 6553560

然而,上面的设置只对通过PAM(插入式认证模块,Pluggable Authentication Modules)登录用户启动的程序才生效,一些 systemctl 运行的后台进程确不起作用,顾需要修改另外一个文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 修改下面这两个文件中的两个配置
$ vim /etc/systemd/system.conf

# 默认最大文件打开数
DefaultLimitNOFILE=65535
# 默认进程最大打开数
DefaultLimitNPROC=65535

$ vim /etc/systemd/user.conf

# 默认最大文件打开数
DefaultLimitNOFILE=65535
# 默认进程最大打开数
DefaultLimitNPROC=65535

改完后,可以重启一下。

如果不想重启系统,可以在需要设置的service文件中设置如下:

1
2
LimitNOFILE=1048576
LimitNPROC=1048576

修改历史执行命令的格式与数量

平常我们运行history看到的是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ history
1004 vim /etc/selinux/config
1005 getenforce
1006 cat /proc/sys/fs/file-max
1007 ulimit -a
1008 systemctl status firewalld.service
1009 firewall-cmd --state
1010 firewall-cmd --get-active-zones
1011 firewall-cmd --list-all
1012 cat /proc/sys/fs/file-max
1013 ulimit -n
1014 vim /etc/security/limits.d/20-nproc.conf
1015 vim /etc/security/limits.conf
1016 vim /etc/systemd/system.conf
1017 history

其实不方便排除,如果加上时间、用户就好了。只要在/etc/profile 加一行代码就好了。

1
2
3
4
5
$ vim /etc/profile
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` "

# 然后 source 一下就好了
$ source /etc/profile

下次再看记录就方便排查了。

安装基础包

1
$ yum install -y vim unzip zip wget gzip zlib zlib-devel lsof gcc-c++ make

安装 docker

为了在软件安装上少花点时间,我准备大部分的软件都用 docker 安装。直接按照 Docker 官方文档安装。

1
2
3
4
5
6
$ yum install -y yum-utils
$ yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
$ yum install docker-ce docker-ce-cli containerd.io
$ systemctl start docker

到这里,我们整个服务器的基础环境就准备好了,后面可以愉快的折腾其他软件了。