linux网络配置命令 ifconfig 、route 、ip 、ip route

一、ifconfig 命令(查看或配置Ip)

1、查看网络连接

1
# ifconfig  \\ 显示所有活动连接,不包令禁用的设备

   或

1
  # ifconfig -a  \\ 显示所有活动及非活动的连接

显示指定接口的连接

1
# ifconfig eth1

2、配置ip地址

1
# ifconfig eth1 192.168.0.1 netmask 255.255.255.

1
# ifconfig eth1 192.168.0.1/24

3、启用或禁用接口

1
2
3
4
# ifconfig eth1 down|up
# ifdown eth1  \\ 禁用接口
# ifup eht1  \\启用接口


二、route命令(查看或配置路由)

1、查看路由,使用-n可以以数字格式查看显示路由避免反解主机名,造成查询过慢。

1
2
3
# route -n
Destination     Gateway         Genmask          Flags  Metric  Ref      Use Iface
192.168.0.0     0.0.0.0         255.255.255.0     U       0      0        0 eth1
1
目标       下一跳地址       掩码      标志位  跳跃点  ref  使用的哪个接口


2、添加路由

(a)、网络路由

1
# route add -net 192.168.1.0/24 gw 172.16.1.106

(b)、主机路由

1
# route add -host 192.168.1.110 gw 172.16.1.106

(c)、默认路由

1
2
3
# route add default gw 172.16.1.106
# route add -net 0.0.0.0 gw 172.16.1.106

查看这三条新创建的

1
2
3
4
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.110   172.16.1.106    255.255.255.255 UGH   0      0        0 eth0
192.168.1.0     172.16.1.106    255.255.255.0   UG    0      0        0 eth0
0.0.0.0         172.16.1.106    0.0.0.0         UG    0      0        0 eth0


3、删除路由

1
2
# route del -host 192.168.1.110
# route del -net 192.168.1.0/24


三、DNS配置

只能通过配置文件:/etc/resolv.conf来修改内容如下

1
2
3
; generated by  /sbin/dhclient-script
search localdomain feng.com   \\ 搜索域
nameserver 172.16.0.2    \\ DNS域名服务器

一行一个DNS,最多配置三个DNS


************************************************************************************

随着系统的发展以上两种命令,会慢慢的被如下两个命令替换.

ifconfig = ip

route = ip route


ip命令

1、ip link(查看网络连接或启用禁用网卡)

(a)、查看网络连接

1
  # ip link show [Interace]  \\接口可省,省略后会显示所有连接

(b)、启用禁用接口及关闭启用多播

1
ip link  set  Interface [up|down] [multicast on|off]


2、ip addr(ip地址管理)

配置Ip

1
格式:ip addr add dev Interface Address [label Interface_alias]

(a)、给eth1配置一个IP或多个IP

1
# ip addr add dev eth1 192.168.1.118/24

(b)、给eth1的别名eth1:0配置一个Ip

1
# ip addr add dev eth1 192.168.0.12/24 label eth1:0

查看配置的Ip

1
2
3
4
5
# ip addr show eth1  \\ 查看配置好的ip,此命令配置的ip不能使用ifconfig来查看
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
     link /ether  00:0c:29:42:91:29 brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.118 /24  scope global eth1
     inet 192.168.0.12 /24  scope global eth1:0

删除配置的ip

1
# ip addr del dev eth1 192.168.1.118/24


清除配置

# ip addr flush Interface [to Net_Address]  

1
# ip addr flush eth1  \\ 不带任何参数可以清除该网卡的所有IP配置


1
ip addr flush eth0 to 192.168.0.0 /24   \\ 只要是和192.168.0相同网段的IP都会清掉

1
ip addr flush eth0 192.168.16.19 /24   \\ 只清除指定的ip



3、ip route 路由配置

    ip route

(a)、查看路由配置

1
# ip route show  或 ip route

(b)、配置路由      

  ip route add DESTINATION [via NEXT_HOP] [src SOURCE_ADDRESS] [dev DEVICE]    # 

1
# ip route add 192.168.1.0/24 via 172.16.1.106

如果网卡配置有多个ip时可以指定数据包从哪个网卡出去

1
# ip route add 192.168.1.0/24 src 172.16.1.106 dev eth1
1
2
# ip route show
192.168.1.0 /24  dev eth1  scope link  src 172.16.1.106

(c)、删除路由

1
# ip route del 192.168.0.1/24

猜你喜欢

转载自blog.csdn.net/lemontree1945/article/details/80441475