cisco常用命令详解
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.常用命令用法展示
1.命令行模式的来回切换

1 yinzhengjie>enable #从用户模式切换到特权模式,权限最低,可以说是无功能,啥也不能做。 2 yinzhengjie# #特权模式,可以进行查看端口的IP配置信息,查看主机表等,重启路由器等等操作。 3 4 yinzhengjie#configure terminal #从特权模式切换到全局配置模式,权限最高,可以在这里配置vlan等等 5 Enter configuration commands, one per line. End with CNTL/Z. 6 yinzhengjie(config)# 7 8 yinzhengjie(config)#exit #从全局配置模式切换到特权模式,如果想要切换到用户模式继续敲击exit即可。 9 yinzhengjie# 10 %SYS-5-CONFIG_I: Configured from console by console
2.修改主机名

1 123(config)#hostname yinzhengjie #注意:需要在全局配置模式下敲击哟 2 yinzhengjie(config)#
3.配置用户切换至特权模式的密码

1 yinzhengjie(config)#enable password yinzhengjie #配置在用户模式进入特权模式的密码是:yinzhengjie, 但是这种配置方法可以通过show run来进行查看 2 yinzhengjie(config)# 3 4 yinzhengjie>enable #验证过程,注意, 5 Password: 6 yinzhengjie# 7 8 9 10 #可以在特权模式下查看你做了哪些配置 11 yinzhengjie#show run 12 Building configuration... 13 14 Current configuration : 457 bytes 15 ! 16 version 12.2 17 no service timestamps log datetime msec 18 no service timestamps debug datetime msec 19 no service password-encryption 20 ! 21 hostname yinzhengjie 22 ! 23 ! 24 ! 25 enable password yinzhengjie #这个就是进入特权模式的密码

1 yinzhengjie(config)#enable secret yinzhengjie #配置用户模式进入特权模式的密码,且是密文形式的,不会被show run看出来密码。 2 yinzhengjie(config)#exit 3 yinzhengjie# 4 %SYS-5-CONFIG_I: Configured from console by console 5 6 yinzhengjie#show run 7 Building configuration... 8 9 Current configuration : 504 bytes 10 ! 11 version 12.2 12 no service timestamps log datetime msec 13 no service timestamps debug datetime msec 14 no service password-encryption 15 ! 16 hostname yinzhengjie 17 ! 18 ! 19 ! 20 enable secret 5 $1$mERr$m71c3Gjeqb4tpumB3.jHY. #这种加密是无法被看到的哟
4.查看命令用法

1 yinzhengjie#show hosts #查看主机表 2 Default Domain is not set 3 Name/address lookup uses domain service 4 Name servers are 255.255.255.255 5 6 Codes: UN - unknown, EX - expired, OK - OK, ?? - revalidate 7 temp - temporary, perm - permanent 8 NA - Not Applicable None - Not defined 9 10 Host Port Flags Age Type Address(es) 11 yinzhengjie# 12 13 yinzhengjie#show ip interface #查看端口的IP配置信息 14 FastEthernet0/0 is administratively down, line protocol is down (disabled) 15 Internet protocol processing disabled 16 FastEthernet0/1 is administratively down, line protocol is down (disabled) 17 Internet protocol processing disabled 18 yinzhengjie#
5.VLAN的创建与删除

1 yinzhengjie#vlan data #进入VLAN配置模式 2 % Warning: It is recommended to configure VLAN from config mode, 3 as VLAN database mode is being deprecated. Please consult user 4 documentation for configuring VTP/VLAN in config mode. 5 6 yinzhengjie(vlan)#vlan 10 name test_1 #创建VLAN10并其名称为test_1 7 VLAN 10 added: #输出信息,创建VLAN10 8 Name: test_1 #输出信息,名称为test_1 9 yinzhengjie(vlan)#vlan 20 name test_2 #创建VLAN20 10 VLAN 20 added: 11 Name: test_2 12 yinzhengjie(vlan)#vlan 30 name test_3 #创建VLAN30 13 VLAN 30 added: 14 Name: test_3 15 yinzhengjie(vlan) 16 17 思科VLAN的创建

1 yinzhengjie#show vlan brief #查看VLAN信息 2 3 VLAN Name Status Ports 4 ---- -------------------------------- --------- ------------------------------- 5 1 default active Fa0/1, Fa0/2, Fa0/3, Fa0/4 6 Fa0/5, Fa0/6, Fa0/7, Fa0/8 7 Fa0/9, Fa0/10, Fa0/11, Fa0/12 8 Fa0/13, Fa0/14, Fa0/15, Fa0/16 9 Fa0/17, Fa0/18, Fa0/19, Fa0/20 10 Fa0/21, Fa0/22, Fa0/23, Fa0/24 11 10 test_1 active #之前已经存在的VLAN 12 20 test_2 active #之前已经存在的VLAN 13 30 test_3 active #之前已经存在的VLAN 14 1002 fddi-default active 15 1003 token-ring-default active 16 1004 fddinet-default active 17 1005 trnet-default active 18 yinzhengjie# 19 yinzhengjie#vlan database #进入VLAN的配置模式 20 % Warning: It is recommended to configure VLAN from config mode, 21 as VLAN database mode is being deprecated. Please consult user 22 documentation for configuring VTP/VLAN in config mode. 23 24 yinzhengjie(vlan)#no vlan 10 #删除VLAN10 25 Deleting VLAN 10... 26 yinzhengjie(vlan)#no vlan 20 #删除VLAN20 27 Deleting VLAN 20... 28 yinzhengjie(vlan)#exit 29 APPLY completed. 30 Exiting.... 31 yinzhengjie#show vlan brief 32 33 VLAN Name Status Ports 34 ---- -------------------------------- --------- ------------------------------- 35 1 default active Fa0/1, Fa0/2, Fa0/3, Fa0/4 36 Fa0/5, Fa0/6, Fa0/7, Fa0/8 37 Fa0/9, Fa0/10, Fa0/11, Fa0/12 38 Fa0/13, Fa0/14, Fa0/15, Fa0/16 39 Fa0/17, Fa0/18, Fa0/19, Fa0/20 40 Fa0/21, Fa0/22, Fa0/23, Fa0/24 41 30 test_3 active #只剩下VLAN30了,10和20都被删除了 42 1002 fddi-default active 43 1003 token-ring-default active 44 1004 fddinet-default active 45 1005 trnet-default active 46 yinzhengjie#
6.将交换机的接口添加或删除到VLAN之中

