安装版本

工作中经常使用 CentOS 7.6-1810 版本(推荐 CentOS-7-x86_64-Everything-1810.iso),开机后常常会设置一些基本配置,这里统一记录下来,方便后续环境部署简单化。

基本配置

#修改历史命令个数
sed -i 's/^HISTSIZE=200/HISTSIZE=/1000' /etc/profile
source /etc/profile

#关闭 SELinux(Security-Enhanced Linux) 强制访问控制
getenforce
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#关闭网络管理工具(有时候会导致 网卡配置启动失败的问题 )
systemctl stop NetworkManager
systemctl disable NetworkManager
systemctl status NetworkManager
之所以要关闭 `NetworkManager` 是因为CentOS系统上,目前有 NetworkManager 和 network 两种网络管理工具。如果两种都配置会引起冲突,而且NetworkManager在网络断开的时候,会清理路由,如果一些自定义的路由,没有加入到NetworkManager的配置文件中,路由就被清理掉,网络连接后需要自定义添加上去。
鉴于目前在CentOS上的 NetworkManager 版本比较低(但是比较适合有桌面环境的系统)。
建议服务器上保留network服务即可,将 NetworkManager关闭,并且禁止开机启动。




#关闭防火墙
systemctl stop firewalld.service 
systemctl disable firewalld.service 
systemctl status firewalld.service 

# SSH 相关配置(解决SSH 访问慢,长时间会断连接)
sed -i 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/^#UseDNS yes/UseDNS=no/'                            /etc/ssh/sshd_config
sed -i 's/^#ClientAliveInterval 0/ClientAliveInterval 300/'   /etc/ssh/sshd_config
sed -i 's/^#ClientAliveCountMax 3/ClientAliveCountMax 10/'    /etc/ssh/sshd_config

systemctl restart sshd

静态IP配置

通常在 Windows 下面,使用 Vmware 会配置两张网卡,配置两个IP地址。

centos 开启NetworkManager 如何启动network centos开机启动networkmanager_sed

1 修改配置文件

为了连接和使用方便,通常会配置成静态 IP 地址。具体操作过程为:首先进入到 /etc/sysconfig/network-scripts/ 目录下,修改网卡配置(如:ifcfg-ens33)

BOOTPROTO=static       #设置网卡获得IP地址的方式,可能的选项为static,dhcp或bootp;
IPADDR=12.168.0.XX     #如果设置网卡获得 IP地址的方式为静态指定,此字段就指定了网卡对应的IP地址;
NETMASK=255.255.255.0  #网卡对应的网络掩码

NETWORK=192.168.0.0    #网卡对应的网络地址

DEVICE=ens34           #描述网卡对应的设备别名
ONBOOT=yes             #设置网卡启动方式为“开机启动”“并且可以通过系统服务管理器 systemctl 控制网卡

若要配置两个IP 地址,可以按照如下方式添加:

BOOTPROTO=static         #设置网卡获得IP地址的方式,可能的选项为static,dhcp或bootp;
IPADDR1=12.168.0.XX1     #指定网卡对应的IP1地址;
NETMASK1=255.255.255.0   #网卡对应的IP地址1掩码;

IPADDR2=12.168.0.XX2     #指定网卡对应的IP2地址;
NETMASK2=255.255.255.0   #网卡对应的IP地址2掩码;

2 修改network

修改 /etc/sysconfig/network 配置。

# Created by anaconda
NETWORKING=yes
GATEWAY=192.168.0.1

3 修改DNS(如果需要)

修改 /etc/resolv.conf 文件:

; generated by /usr/sbin/dhclient-script
nameserver 192.168.0.1
search localdomain

重启网络

执行下面几个命令皆可:

service network restart 
/etc/init.d/network restart
systemctl restart network

脚本执行

也可以直接将上述内容,拷贝到一个 shell 脚本中,然后一键执行更加方便快捷。

#!/bin/sh
# CentOS 7.6 init deploy shell script

#修改历史命令个数
sed -i 's/^HISTSIZE=200/HISTSIZE=1000/' /etc/profile
source /etc/profile

#关闭 SELinux(Security-Enhanced Linux) 强制访问控制
getenforce
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#关闭网络管理工具(有时候会导致 网卡配置启动失败的问题 )
systemctl stop NetworkManager
systemctl disable NetworkManager
systemctl status NetworkManager

#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service

# SSH 相关配置(解决SSH 访问慢,长时间会断连接)
sed -i 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/^#UseDNS yes/UseDNS=no/'                            /etc/ssh/sshd_config
sed -i 's/^#ClientAliveInterval 0/ClientAliveInterval 300/'   /etc/ssh/sshd_config
sed -i 's/^#ClientAliveCountMax 3/ClientAliveCountMax 10/'    /etc/ssh/sshd_config
systemctl restart sshd