Linux中修改ip地址

1.文件修改

网卡配置文件为/etc/sysconfig/network-scripts/ifcfg-< iface>,其中,iface为网卡名称。

[root@localhost 桌面]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet                                                   //设备类型为Ethernet
BOOTPROTO=static                                                //启动协议为static静态协议,默认为dhcp动态协议
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=f34005ac-fb06-4d35-a8e9-57eb08429a90
DEVICE=eno16777736                                              //设备名称
ONBOOT=yes                                                      //开机是否启动该网卡,对虚拟接口无效。yes为开启,no为关闭
IPADDR=192.168.203.12                                           //ip地址。根据要求设置
NETMASK=255.255.255.0                                           //子网掩码。根据要求设置
GATEWAY=192.168.203.1                                           //网关。根据要求设置
DNS1=8.8.8.8                                                    //DNS服务器,多个服务器使用数字标记,如DNS1,DNS2等
DNS2=114.114.114.114

重启网络服务使设置生效

[root@localhost 桌面]# systemctl restart network

2.命令行修改

首先使用ifconfig命令查看网络接口信息

[root@localhost 桌面]# ifconfig
eno16777736: flags=4099  mtu 1500
        inet 192.168.203.99  netmask 255.255.255.0  broadcast 192.168.203.255
        inet6 fe80::20c:29ff:fe8e:2de8  prefixlen 64  scopeid 0x20
        ether 00:0c:29:8e:2d:e8  txqueuelen 1000  (Ethernet)
        RX packets 7  bytes 420 (420.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
...

可以看出,此虚拟机的ip地址为192.168.203.99,子网掩码为255.255.255.0。
下面使用nmcli命令手动修改网络接口信息,设置IP地址为192.168.203.12,子网掩码为255.255.255.0,网关为192.168.203.1,DNS为114.114.114.114。

[root@localhost 桌面]# nmcli connection modify eno16777736 \
> ipv4.method manual \
> ipv4.addresses 192.168.203.12/24 \
> ipv4.gateway 192.168.203.1 \
> ipv4.dns 114.114.114.114 \
> connection.autoconnect yes

[root@localhost 桌面]# nmcli connection down eno16777736;nmcli connection up eno16777736 
Connection 'eno16777736' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

[root@localhost 桌面]# ifconfig eno16777736
eno16777736: flags=4099  mtu 1500
        inet 192.168.203.12  netmask 255.255.255.0  broadcast 192.168.203.255
        inet6 fe80::20c:29ff:fe8e:2de8  prefixlen 64  scopeid 0x20
        ether 00:0c:29:8e:2d:e8  txqueuelen 1000  (Ethernet)
        RX packets 285  bytes 17100 (16.6 KiB)
        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

你可能感兴趣的:(Linux中修改ip地址)