Linux机器设置静态ip合集

/ Linux_manage / 没有评论 / 2009浏览

Ubuntu 18.04固定静态ip

cat /etc/netplan/50-cloud-init.yaml

root@ubuntu:~# cat /etc/netplan/50-cloud-init.yaml

# This file is generated from information provided by

# the datasource.  Changes to it will not persist across an instance.

# To disable cloud-init's network configuration capabilities, write a file

# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:

# network: {config: disabled}

network:
    ethernets:
        eno1:
            addresses: []
            dhcp4: true
        eno2:
            addresses: [192.168.0.213/24,]
            dhcp4: no
            dhcp6: no
            gateway4:  192.168.0.1
            optional: true
            nameservers:
                addresses: [8.8.8.8, 9.9.9.9]
    version: 2

其中,把dhcp4/dhcp6都设为no予以关闭,设上自己的ip地址、网关和域名服务器

立刻生效

netplan apply

Ubuntu 16.04设置固定ip

先ifconfig 查看当前网卡叫啥,这里是ens160

root@paa1:~# ifconfig
ens160    Link encap:Ethernet  HWaddr 00:0c:29:c4:e2:f4
          inet addr:192.168.0.165  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fda5:b414:c627:0:20c:29ff:fec4:e2f4/64 Scope:Global
          inet6 addr: fe80::20c:29ff:fec4:e2f4/64 Scope:Link



root@paa1:~# cat /etc/network/interfaces
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback
# The primary network interface
auto ens160                #网卡名字
iface ens160 inet static   #使用静态ip设置
address 192.168.0.165      #设置ip地址
netmask 255.255.255.0      #设置子网掩码
gateway 192.168.0.1        #设置网关 
dns-nameservers 114.114.114.114  #设置dns服务器地址

生效

/etc/init.d/networking restart

Centos7

vim /etc/sysconfig/network-scripts/ifcfg-网卡-name

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"         # 使用静态IP地址,默认为dhcp
IPADDR="192.168.0.89"   # 设置的静态IP地址
NETMASK="255.255.255.0"    # 子网掩码
GATEWAY="192.168.0.31"    # 网关地址
DNS1="8.8.8.8"       # DNS服务器
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="ens32"
DEVICE="ens32"
ONBOOT="yes"             #是否开机启用

/etc/init.d/network restart
systemctl restart network

Centos 6.5

[root@test ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:2E:2E:DD
TYPE=Ethernet
UUID=aa840f0c-89f8-41bb-b870-5f38ebfa77be
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.0.174
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=8.8.8.8
[root@test ~]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  Determining if ip address 192.168.0.174 is already in use for device eth0...
                                                           [  OK  ]