设置 centos 系统的 ip 地址,Gateway,DNS 等

jimmy 2017年04月03日 2,106次浏览

设置centos系统的ip地址Gateway,DNS

系统版本为:

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

默认配置如下,其中系统有两块网卡ens160和ens192,一个回路设备lo,其中一块网卡ens160分配到的地址为10.10.51.91

[root@localhost ~]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.51.91  netmask 255.255.255.0  broadcast 10.10.51.255
        inet6 fe80::d67a:4123:99b1:6f29  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:53:aa:cd  txqueuelen 1000  (Ethernet)
        RX packets 510  bytes 48449 (47.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 299  bytes 34005 (33.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::5923:f260:3272:ea23  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:53:aa:d7  txqueuelen 1000  (Ethernet)
        RX packets 20  bytes 2722 (2.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 205  bytes 36318 (35.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

修改ens160为固定地址10.10.51.214

临时设置

[root@localhost ~]# ifconfig ens160 10.10.51.214 netmask 255.255.255.0

永久设置

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

TYPE=Ethernet
BOOTPROTO=static
IPADDR=10.10.51.214
PREFIX=24
GATEWAY=10.10.51.1
DNS1=10.10.51.1
DNS2=8.8.8.8
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes

IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens160
UUID=ac5c3430-a9bf-4528-aba5-ded44c070e70
DEVICE=ens160
ONBOOT=yes

新增IPADDR网络地址,PREFIX掩码长度,GATEWAY网关地址 DNS地址,设置结束后,reboot重启看看效果,或者重启网卡 systemctl restart network

[root@localhost ~]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.51.214  netmask 255.255.255.0  broadcast 10.10.51.255
        inet6 fe80::20c:29ff:fe53:aacd  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:53:aa:cd  txqueuelen 1000  (Ethernet)
        RX packets 76  bytes 7685 (7.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 87  bytes 10121 (9.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::5923:f260:3272:ea23  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:53:aa:d7  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 120 (120.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 3300 (3.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

修改centos的hostname

临时修改方式,重启后无效

hostname k8s-master

永久修改方式,重启后也生效

hostnamectl set-hostname centos77.magedu.com

在/etc/hosts文件加上记录

vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.51.215 k8s-node1

在主机上禁用SELinux

让容器可以读取主机文件系统

setenforce 0

永久修改方式

sudo vi /etc/selinux/config
#SELINUX修改为disabled
SELINUX=disabled

关闭防火墙

#停止firewall
systemctl stop firewalld.service 
#禁止firewall开机启动
systemctl disable firewalld.service 

centos 设置yum源为aliyun

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo