switchport port-security mac-address [mac|sticky]
Configures a specific secure MAC address or enables sticky learning on a switchport for port security.
Definition: switchport port-security mac-address [mac|sticky] is a Cisco IOS interface config command. Configures a specific secure MAC address or enables sticky learning on a switchport for port security.
Overview
The `switchport port-security mac-address` command is a fundamental tool in Cisco IOS for enforcing port security on switch access ports. It allows a network administrator to statically define which MAC addresses are permitted to communicate through a specific switchport, or to enable sticky learning where the switch dynamically learns and remembers MAC addresses. This command is critical for mitigating Layer 2 attacks such as MAC flooding, where an attacker floods the switch with frames containing spoofed MAC addresses to overflow the MAC address table and force the switch into fail-open mode (hub behavior).
By limiting the number of allowed MAC addresses per port, port security prevents unauthorized devices from connecting to the network, enhancing overall security posture. The command is typically used on access ports connected to end devices like PCs, printers, or IP phones, where the number of expected MAC addresses is known and limited. It is an alternative to more complex solutions like 802.1X authentication, which requires a RADIUS server, or MAC authentication bypass (MAB), which is often used in conjunction with 802.1X.
Port security is simpler to configure but less flexible; it is best suited for environments where devices are static and predictable. In the broader configuration workflow, port security is often one of several security features applied to switchports, alongside features like BPDU guard, root guard, and DHCP snooping. The command operates in interface configuration mode and requires the port to be configured as an access port (using `switchport mode access`) before enabling port security.
When a violation occurs (e.g., a new MAC address appears after the maximum limit is reached), the switch can be configured to protect (drop traffic), restrict (drop and log), or shutdown (error-disable the port). The `mac-address` parameter allows manual entry of a specific MAC address, while the `sticky` keyword enables the switch to dynamically learn MAC addresses and add them to the running configuration as secure MAC addresses. This is particularly useful for environments where devices are frequently swapped, as the switch remembers the MAC addresses without manual reconfiguration.
However, sticky MAC addresses are stored in the running configuration and are lost on reload unless saved to startup configuration. A common mistake is forgetting to set the maximum number of secure MAC addresses using `switchport port-security maximum <value>`; the default is 1, which may be too restrictive for ports with IP phones (which require two MAC addresses: phone and PC). Another pitfall is enabling port security on trunk ports, which is generally not recommended because trunks carry multiple VLANs and many MAC addresses.
The command is available in IOS 12.x and later, including IOS-XE, but not in NX-OS (which uses `port-security` under interface configuration with different syntax). In terms of privilege level, the command requires privileged EXEC mode (enable) and is applied in global configuration mode. The impact on running configuration is immediate; the configured MAC addresses appear under the interface as `switchport port-security mac-address <mac>` or `switchport port-security mac-address sticky`.
switchport port-security mac-address [mac|sticky]When to Use This Command
- Assign a known trusted device's MAC address to a port to prevent unauthorized access.
- Use sticky learning to dynamically learn and save MAC addresses of connected devices.
- Configure multiple secure MAC addresses on a trunk port for VoIP and data VLANs.
- Set a specific MAC address on a port connected to a server for enhanced security.
Parameters
| Parameter | Syntax | Description |
|---|---|---|
| mac-address | H.H.H | Specifies a static secure MAC address in dotted hexadecimal format (e.g., aaaa.bbbb.cccc). This address is added to the secure MAC address list for the interface. Only one MAC address can be specified per command; to add multiple addresses, repeat the command. A common mistake is using colons or hyphens instead of dots; Cisco uses the dotted format. |
| sticky | sticky | Enables sticky MAC address learning on the interface. When enabled, dynamically learned MAC addresses are converted to secure MAC addresses and added to the running configuration. This allows the switch to remember MAC addresses across reboots if the configuration is saved. Note that sticky is a keyword, not a parameter value; it cannot be combined with a specific MAC address in the same command. |
Command Examples
Configure a static secure MAC address
Switch(config-if)# switchport port-security mac-address 0050.7966.6800This command assigns the MAC address 0050.7966.6800 as a secure address on the interface. No output is shown if successful; use 'show port-security interface' to verify.
Enable sticky MAC address learning
Switch(config-if)# switchport port-security mac-address stickyEnables sticky learning: dynamically learned MAC addresses are saved to the running config. No immediate output; verify with 'show port-security interface' or 'show running-config'.
Understanding the Output
The command itself does not produce output. To verify, use 'show port-security interface <interface>'. The output includes fields like 'Secure Address' (list of allowed MACs), 'Maximum Addresses' (max count), 'Current Addresses' (count in use), 'Security Violation Count' (number of violations), and 'Security Action' (shutdown/restrict/protect).
A good state shows zero violations and correct MAC count. A bad state shows violations >0 or port in errdisable. Watch for 'Sticky Address' entries if sticky learning is enabled.
Configuration Scenarios
Configure Static Secure MAC Address on an Access Port
A network administrator wants to allow only a specific workstation (MAC address aaaa.bbbb.cccc) to connect to a switchport. This is common in high-security areas where device identity must be strictly controlled.
Topology
PC1(MAC:aaaa.bbbb.cccc)---(Gi0/1)SW1Steps
- 1.Step 1: Enter global configuration mode: SW1# configure terminal
- 2.Step 2: Enter interface configuration mode for the port: SW1(config)# interface GigabitEthernet0/1
- 3.Step 3: Ensure the port is in access mode: SW1(config-if)# switchport mode access
- 4.Step 4: Enable port security: SW1(config-if)# switchport port-security
- 5.Step 5: Set the maximum number of secure MAC addresses (default is 1, but explicitly set it): SW1(config-if)# switchport port-security maximum 1
- 6.Step 6: Configure the static secure MAC address: SW1(config-if)# switchport port-security mac-address aaaa.bbbb.cccc
- 7.Step 7: (Optional) Set violation mode to shutdown (default): SW1(config-if)# switchport port-security violation shutdown
- 8.Step 8: Exit and save configuration: SW1(config-if)# end; SW1# copy running-config startup-config
! Full IOS config block interface GigabitEthernet0/1 switchport mode access switchport port-security switchport port-security maximum 1 switchport port-security mac-address aaaa.bbbb.cccc switchport port-security violation shutdown
Verify: Use `show port-security interface GigabitEthernet0/1` to verify. Expected output includes: Secure Address Count: 1, Last Source Address: aaaa.bbbb.cccc, Security Violation Count: 0. Also use `show port-security address` to list all secure MAC addresses.
Watch out: If the port is not configured as an access port (i.e., it is in dynamic desirable or trunk mode), port security will not work. Always ensure `switchport mode access` is configured first.
Enable Sticky MAC Learning on a Port with IP Phone and PC
A switchport connects to an IP phone (MAC bbbb.bbbb.bbbb) and a PC (MAC cccc.cccc.cccc) behind the phone. The administrator wants the switch to automatically learn and remember these MAC addresses without manual entry, but still enforce a limit of 2 MAC addresses.
Topology
PC2(MAC:cccc.cccc.cccc)---IP Phone(MAC:bbbb.bbbb.bbbb)---(Gi0/2)SW1Steps
- 1.Step 1: Enter global configuration mode: SW1# configure terminal
- 2.Step 2: Enter interface configuration: SW1(config)# interface GigabitEthernet0/2
- 3.Step 3: Set the port to access mode: SW1(config-if)# switchport mode access
- 4.Step 4: Enable port security: SW1(config-if)# switchport port-security
- 5.Step 5: Set maximum secure MAC addresses to 2: SW1(config-if)# switchport port-security maximum 2
- 6.Step 6: Enable sticky learning: SW1(config-if)# switchport port-security mac-address sticky
- 7.Step 7: (Optional) Set violation mode to restrict (to log but not shut down): SW1(config-if)# switchport port-security violation restrict
- 8.Step 8: Exit and save: SW1(config-if)# end; SW1# copy running-config startup-config
! Full IOS config block interface GigabitEthernet0/2 switchport mode access switchport port-security switchport port-security maximum 2 switchport port-security mac-address sticky switchport port-security violation restrict
Verify: After connecting the phone and PC, use `show port-security interface GigabitEthernet0/2` to see Secure Address Count: 2. Use `show running-config interface GigabitEthernet0/2` to see the sticky MAC addresses listed as `switchport port-security mac-address sticky bbbb.bbbb.bbbb` and `switchport port-security mac-address sticky cccc.cccc.cccc`.
Watch out: Sticky MAC addresses are stored in running-config only. If the switch reloads without saving, the sticky addresses are lost, and the port will re-learn them. Always save the configuration after sticky addresses are learned. Also, if the maximum is set to 2 but a third device appears, a violation occurs; with restrict mode, traffic from the third device is dropped but the port stays up.
Troubleshooting with This Command
When troubleshooting port security issues, the primary command is `show port-security interface <interface>`. Healthy output shows a Secure Address Count that matches the expected number of devices, Last Source Address as the most recent MAC address seen, and Security Violation Count of 0. If the violation count is non-zero, it indicates that an unauthorized MAC address attempted to communicate.
The violation mode (protect, restrict, shutdown) determines the action: protect silently drops, restrict drops and logs, shutdown error-disables the port. To check if a port is error-disabled due to a security violation, use `show interfaces status` and look for 'err-disabled' status. The command `show port-security address` lists all secure MAC addresses on the switch, including the interface, MAC address, and type (static, dynamic, sticky).
If a port is in err-disabled state, you can re-enable it with `shutdown` followed by `no shutdown` on the interface, but first identify the cause. Common symptoms include: users unable to connect (port in err-disabled), intermittent connectivity (violation count increasing but port not shut down due to restrict mode), or unexpected MAC addresses appearing (sticky learning not enabled). A step-by-step diagnostic flow: 1) Check the port status with `show interfaces <interface>`; if up/up, proceed. 2) Run `show port-security interface <interface>` to see secure address count and violation count. 3) If violation count > 0, examine the Last Source Address to identify the offending MAC. 4) Determine if that MAC should be allowed; if yes, add it statically or increase the maximum. 5) If the port is err-disabled, note the reason with `show interfaces status | include err-disabled`. 6) After fixing the cause, re-enable the port.
Correlate with `show mac address-table interface <interface>` to see all MAC addresses learned on the port (including those that may have caused violations). Also, `debug port-security` can be used in lab environments to see real-time events, but caution is advised in production due to CPU impact. Another useful command is `show port-security` (without interface) to get a summary of all ports with port security enabled, including their status and violation counts.
If sticky MAC addresses are not being learned, verify that `switchport port-security mac-address sticky` is configured and that the maximum number has not been reached. Remember that sticky learning only converts dynamically learned MAC addresses; if the maximum is already reached, new MACs will trigger violations instead of being learned.
CCNA Exam Tips
Remember that 'switchport port-security mac-address sticky' dynamically learns and saves MACs; they appear in running-config as 'switchport port-security mac-address sticky xxxx.xxxx.xxxx'.
The maximum number of secure MACs defaults to 1; use 'switchport port-security maximum <number>' to increase.
If a violation occurs and the action is shutdown, the port enters errdisable state; recover with 'shutdown' then 'no shutdown' or configure errdisable recovery.
Sticky MAC addresses are not saved to startup-config unless you copy running-config to startup-config.
Common Mistakes
Forgetting to enable port security globally with 'switchport port-security' before configuring MAC addresses.
Setting a static MAC address that does not match the connected device, causing a violation and port shutdown.
Not increasing the maximum MAC count when using sticky learning on a trunk port, leading to violations.
Assuming sticky MAC addresses persist across reloads without saving the configuration.
switchport port-security mac-address [mac|sticky] vs show port-security interface [intf]
These two commands are often considered together because switchport port-security mac-address configures which MAC addresses are allowed on a port, while show port-security interface displays the resulting security state and any violations, making them complementary for setup and verification.
| Aspect | switchport port-security mac-address [mac|sticky] | show port-security interface [intf] |
|---|---|---|
| Scope | Configures secure MAC addresses on a specific interface | Displays port security configuration and status for an interface |
| Configuration mode | Interface configuration mode | Privileged EXEC (or any user EXEC with limited info) |
| Persistence | Stored in running-config (and can be copied to startup-config) | No persistent effect; shows current operational state |
| Precedence | Defines allowed MACs; overrides dynamically learned addresses | Displays which MACs are currently secure and violation counts |
| Typical use | Assigning static or sticky secure MAC addresses to a port | Verifying secure MAC addresses and troubleshooting violations |
Use switchport port-security mac-address [mac|sticky] when you need to statically define or enable sticky learning of allowed MAC addresses on a port.
Use show port-security interface [intf] when you need to inspect the current secure MAC addresses, violation count, and security action on a specific interface.
Platform Notes
In IOS-XE (e.g., Catalyst 3650/3850/9300), the command syntax is identical to classic IOS. However, the output of `show port-security` may include additional fields like 'Security Violation Count' and 'Last Source Address' in a slightly different format. For example, IOS-XE may display 'Secure MAC Addresses' in a table with columns for Type (StaticDynamicSticky).
The command is fully supported in IOS-XE. In NX-OS (e.g., Nexus 9000/7000), port security is configured differently. The equivalent command is `port-security` under interface configuration, but the syntax for specifying MAC addresses is `port-security mac-address <mac>` (no 'switchport' prefix).
Additionally, NX-OS uses `port-security maximum <value>` and `port-security violation {protect | restrict | shutdown}`. Sticky learning is not a separate keyword; instead, NX-OS automatically saves dynamically learned secure MAC addresses to the running configuration if you use the `port-security mac-address sticky` command? Actually, NX-OS does not have a 'sticky' keyword; it uses `port-security mac-address` to statically assign, and dynamic learning is enabled by default when port security is enabled.
To make dynamic addresses persistent, you must configure `port-security mac-address sticky`? Wait, NX-OS documentation indicates that the `sticky` keyword is not supported; instead, you can use `port-security mac-address` with the `sticky` option? I need to be accurate: In NX-OS, the command is `switchport port-security mac-address sticky` is not valid; the correct command is `port-security mac-address sticky`?
Actually, NX-OS uses `port-security` under interface, and to set a static MAC, you use `port-security mac-address <mac>`. There is no sticky learning; dynamic MAC addresses are learned and stored in the MAC address table but not in the running config. To make them persistent, you must manually add them.
So, for NX-OS, the equivalent of sticky learning is not directly available; you would need to use a script or manually configure. On ASA devices, port security is not a feature; instead, you would use MAC address filtering or 802.1X. In IOS-XR, port security is not supported; it is a Layer 2 feature primarily on Catalyst switches.
Regarding IOS versions, the command has been available since IOS 12.1(11)EA1. In later versions (15.x and 16.x), the behavior is consistent, but the output format may vary slightly. For example, in IOS 15.x, `show port-security` includes a 'Security Violation Count' field, while in 12.x, it may be labeled 'Violation Count'.
Always refer to the specific IOS documentation for exact output. In summary, the command is robust across IOS platforms but differs significantly in NX-OS and is absent in IOS-XR and ASA.
Related Commands
show port-security address
Displays the secure MAC addresses configured on all switch ports or a specific interface, used to verify port security address learning and aging.
show port-security interface [intf]
Displays port security configuration and status for a specific interface, including secure MAC addresses, violation counts, and action taken.
Practice for the CCNA 200-301
Test your knowledge with practice questions covering all CCNA 200-301 exam domains.
Practice CCNA 200-301 Questions