1 yinzhengjie#show vlan brief #查看VLAN信息 2 3 VLAN Name Status Ports 4 ---- -------------------------------- --------- ------------------------------- 5 1 default active Fa0/1, Fa0/2, Fa0/3, Fa0/4 6 Fa0/5, Fa0/6, Fa0/7, Fa0/8 7 Fa0/9, Fa0/10, Fa0/11, Fa0/12 8 Fa0/13, Fa0/14, Fa0/15, Fa0/16 9 Fa0/17, Fa0/18, Fa0/19, Fa0/20 10 Fa0/21, Fa0/22, Fa0/23, Fa0/24 11 30 test_3 active #当前VLAN30是没用任何的接口在这个VLAN之中的 12 1002 fddi-default active 13 1003 token-ring-default active 14 1004 fddinet-default active 15 1005 trnet-default active 16 yinzhengjie#configure terminal 17 Enter configuration commands, one per line. End with CNTL/Z. 18 19 yinzhengjie(config)#interface fastEthernet 0/1 #只进入f0/1接口 20 yinzhengjie(config-if)#switchport access vlan 30 #将该接口下允许VLAN30通过 21 yinzhengjie(config-if)#exit #退出当前接口 22 yinzhengjie(config)#interface range fastEthernet 0/2-5 #同时进入f0/2到f0/5的接口 23 yinzhengjie(config-if-range)#switchport access vlan 30 #允许vlan30通过 24 yinzhengjie(config-if-range)#exit 25 yinzhengjie(config)#exit #退到特权模式中 26 yinzhengjie# 27 %SYS-5-CONFIG_I: Configured from console by console 28 yinzhengjie#show vlan brief 29 30 VLAN Name Status Ports 31 ---- -------------------------------- --------- ------------------------------- 32 1 default active Fa0/6, Fa0/7, Fa0/8, Fa0/9 33 Fa0/10, Fa0/11, Fa0/12, Fa0/13 34 Fa0/14, Fa0/15, Fa0/16, Fa0/17 35 Fa0/18, Fa0/19, Fa0/20, Fa0/21 36 Fa0/22, Fa0/23, Fa0/24 37 30 test_3 active Fa0/1, Fa0/2, Fa0/3, Fa0/4 38 Fa0/5 #观察发现,f0/1到f0/5都从vlan1中跑到了vlan30中去了 39 1002 fddi-default active 40 1003 token-ring-default active 41 1004 fddinet-default active 42 1005 trnet-default active 43 yinzhengjie#

1 yinzhengjie#show vlan brief 2 3 VLAN Name Status Ports 4 ---- -------------------------------- --------- ------------------------------- 5 1 default active Fa0/6, Fa0/7, Fa0/8, Fa0/9 6 Fa0/10, Fa0/11, Fa0/12, Fa0/13 7 Fa0/14, Fa0/15, Fa0/16, Fa0/17 8 Fa0/18, Fa0/19, Fa0/20, Fa0/21 9 Fa0/22, Fa0/23, Fa0/24 10 30 test_3 active Fa0/1, Fa0/2, Fa0/3, Fa0/4 11 Fa0/5 #当前VLAN存在着5个接口 12 1002 fddi-default active 13 1003 token-ring-default active 14 1004 fddinet-default active 15 1005 trnet-default active 16 yinzhengjie#configure terminal 17 Enter configuration commands, one per line. End with CNTL/Z. 18 yinzhengjie(config)#interface range fastEthernet 0/1-5 #同时进入f0/1到f0/5的接口之中 19 yinzhengjie(config-if-range)#no switchport access vlan 30 #不允许vlan30通过 20 yinzhengjie(config-if-range)#exit 21 yinzhengjie(config)#exit 22 yinzhengjie# 23 %SYS-5-CONFIG_I: Configured from console by console 24 25 yinzhengjie#show vlan brief 26 27 VLAN Name Status Ports 28 ---- -------------------------------- --------- ------------------------------- 29 1 default active Fa0/1, Fa0/2, Fa0/3, Fa0/4 30 Fa0/5, Fa0/6, Fa0/7, Fa0/8 31 Fa0/9, Fa0/10, Fa0/11, Fa0/12 32 Fa0/13, Fa0/14, Fa0/15, Fa0/16 33 Fa0/17, Fa0/18, Fa0/19, Fa0/20 34 Fa0/21, Fa0/22, Fa0/23, Fa0/24 35 30 test_3 active #该VLAN的接口全部被清空了,默认被情况的接口又回到了VLAN1中去了 36 1002 fddi-default active 37 1003 token-ring-default active 38 1004 fddinet-default active 39 1005 trnet-default active 40 yinzhengjie#
7.配置vlan的管理IP

