您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
马哥linux运维学习笔记-Linux网络配置之四 ifconfig及ip命令详解
发布时间:2018-08-25 15:04:48编辑:雪饮阅读()
查看路由表
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 1 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
10.0.0.0 localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
flags字段:U表示启用,G表示网关路由,没有带网关路由就代表没有下一跳,则表示本地网络
-n:destination和gateway的值都会以ip地址显示,而不是名称显示
删除路由
根据destination地址和掩码来删除
route del -net 10.0.0.0/8
根据destination名称来删除
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 1 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route del default
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 1 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
添加路由
根据destination和掩码和网关进行添加
route add -net 10.0.0.0/8 gw 192.168.43.3
根据destination名称和网关进行添加
[root@localhost ~]# route add default gw 192.168.43.5
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 1 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 255.0.0.0 UG 0 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
0.0.0.0 192.168.43.2 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.43.1 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.43.5 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.43.1 0.0.0.0 UG 0 0 0 eth0
通过配置文件添加永久静态路由
格式
目标与掩码-》via=》重定向目标
vi /etc/sysconfig/network-scripts/route-eth0
192.168.10.0/24 via 192.168.43.77
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 1 0 0 eth0
192.168.10.0 localhost 255.255.255.0 UG 0 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
192.168.10.0 192.168.43.77 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 192.168.43.1 0.0.0.0 UG 0 0 0 eth0
通过配置文件添加永久静态路由(另外一种格式)
配置文件路径相同,内容不同
ADDRESS0=192.168.10.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.43.88
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
192.168.10.0 192.168.43.88 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 192.168.43.1 0.0.0.0 UG 0 0 0 eth0
配置dns服务器地址
vi /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.43.1
nameserver 210.12.222.218
最多配置3个dns服务器
若网络接口配置文件如/etc/sysconfig/network-scripts/ifcfg-eth0中的PEERDNS的值决定了在使用了dhcp服务器且dhcp服务器正好能给你分配dns地址,是否覆盖当前配置文件的dns配置
hosts中配置域名别名解析
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.43.179 www.gaojiupan.cn www
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# ping www
PING www.gaojiupan.cn (192.168.43.179) 56(84) bytes of data.
64 bytes from www.gaojiupan.cn (192.168.43.179): icmp_seq=1 ttl=64 time=0.054 ms
64 bytes from www.gaojiupan.cn (192.168.43.179): icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from www.gaojiupan.cn (192.168.43.179): icmp_seq=3 ttl=64 time=0.045 ms
64 bytes from www.gaojiupan.cn (192.168.43.179): icmp_seq=4 ttl=64 time=0.046 ms
^C
--- www.gaojiupan.cn ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3713ms
rtt min/avg/max/mdev = 0.045/0.048/0.054/0.009 ms
查看当前系统网络接口与地址
ifconfig看不到同一网络接口的不同地址,它可以
[root@www ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:be:b9:96 brd ff:ff:ff:ff:ff:ff
inet 192.168.43.179/24 brd 192.168.43.255 scope global eth0
inet6 fe80::20c:29ff:febe:b996/64 scope link
valid_lft forever preferred_lft forever
3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
link/ether 0e:b7:a0:11:3e:90 brd ff:ff:ff:ff:ff:ff
给一个网络接口添加新的ip地址(一个网卡可以有多个ip地址)并别名
此例中的别名是eth0:y
[root@www ~]# ip addr add 192.168.100.3/24 dev eth0 label eth0:y
[root@www ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:be:b9:96 brd ff:ff:ff:ff:ff:ff
inet 192.168.43.179/24 brd 192.168.43.255 scope global eth0
inet 192.168.100.3/24 scope global eth0:y
inet6 fe80::20c:29ff:febe:b996/64 scope link
valid_lft forever preferred_lft forever
3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
link/ether 0e:b7:a0:11:3e:90 brd ff:ff:ff:ff:ff:ff
匹配查询某个网络接口信息
这里匹配的是:eth0网络接口以192.168.100开头的ip地址且掩码位数是24的信息。
[root@www ~]# ip addr show eth0 to 192.168.100/24
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.100.3/24 scope global eth0:y
直接查看某个网络接口的所有信息
[root@www ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:be:b9:96 brd ff:ff:ff:ff:ff:ff
inet 192.168.43.179/24 brd 192.168.43.255 scope global eth0
inet 192.168.100.3/24 scope global eth0:y
inet6 fe80::20c:29ff:febe:b996/64 scope link
valid_lft forever preferred_lft forever
关键字词:linux,ifconfig,ip