一、基本配置
- 1 网络配置
网路配置常用的两种方式为桥接和 NAT 模式。本文采用的是 NAT 模式连接网络。
了解更多网络类型可参照【Web 集群实战】01_VMware 虚拟机常见的网络类型
1)首先,在宿主机 Windows 的计算机管理 - 服务和应用程序 - 服务下, 找到 VMware 的 DHCP 和 NAT 服务,开启。
2)在编辑虚拟机设置 - 网络适配器下,选择 NAT 模式。
3)进入系统后,无需配置 IP、子网掩码和网关,使用 DHCP 自动分配。
4)需要手动配置本地主 DNS 和辅 DNS 解析地址。
5)配置完成后,执行以下命令:
[root@ylt001 ~]# ifup ens33 // 启动网卡
[root@ylt001 ~]# ifconfig ens33 // 查看获取的 IP
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.129 netmask 255.255.255.0 broadcast 192.168.2.255
inet6 fe80::8872:f163:9578:9499 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:17:52:10 txqueuelen 1000 (Ethernet)
RX packets 124 bytes 11626 (11.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 94 bytes 11853 (11.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.2 更新系统,打补丁到最新
- CentOS 安装完毕后,首先应该修改更新源,并升级到最新的系统。修改 yum 更新源的命令如下:
[root@ylt001 ~]# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[root@ylt001 ~]# wget /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
2)然后,使用如下命令将系统更新到最新状态:
[root@ylt001 ~]# ll /etc/pki/rpm-gpg/
total 12
-rw-r--r--. 1 root root 1690 Aug 30 2017 RPM-GPG-KEY-CentOS-7
-rw-r--r--. 1 root root 1004 Aug 30 2017 RPM-GPG-KEY-CentOS-Debug-7
-rw-r--r--. 1 root root 1690 Aug 30 2017 RPM-GPG-KEY-CentOS-Testing-7
[root@ylt001 ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
[root@ylt001 ~]# yum update -y
1.3 额外安装一些有用的软件包
[root@ylt001 ~]# yum install -y tree telnet dos2unix sysstat lrzsz nc nmap
安装软件包组:
[root@ylt001 ~]# yum grouplist // 查看所有包组名称,包括已安装的和未安装的
[root@ylt001 ~]# yum groupinstall "Development Tools"
二、调优
2.1 关闭 SELinux 功能
[root@ylt001 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config // 修改配置文件可使配置永久生效,需重启系统
2.2 设定运行级别为 3 (文本模式)
了解运行级别,可参考【Web 集群实战】04_Linux 的 7 种运行级别 & Linux 系统从开机到登录之前的启动流程
[root@ylt001 ~]# runlevel // 查看当前系统运行级别
[root@ylt001 ~]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
multi-user.target 类似于之前的 runlevel 3
graphical.target 类似于之前的 runlevel 5
[root@ylt001 ~]# systemctl get-default // 查看当前系统默认 target
[root@ylt001 ~]# systemctl set-default multi-user.target
[root@ylt001 ~]# systemctl get-default
2.3 精简开机系统自启动服务
建议保留的开启自启动服务有 crond、sshd、rsyslog、network、sysstat
[root@ylt001 ~]# systemctl list-unit-files | grep enable // 过滤查看启动项
[root@ylt001 ~]# systemctl disable firewalld.service // 关闭开机自启
2.4 关闭防火墙
[root@ylt001 ~]# systemctl stop firewalld // 临时关闭
[root@ylt001 ~]# systemctl disable firewalld.service // 关闭开机自启
2.5 更改 SSH 服务器端远程登录的配置
[root@ylt001 ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
[root@ylt001 ~]# vi /etc/ssh/sshd_config
Port 54321 // 更改默认端口
PermitRootLogin no // 是否允许 root 登录
PermitEmptyPasswords no // 是否允许密码为空的用户远程登录
UseDNS no // 指定 sshd 是否应该对远程主机名进行反向解析,以检查此主机名是否与其 IP 地址真是对应。
GSSAPIAuthentication no // 解决 Linux 之间使用 SSH 远程连接慢的问题
将以上信息更改后,保存退出。
执行如下命令重启 sshd,使修改的配置生效:
[root@ylt001 ~]# systemctl reload sshd
2.6 利用 sudo 控制用户对系统命令的使用权限
编辑 sudo 的配置文件:
[root@ylt001 ~]# visudo // 相当于直接编辑 /etc/sudoers
root ALL=(ALL) ALL
ylt ALL=(ALL) NOPASSWD: ALL // 结尾的 ALL 表示 ylt 可拥有完全的系统管理权限,NOPASSWD 表示提权执行命令是不提示密码
配置完成后检查:
[root@ylt001 ~]# grep ylt /etc/sudoers
ylt ALL=(ALL) NOPASSWD: ALL
注:
1)通过 sudo 授权后,所有用户执行授权的特殊权限格式为“sudo 命令”
2)如果需要切换到 root 执行相关操作,可以通过 “sudo su -”
3)root 切换到普通用户为“su - ylt”
4)对于 Linux 系统 bash 的内置命令,一般无法进行 sudo 授权。
2.7 Linux 中文显示设置
[root@ylt001 ~]# touch /etc/sysconfig/i18n
[root@ylt001 ~]# vi /etc/sysconfig/i18n
LANG="zh_CN.UTF-8"
[root@ylt001 ~]# source /etc/sysconfig/i18n // 使修改生效
这个中文显示设置需要和客户端设置一致,以 putty 为例说明。
1)输入 IP 和 端口
2)输入用户名和密码,下次则无需输入
3)设置 UTF-8 字符集
4)保存 Session
2.8 设置 Linux 服务器时间同步
Linux 系统的时间同步服务为 ntp 服务。
可以手动同步互联网时间到本地 Linux 主机,命令如下:
[root@ylt001 ~]# /usr/sbin/ntpdate ntp1.aliyun.com
利用定时任务 crond 把上述的命令每 5 分钟 自动执行一次,命令如下:
[root@ylt001 ~]# echo '#time sync by ylt001 at 2018-09-16' >>/var/spool/cron/root
[root@ylt001 ~]# echo '*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2&>1' >> /var/spool/cron/root
[root@ylt001 ~]# crontab -l
但是通过上述命令得到的是 EDT,需要改成 CST。
[root@ylt001 ylt]# date
Mon Sep 17 02:36:03 EDT 2018
注:
EDT:指美国东部夏令时间,波士顿、纽约市、华盛顿哥伦比亚特区,都在这个时区内,跟北京时间有 12 小时的时差,晚 12 小时。
CST:可以指下面两种:
1)美国中部标准时间(西六区,-6:00),中国是东八区(+8:00),北京时间比美国中部标准时间早 14 个小时。3:45 PM CST 是北京时间凌晨 1:45。
2)中澳大利亚标准时间(+10:30),中国是东八区(+8:00),北京时间比中澳大利亚标准时间晚两个半小时。3:45 PM CST 是北京时间下午 6:15。
[root@ylt001 ylt]# mv /etc/localtime /etc/localtime.bak
[root@ylt001 ylt]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@ylt001 ylt]# date
Mon Sep 17 14:38:16 CST 2018
完成上述步骤后,可将此虚拟机作为模板,做一个克隆机备用。