1 yinzhengjie(config)#interface vlan 10 #在全局配置模式进入vlan接口 2 yinzhengjie(config-if)# 3 %LINK-5-CHANGED: Interface Vlan10, changed state to up 4 5 %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan10, changed state to up 6 7 yinzhengjie(config-if)#ip add 192.168.10.1 255.255.255.0 #配置管理IP 8 yinzhengjie(config-if)#

1 interface Vlan1 2 no ip address 3 shutdown 4 ! 5 interface Vlan5 6 ip address 192.168.5.1 255.255.255.0 7 ! 8 interface Vlan10 9 ip address 192.168.10.1 255.255.255.0 #我们查看当前是有IP地址的 10 ! 11 line con 0 12 ! 13 line vty 0 4 14 login 15 line vty 5 15 16 login 17 ! 18 ! 19 end 20 yinzhengjie#configure terminal #进入全局配置模式 21 Enter configuration commands, one per line. End with CNTL/Z. 22 yinzhengjie(config)#interface vlan 10 #进入VLAN10的接口 23 yinzhengjie(config-if)#no ip address #情况当前vlan中的IP地址 24 yinzhengjie(config-if)#exit 25 yinzhengjie(config)#exit 26 yinzhengjie# 27 %SYS-5-CONFIG_I: Configured from console by console 28 s 29 % Ambiguous command: "s" 30 yinzhengjie# 31 yinzhengjie#show run #查看当前配置 32 Building configuration... 33 34 Current configuration : 1142 bytes 35 ! 36 version 12.1 37 no service timestamps log datetime msec 38 no service timestamps debug datetime msec 39 no service password-encryption 40 ! 41 hostname yinzhengjie 42 ! 43 ! 44 spanning-tree mode pvst 45 ! 46 interface FastEthernet0/1 47 switchport access vlan 5 48 switchport mode trunk 49 ! 50 interface FastEthernet0/2 51 switchport access vlan 10 52 ! 53 interface FastEthernet0/3 54 ! 55 interface FastEthernet0/4 56 ! 57 interface FastEthernet0/5 58 ! 59 interface FastEthernet0/6 60 ! 61 interface FastEthernet0/7 62 ! 63 interface FastEthernet0/8 64 ! 65 interface FastEthernet0/9 66 ! 67 interface FastEthernet0/10 68 ! 69 interface FastEthernet0/11 70 ! 71 interface FastEthernet0/12 72 ! 73 interface FastEthernet0/13 74 ! 75 interface FastEthernet0/14 76 ! 77 interface FastEthernet0/15 78 ! 79 interface FastEthernet0/16 80 ! 81 interface FastEthernet0/17 82 ! 83 interface FastEthernet0/18 84 ! 85 interface FastEthernet0/19 86 ! 87 interface FastEthernet0/20 88 ! 89 interface FastEthernet0/21 90 ! 91 interface FastEthernet0/22 92 ! 93 interface FastEthernet0/23 94 ! 95 interface FastEthernet0/24 96 ! 97 interface Vlan1 98 no ip address 99 shutdown 100 ! 101 interface Vlan5 102 ip address 192.168.5.1 255.255.255.0 103 ! 104 interface Vlan10 105 no ip address #此时我们发现IP被清空了 106 ! 107 ! 108 line con 0 109 ! 110 line vty 0 4 111 login 112 line vty 5 15 113 login 114 ! 115 ! 116 end 117 118 119 yinzhengjie#
二.简单网络配置练习
小试牛刀:好了,学了以上的命令,我们一起来做一个简单的练习:
1.在交换机上划分vlan,将PC1、PC3加入到vlan2,PC2、PC4加入vlan3,并实现跨交换机的同vlan之间的通讯,如图-1所示:
pc0的cmd窗口测试结果如下:

1 PC>ipconfig 2 3 FastEthernet0 Connection:(default port) 4 Link-local IPv6 Address.........: FE80::20B:BEFF:FEC2:A4BD 5 IP Address......................: 192.168.5.10 6 Subnet Mask.....................: 255.255.255.0 7 Default Gateway.................: 192.168.5.1 8 9 PC>ping 192.168.5.1 #网关地址 10 11 Pinging 192.168.5.1 with 32 bytes of data: 12 13 Reply from 192.168.5.1: bytes=32 time=0ms TTL=255 14 Reply from 192.168.5.1: bytes=32 time=0ms TTL=255 15 16 Ping statistics for 192.168.5.1: 17 Packets: Sent = 2, Received = 2, Lost = 0 (0% loss), 18 Approximate round trip times in milli-seconds: 19 Minimum = 0ms, Maximum = 0ms, Average = 0ms 20 21 Control-C 22 ^C 23 PC>ping 192.168.5.2 #右边的交换机 24 25 Pinging 192.168.5.2 with 32 bytes of data: 26 27 Reply from 192.168.5.2: bytes=32 time=0ms TTL=255 28 29 Ping statistics for 192.168.5.2: 30 Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), 31 Approximate round trip times in milli-seconds: 32 Minimum = 0ms, Maximum = 0ms, Average = 0ms 33 34 Control-C 35 ^C 36 PC>ping 192.168.5.20 #右边的PC机器 37 38 Pinging 192.168.5.20 with 32 bytes of data: 39 40 Reply from 192.168.5.20: bytes=32 time=0ms TTL=128 41 42 Ping statistics for 192.168.5.20: 43 Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), 44 Approximate round trip times in milli-seconds: 45 Minimum = 0ms, Maximum = 0ms, Average = 0ms 46 47 Control-C 48 ^C 49 PC>ping 192.168.10.1 50 51 Pinging 192.168.10.1 with 32 bytes of data: 52 53 54 Ping statistics for 192.168.10.1: 55 Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), 56 57 Control-C 58 ^C 59 PC>ping 192.168.10.10 60 61 Pinging 192.168.10.10 with 32 bytes of data: 62 63 64 Ping statistics for 192.168.10.10: 65 Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), 66 67 Control-C 68 ^C 69 PC>
pc1的cmd窗口测试结果如下:

