# 交换机常用命令

## 各种模式之间的切换

| Command Mode            | Access Method                                                                                                                       | Prompt                 | Exit or Access Next Mode                                                                                                                                                          |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| User EXEC               | This is the first level of access.(For the switch) Change terminal settings, perform basic tasks, and list system information.      | `Switch>`              | Enter the **logout** command.To enter privileged EXEC mode, enter the **enable** command.                                                                                         |
| Privileged EXEC         | From user EXEC mode, enter the **enable** command.                                                                                  | `Switch#`              | To exit to user EXEC mode, enter the **disable** command.To enter global configuration mode, enter the **configure** command.                                                     |
| Global configuration    | From privileged EXEC mode, enter the **configure** command.                                                                         | `Switch(config)#`      | To exit to privileged EXEC mode, enter the **exit** or **end** command, or press **Ctrl-Z**.To enter interface configuration mode, enter the **interface** configuration command. |
| Interface configuration | From global configuration mode, specify an interface by entering the **interface** command followed by an interface identification. | `Switch(config-if)#`   | To exit to privileged EXEC mode, enter the **end** command, or press **Ctrl-Z**.To exit to global configuration mode, enter the **exit** command.                                 |
| Config-vlan             | In global configuration mode, enter the **vlan** *vlan-id* command.                                                                 | `Switch(config-vlan)#` | To exit to global configuration mode, enter the **exit** command.To return to privileged EXEC mode, enter the **end** command, or press **Ctrl-Z**.                               |
| VLAN configuration      | From privileged EXEC mode, enter the **vlan database** command.                                                                     | `Switch(vlan)#`        | To exit to privileged EXEC mode, enter the **exit** command.                                                                                                                      |
| Line configuration      | From global configuration mode, specify a line by entering the **line** command.                                                    | `Switch(config-line)#` | To exit to global configuration mode, enter the **exit** command.To return to privileged EXEC mode, enter the **end** command, or press **Ctrl-Z**.                               |

## 用户模式与特权模式

用户模式：可以使用一些基本的查询命令

特权模式：可以对交换机进行相关的配置

### 切换特权模式

```bash
进入特权模式命令
Switch>enable

退出特权模式命令
Switch#disable
```

### 查看帮助信息

```bash
 ?
```

### 设置时间

```bash
Switch#clock set 时间(自选参数。参数必须符合交换机要求)
```

### 显示信息命令

```bash
Switch#show 可选参数

查看交换机配置
Switch#show running-config

查看端口信息
Switch#show interface

查看MAC地址表：
Switch#show mac address-table

查看交换机CPU的状态信息： 
Switch#show processes
```

> 注意：可以用TAB键补齐命令，自选参数为用户自定义参数，可选参数为交换机设定参数

### 保存交换机配置

```bash
Switch#copy running-config startup-config
Switch#wr
```

## 全局配置模式

### 切换全局配置模式

```bash
进入全局配置模式
Switch#configure terminal

退出配置模式
Switch(config)#exit
```

### 设置主机名

```bash
主机名修改
Switch(config)#hostname 主机名(自选参数)

取消主机名设置
Switch(config)#no hostname
```

### 设置特权模式密码

```bash
设置特权模式进入密码
Switch(config)#enable secret 密码(自选参数)

取消特权模式密码：
Switch(config)#no enable secret
```

### 显示信息命令

```bash
示例： do show *
```

> 注意：配置模式中无法使用show命令，如果要使用的话，请使用do show

### 指定根交换机命令

```bash
Switch(config)#spanning-tree vlan 自选参数(VLAN号)

示例： 
Switch (config)#spanning-tree vlan 1 root primary
```

> 注意：设置根交换机是基于VLAN的

### 关闭生成树协议命令

```bash
Switch (config)#no spanning-tree vlan自选参数(VLAN)

示例： 
Switch (config)#no spanning-tree vlan 1
```

## 接口配置模式

### 切换接口配置模式

```bash
进入接口配置模式
Switch(config)#interface 端口名称(可选参数)

退出接口配置模式
Switch(config-if)#exit
```

### 启用和停用端口

```bash
启用端口
Switch(config-if)#no shutdown

停用端口
Switch(config-if)#shutdown
```

### 进入多端口配置

