vlambda博客
学习文章列表

CentOS虚拟机的基本配置

1.设置虚拟机的主机名

1.1.查看主机名

# 通过hostname命令查看主机名
[root@~]# hostname

1.2.临时修改主机名

可以通过命令hostname 主机名来临时修改主机名,当主机重启后,主机名会自动改成原来的主机名。

[root@~]# hostname myhostname

1.3.永久修改主机名

如果希望永久修改主机名(即重启也不会改变),有两种方法。

  • 方法一:使用hostnamectl命令

[root@~]# hostnamectl set-hostname myhostname
  • 方法二:修改主机名文件 除了方法一,还可以通过修改主机名文件(/etc/hostname)来永久修改主机名

[root@~]# vi /etc/hostname

myhostname

2.虚拟机的网络配置

2.1.桥接模式

2.1.1.设置虚拟机的网络连接模式为桥接


2.1.2.查看宿主机网络信息

2.1.3.配置虚拟机网络

# 编辑 /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static" # 设置静态分配
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="38bb92d4-a1de-481e-ab20-86bd391fe991"
DEVICE="ens33"
ONBOOT="yes" # 开机启动
IPADDR=192.168.0.161 # 设置静态IP
NETMASK=255.255.255.0 # 设置网络掩码
GATEWAY=192.168.0.1 # 设置网关,与宿主机相同
DNS1=192.168.0.1 # 设置域名解析,与宿主机相同
DNS2=192.168.1.1

2.1.4.重启网络

$ systemctl restart network

3.配置主机名与IP地址映射

$ vim /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.161 node1
192.168.0.162 node2
192.168.0.163 node3
192.168.0.164 node4
192.168.0.165 node5

3.设置虚拟机防火墙

3.1.关闭虚拟机防火墙

# 查看防火墙状态
$ systemctl status firewalld
# 禁用防火墙
$ systemctl disable firewalld
# 立即停用防火墙
$ systemctl stop firewalld

3.2.禁用selinux

# 禁用selunix
$ vim /etc/selinux/conf

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled # 禁用
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

4.配置虚拟机间的ssh免密登录

# 在node1节点上执行
$ ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:2T4C3iUCXwWeO4FxE80Y0MktXDz2PgwBWR1qsDgo+9A root@node1
The key's randomart image is:
+---[RSA 2048]----+
| o=O/+... |
| . *O*O.. |
| . o + *oo+ |
| + o o *. . |
| o E + S o+ |
| o . + = + |
| . . o o . |
| . . |
| |
+----[SHA256]-----+


# 生成的公钥id_rsa.pub和私钥id_rsa
$ ll ~/.ssh/
ll ~/.ssh/
total 8
-rw------- 1 root root 1675 Apr 16 18:08 id_rsa
-rw-r--r-- 1 root root 392 Apr 16 18:08 id_rsa.pub

# 将公钥拷贝到当前机器上
$ ssh-copy-id node1
# 将node1生成的公钥拷贝到其它机器上
$ ssh-copy-id node2
$ ssh-copy-id node3
$ ssh-copy-id node4
$ ssh-copy-id node5

5.虚拟机集群时间同步

Linux的时间同步分为System Clock(系统时间)和Real Time Clock(硬件时间,简称RTC)。

  • 系统时间:指当前Linux Kernel中的时间。

  • 硬件时间:主板上有电池供电的时间。

5.1.查看与设置系统时间

# 查看系统时间的命令
$ date
# 设置系统时间的命令(月/日/年 时:分:秒)
$ date -set "MM/dd/yy HH:mm:ss"

5.2.查看与设置硬件时间

# 查看硬件时间的命令
$ hwclock
# 设置硬件时间的命令(月/日/年 时:分:秒)
$ hwclock -set -date = "MM/dd/yy HH:mm:SS"

上面是手动设置时间到一个时间点,可能与当前网络的时间有误差。下面介绍与时间服务器上的时间进行同步的方法。

5.3.与时间服务器时间同步

5.3.1.安装时间同步插件

# 检查是否有时间同步插件
$ rpm -qa|grep ntp
# 如果没有就安装
$ yum -y install ntp ntpdate
# 启动
$ systemctl start ntpd
# 设置开机自启动
$ systemctl enable ntpd

5.3.2.设置系统时间与网络时间同步

$ ntpdate cn.pool.ntp.org

5.3.3.将系统时间写入硬件时间

$ hwclock --systohc

5.3.4.强制将系统时间写入COMS中防止重启失效

# 方法一:
$ hwclock -w
# 方法二:
$ clock -w

5.4.网络时间同步,并作定时同步任务

5.4.1. 修改时区

$ rm -rf /etc/localtime
$ ln -s /usr/share/zoneinfo/Asis/Shanghai /etc/localtime
$ vim /etc/sysconfig/clock

ZONE = "Asia/Shanghai"
UTC = false
ARC = false

5.4.2.安装并设置开机启动

$ yum -y install ntp ntpdate
$ systemctl start ntpd
$ systemctl enable ntpd

5.4.3.配置开机启动检验

# vim /etc/rc.d/rc.local

/usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1; /sbin/hwclock -w

5.4.4.配置定时任务

$ crontab -e 0 */1 * * * ntpdate ntp1.aliyun.com > /dev/null 2>&1; /sbin/hwclock -w