1 PC> 2 PC>ipconfig 3 4 FastEthernet0 Connection:(default port) 5 Link-local IPv6 Address.........: FE80::230:A3FF:FE0D:B465 6 IP Address......................: 192.168.10.10 7 Subnet Mask.....................: 255.255.255.0 8 Default Gateway.................: 192.168.10.1 9 10 PC>ping 192.168.10.1 #网关地址 11 12 Pinging 192.168.10.1 with 32 bytes of data: 13 14 Reply from 192.168.10.1: bytes=32 time=0ms TTL=255 15 Reply from 192.168.10.1: bytes=32 time=0ms TTL=255 16 17 Ping statistics for 192.168.10.1: 18 Packets: Sent = 2, Received = 2, Lost = 0 (0% loss), 19 Approximate round trip times in milli-seconds: 20 Minimum = 0ms, Maximum = 0ms, Average = 0ms 21 22 Control-C 23 ^C 24 PC>ping 192.168.10.2 #右边交换机的网关地址 25 26 Pinging 192.168.10.2 with 32 bytes of data: 27 28 Reply from 192.168.10.2: bytes=32 time=1ms TTL=255 29 30 Ping statistics for 192.168.10.2: 31 Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), 32 Approximate round trip times in milli-seconds: 33 Minimum = 1ms, Maximum = 1ms, Average = 1ms 34 35 Control-C 36 ^C 37 PC>ping 192.168.10.20 38 39 Pinging 192.168.10.20 with 32 bytes of data: 40 41 Reply from 192.168.10.20: bytes=32 time=0ms TTL=128 42 Reply from 192.168.10.20: bytes=32 time=0ms TTL=128 43 Reply from 192.168.10.20: bytes=32 time=0ms TTL=128 44 Reply from 192.168.10.20: bytes=32 time=1ms TTL=128 45 46 Ping statistics for 192.168.10.20: 47 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 48 Approximate round trip times in milli-seconds: 49 Minimum = 0ms, Maximum = 1ms, Average = 0ms 50 51 PC> 52 PC>ping 192.168.5.1 53 54 Pinging 192.168.5.1 with 32 bytes of data: 55 56 57 Ping statistics for 192.168.5.1: 58 Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), 59 60 Control-C 61 ^C 62 PC>ping 192.168.5.2 63 64 Pinging 192.168.5.2 with 32 bytes of data: 65 66 67 Ping statistics for 192.168.5.2: 68 Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), 69 70 Control-C 71 ^C 72 PC>ping 192.168.5.20 73 74 Pinging 192.168.5.20 with 32 bytes of data: 75 76 77 Ping statistics for 192.168.5.20: 78 Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), 79 80 Control-C 81 ^C 82 PC>
左边交换机的配置如下:

1 Switch#show run 2 Building configuration... 3 4 Current configuration : 1159 bytes 5 ! 6 version 12.1 7 no service timestamps log datetime msec 8 no service timestamps debug datetime msec 9 no service password-encryption 10 ! 11 hostname Switch 12 ! 13 ! 14 spanning-tree mode pvst 15 ! 16 interface FastEthernet0/1 17 ! 18 interface FastEthernet0/2 19 switchport access vlan 2 20 ! 21 interface FastEthernet0/3 22 switchport access vlan 3 23 ! 24 interface FastEthernet0/4 25 ! 26 interface FastEthernet0/5 27 ! 28 interface FastEthernet0/6 29 ! 30 interface FastEthernet0/7 31 ! 32 interface FastEthernet0/8 33 ! 34 interface FastEthernet0/9 35 ! 36 interface FastEthernet0/10 37 switchport mode trunk 38 ! 39 interface FastEthernet0/11 40 ! 41 interface FastEthernet0/12 42 ! 43 interface FastEthernet0/13 44 ! 45 interface FastEthernet0/14 46 ! 47 interface FastEthernet0/15 48 ! 49 interface FastEthernet0/16 50 ! 51 interface FastEthernet0/17 52 ! 53 interface FastEthernet0/18 54 ! 55 interface FastEthernet0/19 56 ! 57 interface FastEthernet0/20 58 ! 59 interface FastEthernet0/21 60 ! 61 interface FastEthernet0/22 62 ! 63 interface FastEthernet0/23 64 ! 65 interface FastEthernet0/24 66 ! 67 interface Vlan1 68 no ip address 69 shutdown 70 ! 71 interface Vlan2 72 ip address 192.168.5.1 255.255.255.0 73 ! 74 interface Vlan3 75 ip address 192.168.10.1 255.255.255.0 76 ! 77 ! 78 line con 0 79 ! 80 line vty 0 4 81 login 82 line vty 5 15 83 login 84 ! 85 ! 86 end 87 88 89 Switch#
右边交换机的配置如下:

