网络配置(有线、无线)

来源:互联网 发布:怎么评价杨振宁知乎 编辑:程序博客网 时间:2024/04/27 20:44

参考:树莓派官方文档
注:树莓派的官方文档已经共享在github上内容非常全,并且比国内的教程完善,建议参考。

一、有线网络

  1. 自动获取IP:只要将网线插入树莓派,即可自动获得IP
  2. 手动设定IP:如果电脑和树莓派直连,不能自动获得可以使用ifconfig配置
  3. 设置静态IP:如果担心同网络的ip不固定,可将电脑设为静态IP
  4.     sudo vim /etc/network/interfaces将  auto lo        iface lo inet loopback        iface eth0 inet manual修改为        auto lo        iface lo inet loopback        iface eth0 inet static        address 192.168.1.1        netmask 255.255.255.0        gateway 192.168.1.1

二、无线网络设置为STA(客户端)模式

  1. sudo vim /etc/wpa_supplicant/wpa_supplicant.conf

    添加上你要连接的wifi的账号和密码network={    ssid="i-NUIST"    key_mgmt=NONE}network={        ssid="FAST_84B7DA"        psk="1234567890"        key_mgmt=WPA-PSK}

三、无线网络设置为AP(服务器)模式

1.准备

  1. 更新树莓派系统

    sudo apt-get updatesudo apt-get dist-upgrade
  2. 安装所需软件

    sudo apt-get install dnsmasq hostapd
  3. 因为配置文件还没有写,先关闭这两个软件

    sudo systemctl stop dnsmasqsudo systemctl stop hostapd

2.配置成静态IP

我们将树莓派配置为独立的网络作为服务器,树莓派需要一个静态的ip地址分配给无线端口。

  1. 首先需要禁用wlan0的接口,dhcpcd的守护进程会分配一个ip地址给wlan0,所以禁用wlan0的接口,需要编辑dhcpcd.conf文件。

    sudo vim /etc/dhcpcd.conf在文件的结尾加上:denyinterfaces wlan0
  2. 配置静态IP地址,编辑文件interfaces,修改wlan0部分,我们将静态ip地址设置为192.168.0.1。

    sudo vim /etc/network/interfacesallow-hotplug wlan0  iface wlan0 inet static      address 192.168.0.1    netmask 255.255.255.0    network 192.168.0.0

    注:这里出现了一点问题,最后将配置这段配置放在#Include…,前面重启系统才配置成功

  3. 现在重启dhcpcd的守护进程来重启新的wlan0配置。

    sudo service dhcpcd restartsudo ifdown wlan0sudo ifup wlan0

3. 配置DHCP服务器(dnsmasq)

dnsmasq提供DHCP服务,默认的配置文件包含的信息太多,我们保存一下默认的文件,然后新建一个新的文件

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig  sudo vim /etc/dnsmasq.confinterface=wlan0  dhcp-range=192.168.0.2,192.168.0.20,255.255.255.0,24h

4. 配置hostapd

  1. 新建hostapd的配置文件

    sudo vim  /etc/hostapd/hostapd.conf#This is the name of the wifi interface we configured aboveinterface=wlan0#Use the nl80211 driver with the brcmfmac driverdriver=nl80211#This is the name of the networkssid=xzw#Use the 2.4GHZ band,a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11ghw_mode=g#Use channel 6channel=6#Enable 802.11nieee80211n=1#Enable WMMwmm_enabled=1#Enable 40MHz channels with 20ns guard intervalht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]#Accept all MAC addressesmacaddr_acl=0 #Use WPA authenticationauth_algs=1#Require clients to know the network nameignore_broadcast_ssid=0#Use WPA2wpa=2#Use a pre-shared keywpa_key_mgmt=WPA-PSK#The network passphrasewpa_passphrase=1234567890#Use AES,instead of TKIPrsn_pairwise=CCMP
  2. 告诉系统配置文件在哪

    sudo vim /etc/default/hostapd将#DAEMON_CONF修改为:DAEMON_CONF="/etc/hostapd/hostapd.conf"
  3. 启动之前关掉的服务

    sudo service hostapd start  sudo service dnsmasq start 

    启动玩之后重启,就能搜索到我们的热点。

原创粉丝点击