设置静态ip
修改/etc/sysconfig/network-scripts/ifcfg-xxx
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
# 此处修改为static,而不是dhcp
BOOTPROTO=static
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=enp0s3
UUID=a3e99bef-dde7-4c9a-9425-1749a915436a
DEVICE=enp0s3
ONBOOT=yes
# 正确设置IPADDR(IP地址),GATEWAY(网关),NETMASK(掩码),如果设置失败会导致network服务启动失败
IPADDR=172.21.104.180
GATEWAY=172.21.105.254
NETMASK=255.255.254.0
# 使用静态ip,必须指定dns,否则会导致域名解析失败,最多指定3个域名
# 一般会配置一个114.114.114.114或者8.8.8.8
DNS1=10.45.40.7
DNS2=172.21.111.254
DNS3=114.114.114.114
各个参数获取
ip addr
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:ba:7b:be brd ff:ff:ff:ff:ff:ff
--- inet这里为ip/掩码位数,23位则为255.255.254.0,就是8位,23位则第三个255少一个1
--- brd 为广播地址
inet 172.21.104.180/23 brd 172.21.105.255 scope global noprefixroute enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::be4e:94b0:2a03:cb59/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ifconfig
[hehuang@localhost network-scripts]$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
--- 这里为ip 掩码 以及广播地址
inet 172.21.104.180 netmask 255.255.254.0 broadcast 172.21.105.255
inet6 fe80::be4e:94b0:2a03:cb59 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:ba:7b:be txqueuelen 1000 (Ethernet)
RX packets 293751 bytes 380458599 (362.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 36896 bytes 4351992 (4.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
cat /etc/resolv.conf
用于获取dns,在修改为静态ip之前,先通过该文件获取dns,每次重启network服务,这个文件都会刷新
[hehuang@localhost network-scripts]$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.45.40.7
nameserver 172.21.111.254
nameserver 114.114.114.114
查询路由表
获取网关
- netstat -rn
- route -n
- ip route show
- traceroute xxx 实际测试,第一行有时候是空的,需要通过yum install traceroute进行安装