VM CentOS7 配置上网

1.网络配置

1.1

image.png

1.2然后进入centos系统

其中“ens33”为网卡名称,修改IP地址等信息

vi /etc/sysconfig/network-scripts/ifcfg-ens33

根据实际情况修改内容:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #启用静态ip地址
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=8d497804-9ffa-4edc-951a-5ddd8d2ed504
DEVICE=ens33
ONBOOT=yes #自动启用网络连接
IPADDR0=192.168.0.201 #IP地址
PREFIXO0=24 #子网掩码
GATEWAY0=192.168.0.1 #网关
DNS1=192.168.0.1 #DNS

修改好后wq保存退出,,
并重启网络

[root@centos7 ~]# service network restart

测试网络是否连接正常:

[root@centos7 ~]# ping www.baidu.com

2.安装软件

yum install -y net-tools vim-enhanced wget

3 关闭SELinux

编辑SELinux配置文件

vim /etc/selinux/config

注释掉2行代码,并在最后添加1行代码

#SELINUX=enforcing
#SELINUXTYPE=targeted
SELINUX=disabled

保存并关闭,使设置启用,最好重启一下系统,也可以稍后重启

setenforce 0

4 关闭CentOS自带firewall防火墙,启用iptables

4.1停止firewall

[root@centos7 /]# systemctl stop firewalld

关闭firewall自动启动

[root@centos7 /]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

4.2安装iptables防火墙

yum install -y iptables-services

使用iptables默认配置文件,可以在此配置文件中增加规则

[root@centos7 ~]# vim /etc/sysconfig/iptables

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

重启iptables,并添加到开机自启项中,查看iptables状态

[root@centos7 ~]# systemctl restart iptables.service
[root@centos7 ~]# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@centos7 ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: active (exited) since Mon 2018-05-21 18:09:28 CST; 18s ago
 Main PID: 1195 (code=exited, status=0/SUCCESS)

May 21 18:09:27 centos7 systemd[1]: Starting IPv4 firewall with iptables...
May 21 18:09:28 centos7 iptables.init[1195]: iptables: Applying firewall rules: [  OK  ]
May 21 18:09:28 centos7 systemd[1]: Started IPv4 firew

4配置国内yum源和epel源

4.1 配置yum源

1)新建文件夹,保存系统中原来的repo文件

[root@centos7 ~]# cd /etc/yum.repos.d/
[root@centos7 yum.repos.d]# mkdir repo_bak
[root@centos7 yum.repos.d]# mv *.repo repo_bak/

2)下载阿里云和网易对应系统的repo文件

[root@centos7 yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos7 yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@centos7 yum.repos.d]# ll
total 8
-rw-r--r-- 1 root root 1572 Dec  1  2016 CentOS7-Base-163.repo
-rw-r--r-- 1 root root 2573 Nov 21  2014 Centos-7.repo
drwxr-xr-x 2 root root  187 May 21 18:25 repo_bak

3)清楚系统yum缓存,并生成新的yum缓存

[root@centos7 yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Cleaning repos: base extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@centos7 yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Determining fastest mirrors
base                                                                                   | 3.6 kB  00:00:00
extras                                                                                 | 3.4 kB  00:00:00
updates                                                                                | 3.4 kB  00:00:00
(1/12): base/7/x86_64/group_gz                                                         | 166 kB  00:00:00
(2/12): base/7/x86_64/primary_db                                                       | 5.9 MB  00:00:01
(3/12): extras/7/x86_64/prestodelta                                                    |  47 kB  00:00:00
(4/12): base/7/x86_64/other_db                                                         | 2.5 MB  00:00:00
(5/12): extras/7/x86_64/primary_db                                                     | 143 kB  00:00:00
(6/12): extras/7/x86_64/filelists_db                                                   | 517 kB  00:00:00
(7/12): extras/7/x86_64/other_db                                                       |  91 kB  00:00:00
(8/12): updates/7/x86_64/prestodelta                                                   | 180 kB  00:00:00
(9/12): updates/7/x86_64/filelists_db                                                  | 875 kB  00:00:00
(10/12): updates/7/x86_64/other_db                                                     | 201 kB  00:00:00
(11/12): updates/7/x86_64/primary_db                                                   | 1.2 MB  00:00:00
(12/12): base/7/x86_64/filelists_db                                                    | 6.9 MB  00:00:04
Metadata Cache Created

4.2 配置epel源

1)安装epel-release,安装成功后,会自动生成epel.repo和epel-testing.repo两个文件

[root@centos7 yum.repos.d]# yum list | grep epel-release
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
epel-release.noarch                         7-11                       extras
[root@centos7 yum.repos.d]# yum install -y epel-release
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================
 Package                       Arch                    Version                  Repository               Size
==============================================================================================================
Installing:
 epel-release                  noarch                  7-11                     extras                   15 k

Transaction Summary
==============================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm                                                           |  15 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                                                   1/1
  Verifying  : epel-release-7-11.noarch                                                                   1/1

Installed:
  epel-release.noarch 0:7-11

Complete!
[root@centos7 yum.repos.d]# ll
total 16
-rw-r--r-- 1 root root 1572 Dec  1  2016 CentOS7-Base-163.repo
-rw-r--r-- 1 root root 2573 Nov 21  2014 Centos-7.repo
-rw-r--r-- 1 root root  951 Oct  3  2017 epel.repo
-rw-r--r-- 1 root root 1050 Oct  3  2017 epel-testing.repo
drwxr-xr-x 2 root root  187 May 21 18:25 repo_bak

2)下载阿里云开源镜像的epel源文件

