Hi everyone, the switch ports in an office are usually wired to network wall sockets. If those ports are left unprotected, anyone can unplug a PC, plug in their own laptop, and get straight onto the office network. Port security on a Cisco switch prevents this by remembering the MAC address of the legitimate device on each port, then shutting the port down when an unknown device is plugged in.
In this article I practice port security in sticky mode in Cisco Packet Tracer 8.2.1, including a simulated intruder laptop and how to recover a port that has been locked. If you have never worked in a switch CLI before, read my guide on Cisco switch configuration in Packet Tracer first.
The Topology and Cables Used
One switch for the office LAN

The topology this time is simple and has no router, because port security works on the switch. The devices are 1 Cisco 2960-24TT switch, 3 office PCs, and 1 laptop that plays the role of the intruder.

The teal box is the office LAN on the 192.168.1.0/24 network. The red box holds the intruder laptop with the IP 192.168.1.50. I gave it an IP on the same network on purpose, so it is clear that what blocks it is port security and not a different subnet.
PC0 FastEthernet0 -> Switch0 FastEthernet0/1 (straight)
PC1 FastEthernet0 -> Switch0 FastEthernet0/2 (straight)
PC2 FastEthernet0 -> Switch0 FastEthernet0/3 (straight)
Laptop0 -> belum tersambungEvery cable is Copper Straight-Through, because a PC and a switch are different kinds of devices. The laptop is not connected yet. The PC IPs are 192.168.1.10, 192.168.1.11, and 192.168.1.12 with the subnet mask 255.255.255.0. You can leave the gateway empty, since all devices are on the same network.
Set the PC0 IP

Click PC0, which I marked with a red circle.

Click the Desktop tab. We will use IP Configuration to set the IP and Command Prompt for the ping tests later.

Enter 192.168.1.10 as the IPv4 Address and 255.255.255.0 as the Subnet Mask. Repeat this on PC1 with 192.168.1.11, on PC2 with 192.168.1.12, and on Laptop0 with 192.168.1.50.
Configuring Port Security on the Switch
Open the Switch0 CLI

Close the PC window, then click Switch0 at the top of the topology.

In the Switch0 window, click the CLI tab, then press Enter until the Switch> prompt appears.
Enable port security on Fa0/1 to Fa0/3

enable
configure terminal
interface range fastEthernet0/1-3
switchport mode access
switchport port-security
switchport port-security maximum 1
switchport port-security mac-address sticky
switchport port-security violation shutdown
end
write memoryPort security can only be enabled on an access port, which is why the switchport mode access line comes first. Maximum 1 means only one MAC address is allowed to use each port.
Mac-address sticky tells the switch to automatically record the first MAC address it sees on that port and save it to the configuration. That way we do not have to type the MAC of every PC by hand. Violation shutdown means that if a different MAC shows up, the port is shut down right away.
Letting the Switch Learn the PC MAC Addresses
Send traffic from the PCs

Open the Command Prompt on PC0 and ping PC1 and PC2.
ping 192.168.1.11
ping 192.168.1.12These pings make all three PCs send packets, so the switch can record the MAC address of each one.
Check the locked MAC addresses

Go back to the Switch0 CLI and run the following command.
show port-security addressEach of the three ports now has one MAC address of type SecureSticky. From now on, Fa0/1 only accepts PC0, Fa0/2 only accepts PC1, and Fa0/3 only accepts PC2.
Simulating an Intruder Laptop
Unplug PC0 and plug in the laptop

In the real world, an intruder unplugs the cable from a PC and plugs it into their own laptop. In Packet Tracer I remove the PC0 cable with the Delete tool, then connect a new straight cable from the laptop’s FastEthernet0 to Fa0/1 on the switch. The laptop sits in the red intruder box.
Warning: Before using the Delete tool, click an empty area first so no device is selected. If Switch0 is still selected, Packet Tracer immediately offers to delete the switch. Click Cancel if you see a prompt like that.
The laptop cannot get onto the network

Open the Command Prompt on Laptop0 and ping PC1.
ping 192.168.1.11Every reply is Request timed out. As soon as the first frame from the laptop arrives on Fa0/1, the switch sees a MAC address that does not match the locked PC0 MAC, so it shuts the port down.

In the topology, the cable end at Fa0/1 turns into a red triangle, which means the port is down.

If you zoom in, you can see that only Fa0/1 is red, while Fa0/2 and Fa0/3 stay green.
Check the violation status

In the Switch0 CLI, run the following commands.
show port-security interface fastEthernet0/1
show interfaces fastEthernet0/1 statusThe Port Status changes to Secure-shutdown, the Security Violation Count is 1, and the Last Source Address shows the MAC address of the intruder laptop. The interface status of Fa0/1 is err-disabled.

A port in the err-disabled state does not come back up on its own, even after the laptop is unplugged. The admin has to bring it back up manually.
Recovering an Err-disabled Port
Bring the port back up after the real PC is reconnected

Remove the laptop cable, connect the PC0 cable to Fa0/1 again, then type the following commands in the Switch0 CLI.
configure terminal
interface fastEthernet0/1
shutdown
no shutdown
end
show interfaces fastEthernet0/1 statusRunning shutdown followed by no shutdown resets the port. The status goes back to connected and PC0 works again, because its MAC address is the one saved by sticky.
Common Problems and How to Fix Them
Command rejected appears when typing switchport port-security
The port is still in dynamic mode. Type switchport mode access on that interface first, then enable port security.
The port goes down again right after no shutdown
The connected device is still not the one whose MAC address was saved. Check the Last Source Address in show port-security interface, and make sure the correct device is plugged in.
The official PC was replaced and needs to use the same port
Remove the old MAC address with the command clear port-security sticky interface fastEthernet0/1, then run shutdown and no shutdown on that port. The switch will learn the MAC address of the new PC.
At this point the switch ports are protected from unknown devices being plugged in at random. In the next article we cover VTP, which is how you automatically share the VLAN list from one switch to the others. If you run into any other error while trying this, just leave a comment below.












