OpenWrt简单命令配置

OpenWrt简单命令配置文档

恢复系统(用电脑主机做OpenWrt路由的情况)

进入救援模式,然后
dd if=openwrt-15.05.1-x86-64-combined=ext4.img of=/dev/sda bs=4k
然后重启就可以
如果是普通路由器,直接刷固件就可以。

配置内容

接口添加

vconfig add eth0 123#添加一个网卡接口
vconfig set_flag eth0.123 1 1
ifconfig eth0.123 up#开启该接口
dhclient eth0.123#使该接口DHCP自动获得IP

初始配置

a.路由器启动后,有的型号没有安装 Wifi 模块,需要先用网线连接到 LAN 口,本机 IP 配置为静态 192.168.1.x,然后 telnet 到 192.168.1.1,更改 root 密码,然后 ssh 连入

b.配置 WAN 口,让路由连上 Internet,

配置 PPPoE:

代码如下:

uci set network.wan.proto=pppoe
uci set network.wan.username='上网拨号账号'
uci set network.wan.password='密码'
uci commit network #保存更改
ifup wan

配置完毕后通过如下命令,重启网络服务,使设置生效:
/etc/init.d/network restart
打开你的浏览器,看下网络是否通了。
或者用ping 命令看下网络是否畅通,如下所示则拨号成功。
直接修改network配置文件连接上网:
通过文本编辑器vi打开配置文件:
vi vi /etc/config/network
同时按下G+Shift键,移动至文件末尾 按下o键,输入如下内容:

config interface 'wan'                  option ifname 'eth0.2'            
option proto 'pppoe'              
option username '上网拨号账户'     
option password '上网拨号密码' 

按下ESC键, 输入
:wq
退出编辑后,重启网络服务
/etc/init.d/network restart

配置WIFI无线网络

和有限网络的配置一样,wifi的配置文件也位于/etc/config文件夹中,配置文件为/etc/config/wireless, 也可以通过直接编辑该文件或者通过uci命令进行设置。 但是最简单的方式是使用’wifi detect’
命令自动检测生成配置文件:
登录路由器后输入如下命令:

root@OpenWrt:~# rm -f /etc/config/wireless; wifi detect > /etc/config/wireless
config wifi-device  radio0
    option type     mac80211
    option channel  11
    option hwmode   11g
    option path 'platform/ar934x_wmac'
    option htmode   HT20
    # REMOVE THIS LINE TO ENABLE WIFI:
    option disabled 1

config wifi-iface
    option device   radio0
    option network  lan
    option mode     ap
    option ssid     OpenWrt
    option encryption none

config wifi-device  radio1
    option type     mac80211
    option channel  36
    option hwmode   11a
    option path 'pci0000:00/0000:00:00.0'
    option htmode   HT20
    # REMOVE THIS LINE TO ENABLE WIFI:
    option disabled 1

config wifi-iface
    option device   radio1
    option network  lan
    option mode     ap
    option ssid     OpenWrt
    option encryption none

启用/重启/关闭 无线接口
在 Openwrt下要 启用/重启/关闭 无线接口是使用命令 “wifi"来完成的。
启动无线接口:不带参数直接在命令行运行 wifi 命令;
如果修改了无线的配置,我们需要重新启动无线接口使配置生效,同样是不带参数直接在命令行下运行 wifi 命令;
如果需要关闭无线装置,加上” down "参数使用 “wifi down”
大多数情况下新安装的 Openwrt 是默认关闭无线接口的。

wifi   #开启wifi
wifi down #关闭wifi

打开笔记本或者手机,搜索一下无线网络,看看是不是出现了新的名称为‘OpenWrt’的网络信号。如果出现了,连上试试.

