虚拟机配置 准备一台虚拟机,本教程是以vagrant+vbox进行构建,默认用户名密码均为vagrant,下面开始设置系统,大家也可以自行安装系统。
一、 关闭防火墙、并设置开机不启动防火墙 script 1 2 sudo systemctl stop firewalld sudo systemctl disable firewalld
二、 开启远程访问 script 1 2 sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config sudo systemctl restart sshd
三、 永久关闭selinux 修改/etc/sysconfig/selinux文件设置
script 1 2 3 sudo sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
四、 禁用swap交换分区 打开/etc/fstab注释掉swap
script 1 2 3 sudo sed -i 's/.*swap.*/#&/' /etc/fstab sudo swapoff -a && swapon -a sudo sysctl -p
五、 安装wget 提示Complete! 则安装成功
script 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 [root@localhost ~]# sudo yum install -y wget Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.ustc.edu.cn Resolving Dependencies --> Running transaction check ---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================================================== Installing: wget x86_64 1.14-18.el7_6.1 base 547 k Transaction Summary =================================================================================================================================================================================================================== Install 1 Package Total download size: 547 k Installed size: 2.0 M Downloading packages: wget-1.14-18.el7_6.1.x86_64.rpm | 547 kB 00:00:04 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : wget-1.14-18.el7_6.1.x86_64 1/1 Verifying : wget-1.14-18.el7_6.1.x86_64 1/1 Installed: wget.x86_64 0:1.14-18.el7_6.1 Complete! [root@localhost ~]#
六、 更改系统默认镜像源为阿里镜像源
切换到/etc/yum.repos.d/下查看当前镜像源
script 1 2 3 4 5 6 7 8 9 10 11 [root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# ll total 32 -rw-r--r--. 1 root root 1664 Nov 23 2018 CentOS-Base.repo -rw-r--r--. 1 root root 1309 Nov 23 2018 CentOS-CR.repo -rw-r--r--. 1 root root 649 Nov 23 2018 CentOS-Debuginfo.repo -rw-r--r--. 1 root root 314 Nov 23 2018 CentOS-fasttrack.repo -rw-r--r--. 1 root root 630 Nov 23 2018 CentOS-Media.repo -rw-r--r--. 1 root root 1331 Nov 23 2018 CentOS-Sources.repo -rw-r--r--. 1 root root 5701 Nov 23 2018 CentOS-Vault.repo [root@localhost ~]#
切换到/etc/yum.repos.d/目录下创建backup 文件夹,并将上述镜像源移动到backup 文件夹下
script 1 2 3 4 5 [root@localhost yum.repos.d]# sudo mkdir /etc/yum.repos.d/backup && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup [root@localhost yum.repos.d]# ll total 0 drwxr-xr-x. 2 root root 187 Jun 4 04:48 backup [root@localhost yum.repos.d]#
拉取阿里云镜像源,看见saved 则拉取成功
script 1 2 3 4 5 6 7 8 9 10 11 12 [root@localhost yum.repos.d]# sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo --2020-06-04 04:48:58-- http://mirrors.aliyun.com/repo/Centos-7.repo Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 124.200.113.116, 124.200.113.115, 219.238.20.87, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|124.200.113.116|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2523 (2.5K) [application/octet-stream] Saving to: '/etc/yum.repos.d/CentOS-Base.repo' 100% [=========================================================================================================================================================================>] 2,523 --.-K/s in 0s 2020-06-04 04:48:58 (264 MB/s) - '/etc/yum.repos.d/CentOS-Base.repo' saved [2523/2523] [root@localhost yum.repos.d]#
查看目录结构
script 1 2 3 4 5 [root@localhost yum.repos.d]# ll total 4 drwxr-xr-x. 2 root root 187 Jun 4 04:48 backup -rw-r--r--. 1 root root 2523 Jun 15 2018 CentOS-Base.repo [root@localhost yum.repos.d]#
执行yum clean all清空镜像源缓存
script 1 2 3 4 5 [root@localhost yum.repos.d]# sudo yum clean all Loaded plugins: fastestmirror Cleaning repos: base extras updates Cleaning up list of fastest mirrors [root@localhost yum.repos.d]#
执行yum makecache重新加载缓存,看到Metadata Cache Created 则加载成功
script 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@localhost yum.repos.d]# sudo yum makecache Loaded plugins: fastestmirror Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/10): base/7/x86_64/group_gz | 153 kB 00:00:00 (2/10): extras/7/x86_64/filelists_db | 205 kB 00:00:00 (3/10): extras/7/x86_64/other_db | 122 kB 00:00:00 (4/10): base/7/x86_64/other_db | 2.6 MB 00:00:00 (5/10): updates/7/x86_64/primary_db | 1.3 MB 00:00:00 (6/10): updates/7/x86_64/filelists_db | 997 kB 00:00:00 (7/10): updates/7/x86_64/other_db | 192 kB 00:00:00 (8/10): base/7/x86_64/primary_db | 6.1 MB 00:00:01 (9/10): extras/7/x86_64/primary_db | 194 kB 00:00:00 (10/10): base/7/x86_64/filelists_db | 7.1 MB 00:00:02 Metadata Cache Created [root@localhost yum.repos.d]#
七、 查看服务器IP地址(非必须) 我个人的是在eth1下,对应的是192.168.1.40
script 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0 valid_lft 77402sec preferred_lft 77402sec inet6 fe80::5054:ff:fe8a:fee6/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:dd:2a:77 brd ff:ff:ff:ff:ff:ff inet 192.168.1.40/24 brd 192.168.1.255 scope global noprefixroute eth1 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fedd:2a77/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]#
八、 自定义服务器域名(非必须) 也可以手动修改vi /etc/hostname(需要重启)
script 1 2 3 4 [root@localhost ~]# hostname xincan.base.com [root@localhost ~]# hostname xincan.base.com [root@localhost ~]#
九、 添加hosts自定义域名映射(非必须)
修改hosts配置
script 1 2 3 4 5 [root@localhost ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.40 xincan.base.com [root@localhost ~]#
查看hosts配置
script 1 2 3 4 5 [root@localhost ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.40 xincan.base.com [root@localhost ~]#
十、 同步阿里云时间(墙裂建议)
安装ntp,提示Complete! 则安装成功
script 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 [root@localhost ~]# sudo yum install -y ntp Loaded plugins: fastestmirror Determining fastest mirrors * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.ustc.edu.cn base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): base/7/x86_64/group_gz | 153 kB 00:00:00 (2/4): extras/7/x86_64/primary_db | 194 kB 00:00:00 (3/4): updates/7/x86_64/primary_db | 2.1 MB 00:00:01 (4/4): base/7/x86_64/primary_db | 6.1 MB 00:00:05 Resolving Dependencies --> Running transaction check ---> Package ntp.x86_64 0:4.2.6p5-29.el7.centos will be installed --> Processing Dependency: ntpdate = 4.2.6p5-29.el7.centos for package: ntp-4.2.6p5-29.el7.centos.x86_64 --> Processing Dependency: libopts.so.25()(64bit) for package: ntp-4.2.6p5-29.el7.centos.x86_64 --> Running transaction check ---> Package autogen-libopts.x86_64 0:5.18-5.el7 will be installed ---> Package ntpdate.x86_64 0:4.2.6p5-29.el7.centos will be installed --> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================================== Package Arch Version Repository Size ===================================================================================================================================================== Installing: ntp x86_64 4.2.6p5-29.el7.centos base 548 k Installing for dependencies: autogen-libopts x86_64 5.18-5.el7 base 66 k ntpdate x86_64 4.2.6p5-29.el7.centos base 86 k Transaction Summary ===================================================================================================================================================== Install 1 Package (+2 Dependent packages) Total download size: 701 k Installed size: 1.6 M Downloading packages: warning: /var/cache/yum/x86_64/7/base/packages/ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY- ETA Public key for ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm is not installed (1/3): ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm | 86 kB 00:00:00 (2/3): autogen-libopts-5.18-5.el7.x86_64.rpm | 66 kB 00:00:00 (3/3): ntp-4.2.6p5-29.el7.centos.x86_64.rpm | 548 kB 00:00:00 ----------------------------------------------------------------------------------------------------------------------------------------------------- Total 956 kB/s | 701 kB 00:00:00 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : autogen-libopts-5.18-5.el7.x86_64 1/3 Installing : ntpdate-4.2.6p5-29.el7.centos.x86_64 2/3 Installing : ntp-4.2.6p5-29.el7.centos.x86_64 3/3 Verifying : ntp-4.2.6p5-29.el7.centos.x86_64 1/3 Verifying : ntpdate-4.2.6p5-29.el7.centos.x86_64 2/3 Verifying : autogen-libopts-5.18-5.el7.x86_64 3/3 Installed: ntp.x86_64 0:4.2.6p5-29.el7.centos Dependency Installed: autogen-libopts.x86_64 0:5.18-5.el7 ntpdate.x86_64 0:4.2.6p5-29.el7.centos Complete! [root@localhost ~]#
查看当前系统时间、并设置当前时间为上海
script 1 2 3 4 5 6 [root@localhost /]# date Thu Jun 4 05:28:48 UTC 2020 [root@localhost /]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@localhost /]# date Thu Jun 4 13:29:01 CST 2020 [root@localhost /]#
设置ntp,同步阿里云时间,执行vi /etc/ntp.conf 找到server四行代码注释掉,在其下面增加server aliyun.com iburst ,然后通过sudo systemctl start ntpd 启动服务,稍等一会执行ntpq -p 查看是否同步,如果出现前面的*则,同步成功
systemctl start ntpd 启动ntp systemctl restart ntpd 重启ntp systemctl enable ntpd.service 开机启动 ntpdc -c loopinfo 查看与时间同步服务器的时间差
script 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@localhost /]# vi /etc/ntp.conf # server 0.centos.pool.ntp.org iburst # server 1.centos.pool.ntp.org iburst # server 2.centos.pool.ntp.org iburst # server 3.centos.pool.ntp.org iburst server ntp.aliyun.com iburst [root@localhost /]# [root@localhost /]# sudo systemctl start ntpd [root@localhost /]# systemctl enable ntpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service. [root@localhost /]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *203.107.6.88 10.137.38.86 2 u 37 64 1 53.476 -5.668 2.224 [root@localhost /]#