1 Switch#show running-config 2 Building configuration... 3 4 Current configuration : 1227 bytes 5 ! 6 version 12.1 7 no service timestamps log datetime msec 8 no service timestamps debug datetime msec 9 no service password-encryption 10 ! 11 hostname Switch 12 ! 13 ! 14 spanning-tree mode pvst 15 ! 16 interface FastEthernet0/1 17 ! 18 interface FastEthernet0/2 19 switchport access vlan 2 20 ! 21 interface FastEthernet0/3 22 switchport access vlan 3 23 ! 24 interface FastEthernet0/4 25 ! 26 interface FastEthernet0/5 27 ! 28 interface FastEthernet0/6 29 ! 30 interface FastEthernet0/7 31 ! 32 interface FastEthernet0/8 33 ! 34 interface FastEthernet0/9 35 ! 36 interface FastEthernet0/10 37 switchport mode trunk 38 ! 39 interface FastEthernet0/11 40 ! 41 interface FastEthernet0/12 42 ! 43 interface FastEthernet0/13 44 ! 45 interface FastEthernet0/14 46 ! 47 interface FastEthernet0/15 48 ! 49 interface FastEthernet0/16 50 ! 51 interface FastEthernet0/17 52 ! 53 interface FastEthernet0/18 54 ! 55 interface FastEthernet0/19 56 ! 57 interface FastEthernet0/20 58 ! 59 interface FastEthernet0/21 60 ! 61 interface FastEthernet0/22 62 ! 63 interface FastEthernet0/23 64 ! 65 interface FastEthernet0/24 66 ! 67 interface Vlan1 68 no ip address 69 shutdown 70 ! 71 interface Vlan2 72 ip address 192.168.5.2 255.255.255.0 73 ! 74 interface Vlan3 75 ip address 192.168.10.2 255.255.255.0 76 ! 77 interface Vlan10 78 no ip address 79 ! 80 interface Vlan20 81 no ip address 82 ! 83 ! 84 line con 0 85 ! 86 line vty 0 4 87 login 88 line vty 5 15 89 login 90 ! 91 ! 92 end 93 94 95 Switch#
实现过程其实很简单只需要分别在2个交换机上创建相同的vlan,然后讲两个交换机相连的口设置为trunk口,将pc机遇交换机的直连的端口设置为access口即可。这样就实现了2个交换机中相同VLAN可以互通不同VLAM不能互通的功能,但是如果你非要让同一个交换机中的2个VLAN互通的话其实也可以的,需要在三层交换机上开启路由功能,注意:这个功能傻瓜交换机是不支持的哟!所以你要在实际生产环境中要考虑你的设备是否支持该功能哟。再去考虑交互机VLAN互通的问题。
2.配置DHCP服务器
在路由器上配置DHCP服务为客户端自动分配IP地址架构图如下:
我是在路由器上做的配置,让路由器当DHCP服务器,配置过程如下:

1 yinzhengjie>en #进入特权模式 2 yinzhengjie#configure terminal #进入全局配置模式 3 Enter configuration commands, one per line. End with CNTL/Z. 4 yinzhengjie(config)#interface fastEthernet 0/0 #进入接入的网口地址 5 yinzhengjie(config-if)#ip address 172.16.1.254 255.255.255.0 #为该网口配置一个网管地址 6 yinzhengjie(config-if)#no shutdown #开启这个端口 7 8 yinzhengjie(config-if)# 9 %LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up 10 11 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up 12 13 yinzhengjie(config)#ip dhcp pool wancheng #添加一个地址池起别名为“wancheng” 14 yinzhengjie(dhcp-config)#network 172.16.1.0 255.255.255.0 #分配地址池 15 yinzhengjie(dhcp-config)#default-router 172.16.1.254 #指定分配地址的网关地址 16 yinzhengjie(dhcp-config)#dns-server 219.141.136.10 #配置DNS服务器 17 yinzhengjie(dhcp-config)#exit #退出DHCP配置模式 18 yinzhengjie(config)#ip dhcp excluded-address 172.16.1.1 172.16.1.100 #配置预留的IP,意思是从172.16.1.1~172.16.1.100都不分配 19 yinzhengjie(config)#
测试结果如下:
pc0:
pc1:
在路由器上查看目前分配IP地址的情况如下:

1 yinzhengjie#show run #查看配置 2 Building configuration... 3 4 Current configuration : 602 bytes 5 ! 6 version 12.2 7 no service timestamps log datetime msec 8 no service timestamps debug datetime msec 9 no service password-encryption 10 ! 11 hostname yinzhengjie 12 ! 13 ! 14 ! 15 ! 16 ip dhcp excluded-address 172.16.1.1 172.16.1.100 17 ! 18 ip dhcp pool wancheng #DHCP配置信息 19 network 172.16.1.0 255.255.255.0 20 default-router 172.16.1.254 21 dns-server 219.141.136.10 22 ! 23 ! 24 ! 25 ! 26 ! 27 ! 28 ! 29 ! 30 ! 31 ! 32 ! 33 ! 34 ! 35 interface FastEthernet0/0 36 ip address 172.16.1.254 255.255.255.0 37 duplex auto 38 speed auto 39 ! 40 interface FastEthernet0/1 41 no ip address 42 duplex auto 43 speed auto 44 shutdown 45 ! 46 ip classless 47 ! 48 ! 49 ! 50 ! 51 ! 52 ! 53 ! 54 line con 0 55 ! 56 line aux 0 57 ! 58 line vty 0 4 59 login 60 ! 61 ! 62 ! 63 end 64 65 yinzhengjie#show ip dhcp binding #查看当前地址的在线情况 66 IP address Client-ID/ Lease expiration Type 67 Hardware address 68 172.16.1.101 00E0.A357.0E96 -- Automatic 69 172.16.1.102 0009.7C6D.C274 -- Automatic 70 yinzhengjie#
3.单臂路由配置
要求VLAN20和VLAN30可以互相访问,需要在交换机设置access口(与pc机器相连)和trunk口(与路由相连)
pc0测试环境如下:

