快速开始

这一章会指导你如何快速使用你的新VyOs系统。这里展示了一个简单的配置样例供你学习,样例提供了一个带有两个网络接口(eth0 和 eth1)的设备作为一个 NAT 网关的配置方法。

配置模式

Vyos默认处于操作模式,此时命令行会显示一个 $ 符号。配置Vyos需要进入配置模式,进入该模式后命令行会显示一个 # 符号,如下所示:

vyos@vyos$ configure
vyos@vyos#

提交和保存

每次配置更改之后,需要使用下面的命令来应用更改:

commit

当你的配置能够达到预期的效果时,可以使用下面的命令来永久保存:

save

接口配置

  • 系统外部/WAN口为eth0。该接口的地址通过DHCP获得。

  • 系统内部/Lan口为eth1。该接口使用静态地址 192.168.0.1/24

切换到 配置模式 后,输入下列命令:

set interfaces ethernet eth0 address dhcp
set interfaces ethernet eth0 description 'OUTSIDE'
set interfaces ethernet eth1 address '192.168.0.1/24'
set interfaces ethernet eth1 description 'INSIDE'

SSH 管理

切换到 配置模式 之后,输入下列命令,此时系统将会监听每一个接口上进来的SSH连接。你也可查看 SSH 章节学习如何只监听指定地址。

set service ssh port '22'

DHCP/DNS 快速开始

下列设置会在你的本地/LAN网络上配置DHCP和域名服务, 在该网络中VyOS会作为默认的网关和DNS服务器。

  • 默认的网关和DNS地址为 192.168.0.1/24

  • 地址段 192.168.0.2/24 - 192.168.0.8/24 会被保留作静态分配用

  • DHCP客户端会被分配一个地址段 192.168.0.9 - 192.168.0.254 中的地址,以及一个名为 internal-network 的域名

  • DHCP租期为1天 (86400秒)

  • VyOS 会作为一个完全的DNS服务器,无需再利用Google, Clouldflare 或其他公共DNS服务器(这样做对隐私比较好)

  • 只有本地/LAN网络中的主机可以使用DNS

set service dhcp-server shared-network-name LAN subnet 192.168.0.0/24 default-router '192.168.0.1'
set service dhcp-server shared-network-name LAN subnet 192.168.0.0/24 dns-server '192.168.0.1'
set service dhcp-server shared-network-name LAN subnet 192.168.0.0/24 domain-name 'vyos.net'
set service dhcp-server shared-network-name LAN subnet 192.168.0.0/24 lease '86400'
set service dhcp-server shared-network-name LAN subnet 192.168.0.0/24 range 0 start 192.168.0.9
set service dhcp-server shared-network-name LAN subnet 192.168.0.0/24 range 0 stop '192.168.0.254'

set service dns forwarding cache-size '0'
set service dns forwarding listen-address '192.168.0.1'
set service dns forwarding allow-from '192.168.0.0/24'

NAT

下列设置会为我们的本地/LAN网络配置 SNAT 规则, 允许主机通过IP伪装与外部/WAN网络进行通信。

set nat source rule 100 outbound-interface 'eth0'
set nat source rule 100 source address '192.168.0.0/24'
set nat source rule 100 translation address masquerade

防火墙

为外部/WAN口添加一组防火墙策略。

下面的配置将创建一个合适的有状态的防火墙,可以阻止所有最初不是从本地/LAN侧发出的流量

set firewall name OUTSIDE-IN default-action 'drop'
set firewall name OUTSIDE-IN rule 10 action 'accept'
set firewall name OUTSIDE-IN rule 10 state established 'enable'
set firewall name OUTSIDE-IN rule 10 state related 'enable'

set firewall name OUTSIDE-LOCAL default-action 'drop'
set firewall name OUTSIDE-LOCAL rule 10 action 'accept'
set firewall name OUTSIDE-LOCAL rule 10 state established 'enable'
set firewall name OUTSIDE-LOCAL rule 10 state related 'enable'
set firewall name OUTSIDE-LOCAL rule 20 action 'accept'
set firewall name OUTSIDE-LOCAL rule 20 icmp type-name 'echo-request'
set firewall name OUTSIDE-LOCAL rule 20 protocol 'icmp'
set firewall name OUTSIDE-LOCAL rule 20 state new 'enable'

如果想启用从外部/WAN口通过SSH访问防火墙的话,可以创建一些额外的规则来允许这一类的流量

下列规则可以允许SSH流量并将请求频率限制在4次/分钟,这样可以阻止暴力尝试:

set firewall name OUTSIDE-LOCAL rule 30 action 'drop'
set firewall name OUTSIDE-LOCAL rule 30 destination port '22'
set firewall name OUTSIDE-LOCAL rule 30 protocol 'tcp'
set firewall name OUTSIDE-LOCAL rule 30 recent count '4'
set firewall name OUTSIDE-LOCAL rule 30 recent time '60'
set firewall name OUTSIDE-LOCAL rule 30 state new 'enable'

set firewall name OUTSIDE-LOCAL rule 31 action 'accept'
set firewall name OUTSIDE-LOCAL rule 31 destination port '22'
set firewall name OUTSIDE-LOCAL rule 31 protocol 'tcp'
set firewall name OUTSIDE-LOCAL rule 31 state new 'enable'

应用防火墙规则

set interfaces ethernet eth0 firewall in name 'OUTSIDE-IN'
set interfaces ethernet eth0 firewall local name 'OUTSIDE-LOCAL'

提交更改,保存配置然后推出配置模式

vyos@vyos# commit
vyos@vyos# save
Saving configuration to '/config/config.boot'...
Done
vyos@vyos# exit
vyos@vyos$

加固

特别是当你允许从外部/WAN口远程SSH连接时, 下面几个额外的配置步骤是很有必要的。

替换默认的 vyos 系统用户:

set system login user myvyosuser authentication plaintext-password mysecurepassword

设置 Key Based Authentication

set system login user myvyosuser authentication public-keys myusername@mydesktop type ssh-rsa
set system login user myvyosuser authentication public-keys myusername@mydesktop key contents_of_id_rsa.pub

最后,尝试以新用户SSH连接到VyOS。确认新的用户可以不使用密码访问路由器之后删除原来的 vyos 用户,然后完全禁止使用密码验证的方式进行 SSH

delete system login user vyos
set service ssh disable-password-authentication

上述操作之后,提交更改、保存配置然后退出配置模式:

vyos@vyos# commit
vyos@vyos# save
Saving configuration to '/config/config.boot'...
Done
vyos@vyos# exit
vyos@vyos$

现在你应该有一个简单但安全,且正常工作的路由器来进行后续的玩耍了。尽情享受吧!