[root@centos7 yum.repos.d]# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
--2018-05-21 18:35:24--  http://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 1.81.1.230, 1.81.1.223, 1.81.1.224, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|1.81.1.230|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/epel-7.repo’

100%[====================================================================>] 664         --.-K/s   in 0s

2018-05-21 18:35:24 (148 MB/s) - ‘/etc/yum.repos.d/epel-7.repo’ saved [664/664]
[root@centos7 yum.repos.d]# ll
total 20
-rw-r--r-- 1 root root 1572 Dec  1  2016 CentOS7-Base-163.repo
-rw-r--r-- 1 root root 2573 Nov 21  2014 Centos-7.repo
-rw-r--r-- 1 root root  664 May 11 11:35 epel-7.repo
-rw-r--r-- 1 root root  951 Oct  3  2017 epel.repo
-rw-r--r-- 1 root root 1050 Oct  3  2017 epel-testing.repo
drwxr-xr-x 2 root root  187 May 21 18:25 repo_bak

3)再次清除系统yum缓存,并重新生成yum缓存

[root@centos7 yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Cleaning repos: base epel extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@centos7 yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Determining fastest mirrors
base                                                                                   | 3.6 kB  00:00:00
epel                                                                                   | 4.7 kB  00:00:00
extras                                                                                 | 3.4 kB  00:00:00
updates                                                                                | 3.4 kB  00:00:00
(1/18): base/7/x86_64/group_gz                                                         | 166 kB  00:00:00
(2/18): base/7/x86_64/primary_db                                                       | 5.9 MB  00:00:01
(3/18): base/7/x86_64/other_db                                                         | 2.5 MB  00:00:00
(4/18): epel/x86_64/group_gz                                                           |  88 kB  00:00:01
(5/18): epel/x86_64/updateinfo                                                         | 923 kB  00:00:00
(6/18): epel/x86_64/prestodelta                                                        | 2.8 kB  00:00:00
(7/18): epel/x86_64/primary_db                                                         | 6.4 MB  00:00:02
(8/18): epel/x86_64/filelists_db                                                       |  10 MB  00:00:03
(9/18): extras/7/x86_64/filelists_db                                                   | 517 kB  00:00:00
(10/18): extras/7/x86_64/primary_db                                                    | 143 kB  00:00:00
(11/18): extras/7/x86_64/other_db                                                      |  91 kB  00:00:00
(12/18): extras/7/x86_64/prestodelta                                                   |  47 kB  00:00:00
(13/18): updates/7/x86_64/prestodelta                                                  | 180 kB  00:00:00
(14/18): updates/7/x86_64/primary_db                                                   | 1.2 MB  00:00:00
(15/18): updates/7/x86_64/other_db                                                     | 201 kB  00:00:00
(16/18): epel/x86_64/other_db                                                          | 3.1 MB  00:00:01
(17/18): updates/7/x86_64/filelists_db                                                 | 875 kB  00:00:00
(18/18): base/7/x86_64/filelists_db                                                    | 6.9 MB  00:00:09
Metadata Cache Created

3.7.3 查看系统可用的yum源和所有的yum源

[root@centos7 yum.repos.d]# yum repolist enabled
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
repo id                              repo name                                                          status
base/7/x86_64                        CentOS-7 - Base - 163.com                                           9,911
epel/x86_64                          Extra Packages for Enterprise Linux 7 - x86_64                     12,542
extras/7/x86_64                      CentOS-7 - Extras - 163.com                                           291
updates/7/x86_64                     CentOS-7 - Updates - 163.com                                          539
repolist: 23,283
[root@centos7 yum.repos.d]#
[root@centos7 yum.repos.d]# yum repolist all
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
repo id                       repo name                                                        status
base/7/x86_64                 CentOS-7 - Base - 163.com                                        enabled:  9,911
centosplus/7/x86_64           CentOS-7 - Plus - 163.com                                        disabled
contrib/7/x86_64              CentOS-7 - Contrib - mirrors.aliyun.com                          disabled
epel/x86_64                   Extra Packages for Enterprise Linux 7 - x86_64                   enabled: 12,542
epel-debuginfo/x86_64         Extra Packages for Enterprise Linux 7 - x86_64 - Debug           disabled
epel-source                   Extra Packages for Enterprise Linux 7 - x86_64 - Source          disabled
epel-testing/x86_64           Extra Packages for Enterprise Linux 7 - Testing - x86_64         disabled
epel-testing-debuginfo/x86_64 Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Debug disabled
epel-testing-source/x86_64    Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Sourc disabled
extras/7/x86_64               CentOS-7 - Extras - 163.com                                      enabled:    291
updates/7/x86_64              CentOS-7 - Updates - 163.com                                     enabled:    539
repolist: 23,283

5. 修改hostname主机名

3.8.1 hostnamectl

[root@localhost ~]# hostnamectl
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: 8dbe281bfa3d4b3c9390c26f494db334
Boot ID: b629ca855d3e4406ab886fe709d2f917
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-514.el7.x86_64
Architecture: x86-64
[root@localhost ~]# hostnamectl set-hostname CentOS
[root@localhost ~]# hostnamectl
Static hostname: centos
Pretty hostname: CentOS
Icon name: computer-vm
Chassis: vm
Machine ID: 8dbe281bfa3d4b3c9390c26f494db334
Boot ID: b629ca855d3e4406ab886fe709d2f917
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-514.el7.x86_64
Architecture: x86-64
[root@localhost ~]# hostname
centos
3.8.2 修改主机名文件

[root@localhost ~]# vi /etc/hostname
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,012评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,628评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,653评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,485评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,574评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,590评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,596评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,340评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,794评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,102评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,276评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,940评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,583评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,201评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,441评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,173评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,136评论 2 352

推荐阅读更多精彩内容