1 PC>ipconfig #查看本机地址 2 3 FastEthernet0 Connection:(default port) 4 Link-local IPv6 Address.........: FE80::2D0:BAFF:FEE4:777E 5 IP Address......................: 192.168.20.1 6 Subnet Mask.....................: 255.255.255.0 7 Default Gateway.................: 192.168.20.254 8 9 PC>ping 192.168.20.2 #vlan20的另外一台pc1 10 11 Pinging 192.168.20.2 with 32 bytes of data: 12 13 Reply from 192.168.20.2: bytes=32 time=0ms TTL=128 14 Reply from 192.168.20.2: bytes=32 time=0ms TTL=128 15 Reply from 192.168.20.2: bytes=32 time=0ms TTL=128 16 Reply from 192.168.20.2: bytes=32 time=0ms TTL=128 17 18 Ping statistics for 192.168.20.2: 19 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 20 Approximate round trip times in milli-seconds: 21 Minimum = 0ms, Maximum = 0ms, Average = 0ms 22 23 PC>ping 172.16.30.254 #vlan30的网关地址 24 25 Pinging 172.16.30.254 with 32 bytes of data: 26 27 Reply from 172.16.30.254: bytes=32 time=1ms TTL=255 28 Reply from 172.16.30.254: bytes=32 time=1ms TTL=255 29 Reply from 172.16.30.254: bytes=32 time=0ms TTL=255 30 Reply from 172.16.30.254: bytes=32 time=0ms TTL=255 31 32 Ping statistics for 172.16.30.254: 33 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 34 Approximate round trip times in milli-seconds: 35 Minimum = 0ms, Maximum = 1ms, Average = 0ms 36 37 PC>ping 172.16.30.1 #VLAN30的机器 38 39 Pinging 172.16.30.1 with 32 bytes of data: 40 41 Request timed out. 42 Reply from 172.16.30.1: bytes=32 time=0ms TTL=127 43 Reply from 172.16.30.1: bytes=32 time=0ms TTL=127 44 Reply from 172.16.30.1: bytes=32 time=0ms TTL=127 45 46 Ping statistics for 172.16.30.1: 47 Packets: Sent = 4, Received = 3, Lost = 1 (25% loss), 48 Approximate round trip times in milli-seconds: 49 Minimum = 0ms, Maximum = 0ms, Average = 0ms 50 51 PC>
交换机配置如下:

1 yinzhengjie>en 2 yinzhengjie#vlan database 3 % Warning: It is recommended to configure VLAN from config mode, 4 as VLAN database mode is being deprecated. Please consult user 5 documentation for configuring VTP/VLAN in config mode. 6 7 yinzhengjie(vlan)#vlan 20 name test_1 8 VLAN 20 added: 9 Name: test_1 10 yinzhengjie(vlan)#vlan 30 name test_2 11 VLAN 30 added: 12 Name: test_2 13 yinzhengjie(vlan)#exit 14 APPLY completed. 15 Exiting.... 16 yinzhengjie#configure terminal 17 Enter configuration commands, one per line. End with CNTL/Z. 18 yinzhengjie(config)#interface range fastEthernet 0/1-2 19 yinzhengjie(config-if-range)#switchport access vlan 20 20 yinzhengjie(config-if-range)#exit 21 yinzhengjie(config)#interface range fastEthernet 0/3-4 22 yinzhengjie(config-if-range)#switchport access vlan 30 23 yinzhengjie(config-if-range)#exit 24 yinzhengjie(config)#interface fastEthernet 0/10 25 yinzhengjie(config-if)#switchport mode trunk 26 27 yinzhengjie(config-if)# 28 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/10, changed state to down 29 30 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/10, changed state to up 31 32 yinzhengjie(config-if)# 33 yinzhengjie(config-if)# 34 yinzhengjie(config-if)#

1 yinzhengjie#show run 2 Building configuration... 3 4 Current configuration : 1107 bytes 5 ! 6 version 12.1 7 no service timestamps log datetime msec 8 no service timestamps debug datetime msec 9 no service password-encryption 10 ! 11 hostname yinzhengjie 12 ! 13 ! 14 spanning-tree mode pvst 15 ! 16 interface FastEthernet0/1 17 switchport access vlan 20 18 ! 19 interface FastEthernet0/2 20 switchport access vlan 20 21 ! 22 interface FastEthernet0/3 23 switchport access vlan 30 24 ! 25 interface FastEthernet0/4 26 switchport access vlan 30 27 ! 28 interface FastEthernet0/5 29 ! 30 interface FastEthernet0/6 31 ! 32 interface FastEthernet0/7 33 ! 34 interface FastEthernet0/8 35 ! 36 interface FastEthernet0/9 37 ! 38 interface FastEthernet0/10 39 switchport mode trunk 40 ! 41 interface FastEthernet0/11 42 ! 43 interface FastEthernet0/12 44 ! 45 interface FastEthernet0/13 46 ! 47 interface FastEthernet0/14 48 ! 49 interface FastEthernet0/15 50 ! 51 interface FastEthernet0/16 52 ! 53 interface FastEthernet0/17 54 ! 55 interface FastEthernet0/18 56 ! 57 interface FastEthernet0/19 58 ! 59 interface FastEthernet0/20 60 ! 61 interface FastEthernet0/21 62 ! 63 interface FastEthernet0/22 64 ! 65 interface FastEthernet0/23 66 ! 67 interface FastEthernet0/24 68 ! 69 interface Vlan1 70 no ip address 71 shutdown 72 ! 73 ! 74 line con 0 75 ! 76 line vty 0 4 77 login 78 line vty 5 15 79 login 80 ! 81 ! 82 end 83 84 85 yinzhengjie#
路由器配置如下:

