1、打开Cisco Packet Tracer,点击【交换机】,选择第六个图标3560交换机,按住鼠标左键拖动到工作区。只有这个交换机及以上级别的才具备DHCP功能,我们之前讲的2960只可以划分Vlan,不支持DHCP服务
2、在工作区内拖入3台电脑,用直通线和交换机连接
3、打开交换机属性界面,在命令行中输入代码,划分Vlan 10和Vlan 20两个Vlan代码如下:【Switch>enableSwitch#vlan database% Warning: It is recommended to configure VLAN from config mode, as VLAN database mode is being deprecated. Please consult user documentation for configuring VTP/VLAN in config mode.Switch(vlan)#vlan 10 name v10VLAN 10 modified: Name: v10Switch(vlan)#vlan 20 name v20VLAN 20 modified: Name: v20Switch(vlan)#exit】
4、设置vlan10和Vlan20的默认地址池和网关dns,代码如下:【Switch(config)#ip dhcp pool vlan10 *设置相应的VLANSwitch(dhcp-config)#default-router 192.168.1.254 *设置该地址池的默认网关Switch(dhcp-config)#network 192.168.1.0 255.255.255.0 *设置分配的地址池Switch(dhcp-config)#dns-server 202.1.1.1 *设置该地址池的默认DNS地址。Switch(dhcp-config)#ip dhcp pool vlan20Switch(dhcp-config)#default-router 192.168.2.254Switch(dhcp-config)#network 192.168.2.0 255.255.255.0Switch(dhcp-config)#dns-server 202.1.1.2Switch(dhcp-config)#ip dhcp pool vlan30Switch(dhcp-config)#default-router 192.168.3.254Switch(dhcp-config)#network 192.168.3.0 255.255.255.0Switch(dhcp-config)#exit】
5、将交换机0/1和0/2端口加入Vlan10;0/3端口加入Vlan20,代码如下【Switch(config)#interface range f0/1 -2Switch(config-if-range)#switchport access vlan 10Switch(config-if-range)#interface range f0/3Switch(config-if-range)#switchport access vlan 20】
6、配置完成,我们打开主机0,让其主动获取ip,点击【ip配置】
7、在同一Vlan的主机0和主机1可以ping通
8、下面附上全部代码【交换机的配置:Switch>enableSwitch#vlan database% Warning: It is recommended to configure VLAN from config mode, as VLAN database mode is being deprecated. Please consult user documentation for configuring VTP/VLAN in config mode.Switch(vlan)#vlan 10 name v10VLAN 10 modified: Name: v10Switch(vlan)#vlan 20 name v20VLAN 20 modified: Name: v20Switch(vlan)#vlan 30 name v30VLAN 30 modified: Name: v30Switch(vlan)#exitAPPLY completed.Exiting....Switch#Switch(config)#ip dhcp pool vlan10 *设置相应的VLANSwitch(dhcp-config)#default-router 192.168.1.254 *设置该地址池的默认网关Switch(dhcp-config)#network 192.168.1.0 255.255.255.0 *设置分配的地址池Switch(dhcp-config)#dns-server 202.1.1.1 *设置该地址池的默认DNS地址。Switch(dhcp-config)#ip dhcp pool vlan20Switch(dhcp-config)#default-router 192.168.2.254Switch(dhcp-config)#network 192.168.2.0 255.255.255.0Switch(dhcp-config)#dns-server 202.1.1.2Switch(dhcp-config)#ip dhcp pool vlan30Switch(dhcp-config)#default-router 192.168.3.254Switch(dhcp-config)#network 192.168.3.0 255.255.255.0Switch(dhcp-config)#exitSwitch(config)#ip dhcp excluded-address 192.168.3.2 192.168.3.10 *设置保留地址,此条命令不是保留192.168.3.2与192.168.3.10两个地址,而是保留192.168.3.2至192.168.3.10的全部地址。Switch(config)#ip dhcp excluded-address 192.168.2.1 *保留单一的一个地址。Switch(config)#Switch(config)#interface range f 0/1 -24Switch(config-if-range)#spanning-tree portfastSwitch(config-if-range)#exitSwitch(config)#int f 0/1Switch(config-if)#switchport access vlan 10Switch(config-if)#int f 0/2Switch(config-if)#switchport access vlan 20Switch(config-if)#int range f 0/3 -5Switch(config-if-range)#switchport access vlan 30Switch(config-if-range)#exitSwitch(config)#Switch(config)#interface vlan 10%LINK-5-CHANGED: Interface Vlan10, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan10, changed state to upSwitch(config-if)#ip add 192.168.1.254 255.255.255.0Switch(config-if)#interface vlan 20%LINK-5-CHANGED: Interface Vlan20, changed state to upSwitch(config-if)#ip add 192.168.2.254 255.255.255.0Switch(config-if)#interface vlan 30%LINK-5-CHANGED: Interface Vlan30, changed state to upSwitch(config-if)#ip add 192.168.3.254 255.255.255.0Switch(config-if)#exitSwitch(config)#ip routing】