注意:此时的无线信号是没有加密的,了解无线配置的基本说明后,我们继续配置网络的密码。
一个典型的无线设置文件至少应该包含两个方面的内容,即至少一个无线设备 和 至少一个与之相关的无线接口 配置。
无线设备 的设置指的是一般无线电通讯相关的参数,例如设备硬件(网卡芯片驱动程序类型),信道,频率,发射功率等;
无线接口 的设置指的是 无线设备 的工作模式,essid,无线加密方式等。
无线设备 和 无线接口 具有关联性,首先是设置好一个 无线设备 的参数,然后再设置与这个 无线设备 相关的 无线接口 参数,从而构造出一个可以有效工作的无线局域网环境。
wifi-device 配置项
wifi-device 所配置的是指设备中无线通讯硬件,很多情况中,一个设备只有一个无线通讯接口,所以只有一个 wifi-device 配置项,如果有多个无线通讯设备则会有多个 wifi-device 配置项,每个配置项用来指定不同的接口。
一个最小的 wifi-device 配置就像下面的例子,需要注意的是其中所指定的不同的芯片类型和驱动。

config 'wifi-device' 'wl0'
        option 'type'    'broadcom'
        option 'channel' '6'
        
wl0        是无线网卡的内置标识符
broadcom   表示芯片或驱动程序的类型
6          指定无线网卡工作的无线频道

简单介绍配置的常见选项,对比看下wifi detect的输出:

名称 类型 说明
type string 设备启动时自动检测的无线类型,broadcom平台类型为brcm-2.4,atheros平台为madwifi或mac80211
channel 数字或’auto’ 使用的无线频道

wifi-iface 配置项 wifi-iface 的设置指的是 无线设备 的工作模式,essid,无线加密方式等。 最简单的配置如下:

config 'wifi-iface'
        option 'device'     'wl0'
        option 'network'    'lan'
        option 'mode'       'ap'
        option 'ssid'       'MyWifiAP'
        option 'encryption' 'psk2'
        option 'key'        'secret passphrase'

这里我们就可以去修改无线网络的密码了
vi /etc/config/wireless
修改 wifi-iface的加密

config wifi-iface
    option device   radio0
    option network  lan
    option mode     ap
    option ssid     OpenWrt
    option encryption 'psk2'
    option key '密码'

重启wifi

wifi down
wifi up

c.安装 LuCI Web 管理界面并设置开机自动启动
代码如下:

opkg update
opkg install luci
/etc/init.d/uhttpd start
/etc/init.d/uhttpd enable

d.浏览器输入路由器 LAN 侧 IP(多为192.168.1.1),进行 Wifi 等配置

配置 DNS

a.创建 /etc/config/sec_resolv.conf
vim /etc/config/sec_resolv.conf
填入以下 DNS Servers:
代码如下:

nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 208.67.222.222

b.编辑 /etc/config/dhcp
vim /etc/config/dhcp
找到 opTIon resolvfile 选项,替换为:
option resolvfile ‘/etc/config/sec_resolv.conf’

配置 PPTP

a.安装 ppp-mod-pptp
代码如下:

opkg update
opkg install ppp-mod-pptp

如果需要 LuCI 支持(推荐):

opkg install luci-proto-ppp

b.配置 vpn 接口,编辑 /etc/config/network 文件,应该已经有以下内容(如果没有,需要插入),并配置里面的 server、username 和 password:
代码如下

config 'interface' 'vpn'
option 'ifname' 'pptp-vpn'
option 'proto' 'pptp'
option 'username' 'vpnusername'
option 'password' 'vpnpassword'
option 'server' 'vpn.example.org or ipaddress'
option 'buffering' '1'

c.进入 Network -》 Firewall ,把 vpn 加入 wan zone
d.进入 Network -》 Interfaces ,此时应该已经可以看到 VPN Interface 并可以连接
e.此时在本机 traceroute www.google.com
应该就可以访问了

配置 chnroutes

a.到 chnroutes 项目的下载页面:

http://chnroutes-dl.appspot.com/

下载 linux.zip,解压
b.把 ip-pre-up 重命名为 chnroutes.sh,打开编辑,在

if [ ! -e /tmp/vpn_oldgw ]; then

前插入以下代码,以避免 ppp 连接脚本重复执行导致重复添加路由表项:
代码如下:

if [ $OLDGW == 'x.x.x.x' ]; then
exit 0
fi

其中 x.x.x.x是VPN的网关,可以先本机连接上去之后查看一下网关地址
c.ssh 连接到路由器,执行以下命令:
代码如下:

cd /etc/config/
mkdir pptp-vpncd pptp-vpnvim chnroutes.sh