1 yinzhengjie#configure terminal 2 Enter configuration commands, one per line. End with CNTL/Z. 3 yinzhengjie(config)#interface fastEthernet 0/0 4 yinzhengjie(config-if)#no shutdown 5 yinzhengjie(config-if)#exit 6 yinzhengjie(config)#interface fastEthernet 0/0.1 7 yinzhengjie(config-subif)#encapsulation dot1Q 20 #配置trunk的封装协议,如果不配置会无法配置成功IP地址的哟~ 8 yinzhengjie(config-subif)#ip address 192.168.20.254 255.255.255.0 9 yinzhengjie(config-subif)#exit 10 yinzhengjie(config)#interface fastEthernet 0/0.2 11 yinzhengjie(config-subif)#encapsulation dot1Q 30 12 yinzhengjie(config-subif)#ip address 172.16.30.254 255.255.255.0 13 yinzhengjie(config-subif)#exit 14 yinzhengjie(config)#exit 15 yinzhengjie# 16 %SYS-5-CONFIG_I: Configured from console by console 17 18 yinzhengjie#show run 19 Building configuration... 20 21 Current configuration : 528 bytes 22 ! 23 version 12.2 24 no service timestamps log datetime msec 25 no service timestamps debug datetime msec 26 no service password-encryption 27 ! 28 hostname yinzhengjie 29 ! 30 ! 31 ! 32 ! 33 ! 34 ! 35 ! 36 ! 37 ! 38 ! 39 ! 40 ! 41 ! 42 ! 43 ! 44 ! 45 ! 46 ! 47 interface FastEthernet0/0 48 no ip address 49 duplex auto 50 speed auto 51 ! 52 interface FastEthernet0/0.1 53 encapsulation dot1Q 20 54 ip address 192.168.20.254 255.255.255.0 55 ! 56 interface FastEthernet0/0.2 57 encapsulation dot1Q 30 58 ip address 172.16.30.254 255.255.255.0 59 ! 60 ip classless 61 ! 62 ! 63 ! 64 ! 65 ! 66 ! 67 ! 68 line con 0 69 ! 70 line aux 0 71 ! 72 line vty 0 4 73 login 74 ! 75 ! 76 ! 77 end 78 79 80 yinzhengjie# 81 82 ‘’‘ 83 扩充: 84 1:在路由器上配置trunk的封装协议的命令: 85 encapsulation [isl/dot1q] vlan# 86 2:上面的路由器配置了dot1q中继封装,20是vlan 20。 87 3:Trunk的封装类型一共有 ISL和802.1Q两种,而802.1Q在输入的时候则为dot1q 88 89 ’‘’

1 yinzhengjie#show run 2 Building configuration... 3 4 Current configuration : 528 bytes 5 ! 6 version 12.2 7 no service timestamps log datetime msec 8 no service timestamps debug datetime msec 9 no service password-encryption 10 ! 11 hostname yinzhengjie 12 ! 13 ! 14 ! 15 ! 16 ! 17 ! 18 ! 19 ! 20 ! 21 ! 22 ! 23 ! 24 ! 25 ! 26 ! 27 ! 28 ! 29 ! 30 interface FastEthernet0/0 31 no ip address 32 duplex auto 33 speed auto 34 ! 35 interface FastEthernet0/0.1 36 encapsulation dot1Q 20 37 ip address 192.168.20.254 255.255.255.0 38 ! 39 interface FastEthernet0/0.2 40 encapsulation dot1Q 30 41 ip address 172.16.30.254 255.255.255.0 42 ! 43 ip classless 44 ! 45 ! 46 ! 47 ! 48 ! 49 ! 50 ! 51 line con 0 52 ! 53 line aux 0 54 ! 55 line vty 0 4 56 login 57 ! 58 ! 59 ! 60 end 61 62 63 yinzhengjie#
4.如下图所示,通过动态路由RIP实现三个路由器之间的全网互通。
pc0测试结果如下:

