一、.ifconfig命令
用来设定IP地址等。
1.命令格式ifconfig [网络设备] [参数]
2.常用选项查看:ifconfig [Interface] [-a]
启用或禁用:ifconfig IFACE up|down
启用或禁用混杂模式:[-]promisc,如果选择此参数,网卡将接收网络中发给它所有的数据包
设定MTU: mtu N
设定IP:
ifconfig IFACE IP/Netmask [up]
ifcconfig IFACE IP netmask NETMASK
#立即起效,但不能永久有效;
3、实例
1)、查看所有网卡状态[root@bash ~]# ifconfig -a
eno16777736: flags=4163 mtu 1500
inet 192.168.1.6 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fee1:6e8c prefixlen 64 scopeid 0x20
ether 00:0c:29:e1:6e:8c txqueuelen 1000 (Ethernet)
RX packets 2861646 bytes 2752671819 (2.5 GiB)
RX errors 0 dropped 224 overruns 0 frame 0
TX packets 1032405 bytes 108810235 (103.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 1308 bytes 141244 (137.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1308 bytes 141244 (137.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2)、启用和禁用网卡[root@CentOS ~]# ifconfig eth1 down
[root@CentOS ~]# ifconfig|grep eth1
[root@CentOS ~]# ifconfig eth1 up
[root@CentOS ~]# ifconfig|grep eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:30:84:C4
3)、设置临时ip地址[root@CentOS ~]# ifconfig eth1 192.168.1.11/24 up
[root@CentOS ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:30:84:C4
inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST SLAVE MULTICAST MTU:1500 Metric:1
RX packets:771 errors:0 dropped:0 overruns:0 frame:0
TX packets:202 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:50906 (49.7 KiB) TX bytes:24364 (23.7 KiB)
4)、设置MTU[root@CentOS ~]# ifconfig eth1 mtu 2000
[root@CentOS ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:30:84:C4
inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST SLAVE MULTICAST MTU:2000 Metric:1
RX packets:771 errors:0 dropped:0 overruns:0 frame:0
TX packets:202 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:50906 (49.7 KiB) TX bytes:24364 (23.7 KiB)
二、route命令
查看管理路由表,立即起效,但不会永久生效;
1.命令格式route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]
2.常用选项查看内核路由表:route [-n][-ee]
添加路由:route add
-host HOST_IP gw NEXT_HOP [dev DEVICE] #不能填写掩码
-net NET_ADDR gw NEXT_HOP [dev DEVICE] #只能制定网段
-net 0.0.0.0 gw NEXT_HOP [dev DEVICE]
default gw NEXT_HOP [dev DEVICE]
删除路由:route del
-host HOST_IP
-net NET_ADDR
3.实例
1)、查看当前路由表[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 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.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
说明:第一行表示主机所在网络的地址为192.168.120.0,若数据传送目标是在本局域网内通信,则可直接通过eth0转发数据包;
第四行表示数据传送目的是访问Internet,则由接口eth0,将数据包发送到网关192.168.120.240
其中Flags为路由标志,标记当前网络节点的状态。
Metric 距离、跳数。暂无用。
Ref 不用管,恒为0。
Flags标志说明:U Up表示此路由当前为启动状态
H Host,表示此网关为一主机
G Gateway,表示此网关为一路由器
R Reinstate Route,使用动态路由重新初始化的路由
D Dynamically,此路由是动态性地写入
M Modified,此路由是由路由守护程序或导向器动态修改
! 表示此路由当前为关闭状态
备注:
route -n (-n 表示不解析名字,列出速度会比route 快)
2)、添加路由表[root@CentOS ~]# route add default gw 192.168.1.9 #添加默认网关
[root@CentOS ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.9 0.0.0.0 UG 0 0 0 eth0
[root@CentOS ~]# route add -net 192.168.10.0 gw 192.168.10.22\
netmask 255.255.255.0 dev eth1 #添加网络路由
[root@CentOS ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 192.168.10.22 255.255.255.0 UG 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.9 0.0.0.0 UG 0 0 0 eth0
[root@CentOS ~]# route add -host 192.168.10.3 gw 192.168.10.22 dev eth1
#添加主机路由,此处不应该写掩码,写会报错
[root@CentOS ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.3 192.168.10.22 255.255.255.255 UGH 0 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 192.168.10.22 255.255.255.0 UG 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.9 0.0.0.0 UG 0 0 0 eth0
3)、删除路由记录[root@CentOS ~]# route del -host 192.168.10.3 gw 192.168.10.22 dev eth1 #删除主机路由
[root@CentOS ~]# route del -net 192.168.10.0 gw 192.168.10.22\
netmask 255.255.255.0 dev eth1 #删除网络路由
[root@CentOS ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.9 0.0.0.0 UG 0 0 0 eth0
小贴士:
在linux中添加永久路由的方法
1)、写入/etc/rc.loacl
可以将上面提到的命令写入/etc/rc.local文件中,这样在系统启动的时候会自动加入相关的路由设置 不过这种方法有一个缺点:如果某个系统服务,比如说是NFS服务,这个服务是在启动network服务之后,在执行rc.local之前,如果你设置的有自动挂载的nfs,那么,这里链路的不通畅,会造成挂载的失败。另外一个就是如果你重启了网络服务器,那么路由就失效了,这个时候你不得不重新加载这个文件,但是如果你是远程操作的呢?所以,这个方法不是非常的不推荐
2)、在/etc/sysconfig/network里添加到文件末尾,格式如下(注:未验证有效性):
GATEWAY=gw-ip 或者 GATEWAY=gw-dev 这种方式从外观上来看只能用于添加默认网关了。。。对于多路网络环境貌似不太适合的样子
3)、直接写入ifcfg文件
在配置ip地址的时候直接将GATEWAY的配置写入ifcfg文件,形式:GATEWAY=gw-ip 适合添加默认路由
4)、写入/etc/sysconfig/static-routes文件
默认在/etc/sysconifg目录中是没有这个文件的,需要我们手工创建,对这个文件的调用在下面: /etc/init.d/network:159 # Add non interface-specific static-routes.
160 if [ -f /etc/sysconfig/static-routes ]; then
161 grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
162 /sbin/route add -$args
163 done
164 fi
/etc/sysconfig/static-routes:
any net 192.168.1.0/24 gw 192.168.1.24
any net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1
这种方式的话,和rc.local比较而言,比较实用,还比如nfs,这个路由的生效时间是在网络服务network启动的时候生效的,而其他的一些网络相关服务都是在网络服务启动成功之后再启动的,所以能够保证网络链路的通畅。 而且,如果我重启了网络服务,这个脚本是在网络服务启动脚本里面调用的,所以也会自动添加上相关的路由设置
三、tui或者gui配置网络命令TUI:system-config-network-tui
GUI: system-config-network-gui
#setup
#nmtui NetworkManager的字符界面管理工具
四、ip命令
ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig、route等,使用权限为超级用户。几乎所有的Linux发行版本都支持该命令。ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT:
link: 链接接口,如eth0
addr: 管理设备上的地址
route: 管理路由
link:
show [IFACE]
set IFACE {up|down}
set IFACE name NEWNAME
addr:
添加地址:ip addr add IP/Prefixlen dev DEVICE
删除地址:ip addr del IP/prefixlen dev DEVICE
查看:ip addr show [IFACE]
清空:ip addr flush IFACE
route:
查看路由:ip route {show|list}
添加路由:ip route add DESTINATION via NEXT_HOP [dev DEIVCE]
定义默认网关:ip route add default via NEXT_HOP [dev DEVICE]
删除路由:ip route del DESTINATION
搜索指定路由:ip route get DESTINATION
实例:
1)link[root@young ~]# ip link #显示所有网络设备状态,ip -s link会显示更为详细信息
1: lo: mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
4: pan0: mtu 1500 qdisc noop state DOWN
link/ether 22:4f:02:f2:45:36 brd ff:ff:ff:ff:ff:ff
[root@young ~]# ip link show eth0 #显示指定网络设备状态
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
[root@young ~]# ip link set eth1 down #关闭指定设备
[root@young ~]# ip link show eth1
3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
[root@young ~]# ip link set eth1 up #打开指定设备
[root@young ~]# ip link set eth1 down
[root@young ~]# ip link set eth1 name eth9 #更改设备名称,要先关闭设备
[root@young ~]# ip link
1: lo: mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
3: eth9: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
4: pan0: mtu 1500 qdisc noop state DOWN
link/ether 22:4f:02:f2:45:36 brd ff:ff:ff:ff:ff:ff
2)addr[root@young ~]# ip addr add 192.168.10.11/24 dev eth1 #给设备添加ip地址
[root@young ~]# ip addr show eth1
3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
inet 192.168.10.11/24 scope global secondary eth1
[root@young ~]# ip addr del 192.168.10.11/24 dev eth1 #删除设备ip地址
[root@young ~]# ip addr show eth1
3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
[root@young ~]# ip addr show #查看所有设备ip地址
1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.9/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe30:84c4/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
4: pan0: mtu 1500 qdisc noop state DOWN
link/ether 22:4f:02:f2:45:36 brd ff:ff:ff:ff:ff:ff
[root@young ~]# ip addr show eth0 #查看制定设备ip地址
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.9/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe30:84c4/64 scope link
valid_lft forever preferred_lft forever
[root@young ~]# ip addr show eth1
3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
[root@young ~]# ip addr flush eth1 #清空指定设备的ip地址
[root@young ~]# ip addr show eth1
3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
3)route[root@young ~]# ip route show #显示所有路由记录
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.9
169.254.0.0/16 dev eth0 scope link metric 1002
[root@young ~]# ip route add 192.168.10.0 via 192.168.10.6 dev eth1 #添加路由记录
[root@young ~]# ip route list
192.168.10.0 via 192.168.10.6 dev eth1
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.9
192.168.10.0/23 dev eth1 proto kernel scope link src 192.168.10.6
169.254.0.0/16 dev eth0 scope link metric 1002
[root@young ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.0 192.168.10.6 255.255.255.255 UGH 0 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 0.0.0.0 255.255.254.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
[root@young ~]# ip route add default via 192.168.1.1 dev eth0 #添加默认路由
[root@young ~]# ip route del 192.168.10.0/23 #删除指定路由
[root@young ~]# ip route show
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.9
169.254.0.0/16 dev eth0 scope link metric 1002
default via 192.168.1.1 dev eth0
[root@young ~]# ip route get 10.42.0.47 #查看路由包来源
10.42.0.47 via 192.168.1.1 dev eth0 src 192.168.1.9
cache mtu 1500 hoplimit 64