在 vim 中把编辑好的 chnroutes.sh 粘贴进去(当然也可以通过 ssh 直接把 chnroutes.sh 文件传过去,或者上传到某个地方再 wget 下载)
执行以下命令,设置权限为可执行:

chmod a+x chnroutes.sh

d.用 vim 编辑 /lib/netifd/ppp-up 文件:

vim /lib/netifd/ppp-up

[ -d /etc/ppp/ip-up.d ] && { 这一行前插入以下内容,确保 ppp 连接脚本能够被执行:
sh /etc/config/pptp-vpn/chnroutes.sh

e.重启路由,启动好之后,进入 LuCI 查看接口状态,等 WAN 和 VPN 都连接成功后,ssh进去,执行

route -n | head -n 10 

效果应该类似这样:
代码如下:

root@FC_R0:/etc/config# route -n | head -n 10
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.7.0.1 0.0.0.0 UG 0 0 0 pptp-vpn
1.0.1.0 58.111.43.1 255.255.255.0 UG 0 0 0 pppoe-wan
1.0.2.0 58.111.43.1 255.255.254.0 UG 0 0 0 pppoe-wan
1.0.8.0 58.111.43.1 255.255.248.0 UG 0 0 0 pppoe-wan
1.0.32.0 58.111.43.1 255.255.224.0 UG 0 0 0 pppoe-wan
1.1.0.0 58.111.43.1 255.255.255.0 UG 0 0 0 pppoe-wan
1.1.2.0 58.111.43.1 255.255.254.0 UG 0 0 0 pppoe-wan
1.1.4.0 58.111.43.1 255.255.252.0 UG 0 0 0 pppoe-wan

其中 Destination 为 0.0.0.0 的是默认路由,网关为 VPN 网关,意味着默认流量都经过 VPN,而以下的条目则把目的为国内的网段都指向了 ISP 提供的网关。
至此 PPTP VPN 和 chnroutes 已经配置完毕。
6.配置 VPN 断线自动重连
a.创建 /etc/config/pptp-vpn/status-check.sh:
vim /etc/config/pptp-vpn/status-check.sh
在 vim 中粘贴以下内容(此脚本检测 VPN 连接状态,并在断线后会断开 WAN 和 VPN 接口,10秒后重新连接 WAN,并在 30 秒后重连 VPN):
代码如下:

#!/bin/sh
if [ -f "/tmp/vpn_status_check.lock" ]
then
        exit 0
fi
VPN_CONN=`ifconfig | grep pptp-vpn`
if [ -z "$VPN_CONN" ]
then
        touch /tmp/vpn_status_check.lock
        echo WAN_VPN_RECONNECT at: >> /tmp/vpn_status_check_reconn.log
        date >> /tmp/vpn_status_check_reconn.log
        ifdown vpn
        ifdown wan
        sleep 10
        ifup wan
        sleep 30
        ifdown vpn
        sleep 10
        ifup vpn
        sleep 40
        rm /tmp/vpn_status_check.lock
else
        date > /tmp/vpn_status_check.log
fi

执行以下命令,设置权限为可执行:

chmod a+x /etc/config/pptp-vpn/status-check.sh

b.进入LuCI 的 System -》 Scheduled Tasks 填入以下内容,并保存:

*/1 * * * * /etc/config/pptp-vpn/status-check.sh

以上实际上是编辑了 cron 配置,cron 每分钟运行检测 / 重连脚本,重启 cron:

/etc/init.d/cron restart

c.静待几分钟,查看 /tmp 目录,应该能看到 vpn_oldgw 和 vpn_status_check.log 文件,查看 vpn_status_check.log 文件,可以看到最近一次检测 VPN 连接状态的时间。
代码如下:

root@FC_R0:/tmp# ls vpn*
vpn_oldgw vpn_status_check.log
root@FC_R0:/tmp# cat vpn_status_check.log
Tue Jul 15 00:04:02 HKT 2014
root@FC_R0:/tmp#

你可以在 LuCI 中断开 VPN 接口,在接下来的4-5分钟,观察 WAN 和 VPN 的重连情况。
d.分别 traceroute www.google.comwww.baidu.com ,观察第二跳的地址:
代码如下:

FL-MBP:~ fatlyz$ traceroute www.google.com | head -n 3
traceroute: Warning: www.google.com has multiple addresses; using 74.125.239.115
traceroute to www.google.com (74.125.239.115), 64 hops max, 52 byte packets
fc_r0.lan (192.168.7.1) 2.161 ms 0.912 ms 0.895 ms
10.7.0.1 (10.7.0.1) 193.747 ms 187.789 ms 289.744 ms
23.92.24.2 (23.92.24.2) 259.323 ms 354.625 ms 408.535 ms

代码如下:

FL-MBP:~ fatlyz$ traceroute www.baidu.com | head -n 3
traceroute to www.a.shifen.com (180.76.3.151), 64 hops max, 52 byte packets
1 fc_r0.lan (192.168.7.1) 1.190 ms 0.984 ms 0.731 ms
2 58.111.43.1 (58.111.43.1) 20.616 ms 38.822 ms 18.484 ms
3 183.56.35.133 (183.56.35.133) 20.056 ms 52.353 ms 87.841 ms

可以看出,已成功对国内外的目标地址进行了路由选择。
至此,OpenWRT 路由的基本配置、PPTP VPN、chnroutes 和自动重连已经配置完成。

基于OpenWrt防火墙配置(作为二级路由)

OpenWRT下的防火墙管理是由配置文件“/etc/config/firewall”进行控制管理的。此文件可以使用UCI进行控制,也可以用vi编辑器直接修改。但如果两种方式都使用时需要注意UCI命令修改会产生缓存,每次修改好要尽快确认保存避免出现冲突。而该文件最终会在/etc/init.d/firewall启动的时候由UCI进行解码并且生成iptables规则生效。因此使用者不需要了解iptables即可通过配置文件实现防火墙控制。

防火墙的修改生效,需要重启防火墙执行以下指令:

/etc/init.d/firewallrestart

简单配置如下

uci show firewall
uci add firewall rule
uci set firewall.@rule[-1].name=LuCI
uci set firewall.@rule[-1].src=wan
uci set firewall.@rule[-1].proto=tcp
uci set firewall.@rule[-1].dest_port=80
uci set firewall.@rule[-1].target=ACCEPT
uci commit
uci add firewall rule
uci set firewall.@rule[-1].name=SSH
uci set firewall.@rule[-1].src=wan
uci set firewall.@rule[-1].proto=tcp
uci set firewall.@rule[-1].dest_port=22
uci set firewall.@rule[-1].target=ACCEPT
uci commit

#手动更改:高级设置-》Dropbear设置-》接口-》wan

uci add firewall rule
uci set firewall.@rule[-1].name=Samba_1
uci set firewall.@rule[-1].src=wan
uci set firewall.@rule[-1].proto=tcp
uci set firewall.@rule[-1].dest_port=445
uci set firewall.@rule[-1].target=ACCEPT
uci add firewall rule
uci set firewall.@rule[-1].name=Samba_2
uci set firewall.@rule[-1].src=wan
uci set firewall.@rule[-1].proto=tcp
uci set firewall.@rule[-1].dest_port=8200
uci set firewall.@rule[-1].target=ACCEPT
uci commit
uci add firewall rule
uci set firewall.@rule[-1].name=nginx
uci set firewall.@rule[-1].src=wan
uci set firewall.@rule[-1].proto=tcp
uci set firewall.@rule[-1].dest_port=99
uci set firewall.@rule[-1].target=ACCEPT
uci commit
uci add firewall rule
uci set firewall.@rule[-1].name=FTP
uci set firewall.@rule[-1].src=wan
uci set firewall.@rule[-1].proto=tcp
uci set firewall.@rule[-1].dest_port=21
uci set firewall.@rule[-1].target=ACCEPT
uci commit
uci add firewall rule
uci set firewall.@rule[-1].name=telnet
uci set firewall.@rule[-1].src=wan
uci set firewall.@rule[-1].proto=tcp
uci set firewall.@rule[-1].dest_port=23
uci set firewall.@rule[-1].target=ACCEPT
uci commit
/etc/init.d/firewall restart

猜你喜欢

转载自blog.csdn.net/qq_36905972/article/details/85063290
今日推荐