1 PC> 2 PC>ping 192.168.1.254 #网关地址 3 4 Pinging 192.168.1.254 with 32 bytes of data: 5 6 Reply from 192.168.1.254: bytes=32 time=1ms TTL=255 7 Reply from 192.168.1.254: bytes=32 time=1ms TTL=255 8 Reply from 192.168.1.254: bytes=32 time=0ms TTL=255 9 Reply from 192.168.1.254: bytes=32 time=1ms TTL=255 10 11 Ping statistics for 192.168.1.254: 12 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 13 Approximate round trip times in milli-seconds: 14 Minimum = 0ms, Maximum = 1ms, Average = 0ms 15 16 PC>ping 192.168.2.1 #路由器0/1地址 17 18 Pinging 192.168.2.1 with 32 bytes of data: 19 20 Reply from 192.168.2.1: bytes=32 time=1ms TTL=255 21 Reply from 192.168.2.1: bytes=32 time=0ms TTL=255 22 Reply from 192.168.2.1: bytes=32 time=0ms TTL=255 23 Reply from 192.168.2.1: bytes=32 time=0ms TTL=255 24 25 Ping statistics for 192.168.2.1: 26 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 27 Approximate round trip times in milli-seconds: 28 Minimum = 0ms, Maximum = 1ms, Average = 0ms 29 30 PC>ping 192.168.3.1 #中间路由地址 31 32 Pinging 192.168.3.1 with 32 bytes of data: 33 34 Reply from 192.168.3.1: bytes=32 time=1ms TTL=254 35 Reply from 192.168.3.1: bytes=32 time=0ms TTL=254 36 Reply from 192.168.3.1: bytes=32 time=2ms TTL=254 37 Reply from 192.168.3.1: bytes=32 time=0ms TTL=254 38 39 Ping statistics for 192.168.3.1: 40 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 41 Approximate round trip times in milli-seconds: 42 Minimum = 0ms, Maximum = 2ms, Average = 0ms 43 44 PC>ping 192.168.4.1 #pc1机器地址 45 46 Pinging 192.168.4.1 with 32 bytes of data: 47 48 Reply from 192.168.4.1: bytes=32 time=11ms TTL=125 49 Reply from 192.168.4.1: bytes=32 time=1ms TTL=125 50 Reply from 192.168.4.1: bytes=32 time=0ms TTL=125 51 Reply from 192.168.4.1: bytes=32 time=0ms TTL=125 52 53 Ping statistics for 192.168.4.1: 54 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 55 Approximate round trip times in milli-seconds: 56 Minimum = 0ms, Maximum = 11ms, Average = 3ms 57 58 PC>
路由器配置信息

1 yinzhengjie(config)#interface fastEthernet 0/0 2 yinzhengjie(config-if)#no shutdown 3 %LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up 4 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up 5 yinzhengjie(config-if)#ip address 192.168.1.254 255.255.255.0 6 yinzhengjie(config-if)#exit 7 yinzhengjie(config)#interface fastEthernet 0/1 8 yinzhengjie(config-if)#no shutdown 9 %LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up 10 yinzhengjie(config-if)#ip address 192.168.2.1 255.255.255.0 11 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up 12 yinzhengjie(config-if)#ex 13 yinzhengjie(config)#router rip #进入动态路由配置模式 14 yinzhengjie(config-router)#version 2 15 yinzhengjie(config-router)#no auto-summary 16 yinzhengjie(config-router)#network 192.168.1.0 #与路由直连网段 17 yinzhengjie(config-router)#network 192.168.2.0 #与路由直连网段 18 yinzhengjie#show run 19 Building configuration... 20 21 Current configuration : 538 bytes 22 ! 23 version 12.2 24 no service timestamps log datetime msec 25 no service timestamps debug datetime msec 26 no service password-encryption 27 ! 28 hostname yinzhengjie 29 ! 30 ! 31 ! 32 ! 33 ! 34 ! 35 ! 36 ! 37 ! 38 ! 39 ! 40 ! 41 ! 42 ! 43 ! 44 ! 45 ! 46 ! 47 interface FastEthernet0/0 48 ip address 192.168.1.254 255.255.255.0 49 duplex auto 50 speed auto 51 ! 52 interface FastEthernet0/1 53 ip address 192.168.2.1 255.255.255.0 54 duplex auto 55 speed auto 56 ! 57 router rip 58 version 2 59 network 192.168.1.0 60 network 192.168.2.0 61 no auto-summary 62 ! 63 ip classless 64 ! 65 ! 66 ! 67 ! 68 ! 69 ! 70 ! 71 line con 0 72 ! 73 line aux 0 74 ! 75 line vty 0 4 76 login 77 ! 78 ! 79 ! 80 end 81 82 83 yinzhengjie#

1 Router>en 2 Router#configure terminal 3 Enter configuration commands, one per line. End with CNTL/Z. 4 Router(config)#interface fastEthernet 0/0 5 Router(config-if)#no shutdown 6 %LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up 7 8 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up 9 Router(config-if)#ip address 192.168.2.2 255.255.255.0 10 Router(config-if)#exit 11 Router(config)#interface fastEthernet 0/1 12 Router(config-if)#no shutdown 13 14 Router(config-if)# 15 %LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up 16 Router(config-if)#ip address 192.168.3.1 255.255.255.0 17 Router(config-if)# 18 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up 19 20 Router(config-if)#ex 21 Router(config)#router rip 22 Router(config-router)#version 2 23 Router(config-router)#no auto-summary 24 Router(config-router)#network 192.168.2.0 25 Router(config-router)#network 192.168.3.0 26 Router(config-router)#

1 yinzhengjie(config)#interface fastEthernet 0/0 2 yinzhengjie(config-if)#no shutdown 3 %LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up 4 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up 5 yinzhengjie(config-if)#ip address 192.168.3.2 255.255.255.0 6 yinzhengjie(config-if)#exit 7 yinzhengjie(config)#interface fastEthernet 0/1 8 yinzhengjie(config-if)#no shutdown 9 yinzhengjie(config-if)# 10 %LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up 11 %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up 12 yinzhengjie(config-if)#ip address 192.168.4.254 255.255.255.0 13 yinzhengjie(config-if)#exit 14 yinzhengjie(config)#router rip 15 yinzhengjie(config-router)#version 2 16 yinzhengjie(config-router)#no auto-summary 17 yinzhengjie(config-router)#network 192.168.3.0 18 yinzhengjie(config-router)#network 192.168.4.0 19 yinzhengjie(config-router)#