```bash
进入同类型多端口配置
Switch(config)#interface range fastethernet0/1-5

进入不同类型多端口配置
Switch(config)#interface range fastethernet0/1-5，gigabitethernet 0/
```

## 二层端口的配置

### 端口速率

```bash
Switch(config-if)#speed 可选参数
```

### 双工模式

```bash
Switch (config-if)#duplex可选参数
```

### 启用和禁用链路协商

```bash
启用链路协商
Switch (config-if)#negotiation auto

禁用链路协商
Switch (config-if)#no negotiation auto
```

## VLAN 的配置

### 新建VLAN

```bash
Switch (config)#vlan 自选参数(VLAN号)
Switch(config-vlan)#name 自选参数(VLAN名)

示例： 
Switch (config)#vlan 1
Switch (config-vlan)#name lab1
```

### 删除VLAN

```bash
Switch#vlan database
Switch(v1an)#no vlan 自选参数(vlan号)

示例： 
Switch#vlan database
Switch(v1an)#no vlan 2
```

### 将一个接口加入VLAN

```bash
Switch(config)#interface 可选参数 (接口号)
Switch (config-if)#switchport mode access
Switch(config-if)#switchport access vlan 自选参数(VLAN号)
Switch(config-if)#no shutdown
Switch(config-if)#exit 

示例：
Switch(config)#interface f0/1
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 1
Switch(config-if) #no shutdown
Switch(config-if)#exit
```

### 将一个接口退出VLAN

```bash
Switch(config)#interface 可选参数(接口号)
Switch(config-if)#no switchport access vlan 自选参数(VLAN号)

示例
Switch(config)#interface f0/1
Switch(config-if)#no switchport access vlan 2
```

### 配置vlan的ip地址

```bash
Switch(config)#interface vlan {Vlan号}
Switch(config-if)#ip address {ip地址} {子网掩码}

示例
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 up

Switch(config-if)#ip address 192.168.1.1 255.255.255.0
```

### 修改一个接口为端口汇聚模式

```bash
Switch (config)#interface 可选参数(接口号)
Switch(config-if)#switchport mode trunk
```

### VLAN的验证命令

```bash
Switch#show vlan                    # 显示VLAN列表
Switch#show interface switchport     # 查看具体接口的VLAN成员资格信息
Switch#show run                        # 查看vlan的ip
```

## VTP的配置

### VTP Domain Name (VTP域名)

```bash
Switch(config)#configure terminal

Switch(config)#vtp domain 自选参数

示例： 
Switch (config)#vtp domain Lab_Network
```

### VTP mode (VTP模式)

```bash
Switch(config)#vtp mode 可选参数(Server/C1ient/ Transparent)

示例： 
Switch (config)#rtp mode Server
```

### VTP Pruning (VIP修剪)

```bash
Switch(config)#vtp pruning
```

> 注意：有的交换机不支持VTP修剪命令

### 查看VTP的配置命令

```bash
显示交换机运行的VTP模式，配置修订号和交换机所属的VTP域
Switch#show vtp status

显示VTP消息相关的统计数据
Switch#show vtp counters
```

## 远程登录交换机设置

1. 配置管理VLAN

   ```bash
   Switch(conf)#interface vlan 1
   Switch (config-if)#ip address x.x.x.x 255.255.255.0
   Switch (config-if)#no shutdown
   ```

   > 注意： IP 地址必须与远程连接交换机的PC机地址在同一网段
2. 在交换机上指定默认网关

   ```bash
   Switch (config)#ip default-gateway x.x.x.x
   ```

   > 注意：指定的默认网关仅当前交换机有效。
3. 将交换机连接PC机的端口加入VLAN 1

   ```bash
   Switch(config)#interface 端口号(可选参数)
   Switch(config-if)#switchport mode access
   Switch(config-if)#switchport access vlan 1
   Switch(config-if)#no shutdown
   ```
4. 配置交换机的远程登录

   ```bash
   Switch(config)#line vty 0 4
   Switch (config-line)#password 自选参数
   Switch (config-line)#login
   Switch (config-line)#enable secret 自选参数
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://moluo.gitbook.io/notes/bian-cheng-xue-xi/wang-luo/cisco/jiao-huan-ji-chang-yong-ming-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
