A VLAN (Virtual Local Area Network) is a logical grouping of devices that share the same broadcast domain. VLANs can improve network performance, security, and management by separating different types of traffic or users. To assign a switch port to a VLAN, you need to configure the port as either an access port or a trunk port. An access port can carry traffic for only one VLAN, while a trunk port can carry traffic for multiple VLANs.
Prerequisites
Before you begin, make sure that you have the following:
- A Cisco switch that supports VLANs and trunking.
- A console cable or a Telnet/SSH connection to the switch.
- The VLAN ID and name that you want to create and assign to the port.
- The port number and mode (access or trunk) that you want to configure.
Steps
Log in to the switch and enter privileged EXEC mode. You can do this by typing
enable
and entering the enable password if prompted.Enter global configuration mode by typing
configure terminal
.Create the VLAN that you want to use by typing
vlan vlan-id
, wherevlan-id
is a number between 1 and 4094. For example, to create VLAN 10, typevlan 10
.Optionally, assign a name to the VLAN by typing
name vlan-name
, wherevlan-name
is a descriptive name for the VLAN. For example, to name VLAN 10 as Sales, typename Sales
.Exit VLAN configuration mode by typing
exit
.Select the interface that you want to assign to the VLAN by typing
interface interface-id
, whereinterface-id
is the name of the interface. For example, to select FastEthernet 0/1, typeinterface fastethernet 0/1
.Configure the interface as an access port or a trunk port by typing
switchport mode mode
, wheremode
is eitheraccess
ortrunk
. For example, to configure the interface as an access port, typeswitchport mode access
.If you configured the interface as an access port, assign it to the VLAN by typing
switchport access vlan vlan-id
, wherevlan-id
is the VLAN number that you created in step 3. For example, to assign the interface to VLAN 10, typeswitchport access vlan 10
.If you configured the interface as a trunk port, you can optionally specify which VLANs are allowed or not allowed on the trunk by typing
switchport trunk allowed vlan {add | all | except | remove | none} vlan-list
, wherevlan-list
is a comma-separated list of VLAN numbers or ranges. For example, to allow only VLANs 10 and 20 on the trunk, typeswitchport trunk allowed vlan 10,20
.Exit interface configuration mode by typing
exit
.Save the configuration by typing
copy running-config startup-config
.
Verification
To verify that the interface is assigned to the VLAN, you can use the following commands:
show vlan
– This command displays information about the VLANs on the switch, such as the VLAN ID, name, status, and ports.show interfaces switchport
– This command displays information about the switchport configuration of the interfaces, such as the mode, access VLAN, trunk VLANs, and encapsulation.show mac address-table
– This command displays the MAC address table of the switch, which shows the MAC addresses and VLANs of the devices connected to the switch.
Example
The following example shows how to create VLAN 10 and VLAN 20, name them as Sales and Marketing, and assign FastEthernet 0/1 to VLAN 10 as an access port and FastEthernet 0/2 to VLAN 20 as a trunk port that allows only VLANs 10 and 20.
Switch> enable
Switch# configure terminal
Switch(config)# vlan 10
Switch(config-vlan)# name Sales
Switch(config-vlan)# exit
Switch(config)# vlan 20
Switch(config-vlan)# name Marketing
Switch(config-vlan)# exit
Switch(config)# interface fastethernet 0/1
Switch(config-if)# switchport mode access
Switch(config-if)# switchport access vlan 10
Switch(config-if)# exit
Switch(config)# interface fastethernet 0/2
Switch(config-if)# switchport mode trunk
Switch(config-if)# switchport trunk allowed vlan 10,20
Switch(config-if)# exit
Switch(config)# copy running-config startup-config