树莓派初始化安装

875 阅读4分钟
原文链接: nanjishidu.me

最近购置树莓派3,新版支持无线和蓝牙。部署lite版本做家用服务器使用,使用32Gtf卡做存储. 树莓派在家里做服务器使用,通过阿里云服务器做ssh隧道访问。

系统安装(MAC 烧录TF卡)

sudo dd if=2016-09-23-raspbian-jessie-lite.img of=/dev/rdisk2 bs=1m

串口支持(2016-09-23版本)

echo "dtoverlay=pi3-miniuart-bt" >>/boot/config.txt

修改源

cp /etc/apt/sources.list /etc/apt/sources.list.bak
vi /etc/apt/sources.list

添加

deb http://mirrors.aliyun.com/raspbian/raspbian/ jessie main non-free contrib rpi
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ jessie main non-free contrib rpi

修改后执行

apt update
apt upgrade

配置SSH

修改/etc/ssh/ssd-config将

PermitRootLogin without-password

PermitRootLogin yes 

支持root 权限登录

如果需要无密免登录

ssh-keygen -t rsa

生成rsa和rsa.pub,拷贝rsa.pub到远程服务器

cat rsa.pub >> ~/.ssh/authorized_keys

添加autossh支持

apt install autossh

添加ssh隧道支持

ssh隧道功能,可以实现一些代理功能或者是穿透内网功能

安装autossh后

vi /opt/bin/sshd.sh

添加

#!/bin/sh
autossh -M 5678 -C -N -g -o TCPKeepAlive=yes -o ServerAliveInterval=60 -o ServerAliveCountMax=1 -R 8080:127.0.0.1:80 root@gophper.com &

设置远程服务器监听8080端口代理树莓派80端口

参数说明

-g Allow remote hosts to connect to forwarded ports.

在-L/-R/-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。只是对本地转发有效,要使远程转发也使用外部ip,必须修改远程主机上的sshd_config,增加

GatewayPorts yes

-L port:host:hostport

将本地机(客户机)的某个端口转发到远端指定机器的指定端口.

ssh -C -f -N -g -L listen_port:DST_Host:DST_port user@Tunnel_Host

-R port:host:hostport

将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口.

ssh -C -f -N -g -R listen_port:DST_Host:DST_port user@Tunnel_Host

-D port

指定一个本地机器 “动态的’’ 应用程序端口转发.

ssh -N -f -D 8000 登录名@我的vps的ip

就相当与在本机上开了一个sock代理,端口是8000,给浏览器挂上127.0.0.1:8000的代理,查一下本机ip,发现已经变成vps的ip了。

-C Enable compression.

压缩数据传输。

-f Fork into background after authentication.

后台认证用户/密码,通常和-N连用,不用登录到远程主机。

-N Do not execute a shell or command.

不执行脚本或命令,通常与-f连用。

-p port Connect to this port. Server must be on the same port.

被登录的ssd服务器的sshd服务端口,省略就是默认的22端口。

添加crontab监听脚本

vi /opt/bin/monitor.sh

添加

#/bin/sh
ssh=`ps awx|grep "autossh"|grep -v grep`
if [ "$ssh" = "" ];then
       echo "`date '+%Y-%m-%d %H:%M:%S'` ssh is not exist,running..."
       /opt/bin/sshd.sh &
fi

修改 /etc/crontab添加autossh

	*  *    * * *   root    /opt/bin/monitor.sh>>/opt/bin/monitor.log

添加SSH公钥实现git免密

ssh-keygen -t rsa -C "email邮箱" -f id_rsa.mayun
ssh-keygen -t rsa -C "email邮箱" -f id_rsa.github

将 id_rsa.mayun.pub 添加码云ssh 将 id_rsa.github.pub 添加github ssh

因为创建了多个SSH公钥,需要config配置文件

vi  ~/.ssh/config
	
Host git.oschina.net
    HostName git.oschina.net
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa.mayun
Host nanjishidu.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa.github

验证是否添加成功

ssh -T git@git.oschina.net
ssh git@github.com

成功后发现每次提交时还是需要输入密码

git clone 重新拉取使用ssh链接,不使用默认的git链接。。。

树莓派AP

  • 安装hostapd和dnsmasq,dnsmasq用于管理dns和dhcp。hostapd提供基于Linux的无线接入点强WPA2加密和身份认证。

执行

apt-get install dnsmasq
apt-get install hostapd
  • 将无线网口wlan0 设置为静态地址,以太网eth0接入因特网。系统默认DHCPCD配置网络接口。
vi /etc/dhcpcd.conf

添加

interface wlan0  
static ip_address=10.10.0.1/24
  • 为防止wpa_supplicant的影响和干扰
vi /etc/network/interfaces

将wpa-conf注释

allow-hotplug wlan0
iface wlan0 inet manual
#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

执行

service dhcpcd restart
  • 配置HOSTAPD
vi /etc/hostapd/hostapd.conf

添加

interface=wlan0
driver=nl80211
#ssid蜜渍
ssid=gophper
hw_mode=g
channel=6
wmm_enabled=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
#加密方式
wpa=2
#密码
wpa_passphrase=1234876500
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP

执行

/usr/sbin/hostapd /etc/hostapd/hostapd.conf

如果可以搜索到表示成功,添加系统自启动hostapd

vi /etc/default/hostapd

修改

#DAEMON_CONF=""

DAEMON_CONF="/etc/hostapd/hostapd.conf"
  • 配置DNSMASQ为了实现上网功能,需要对DNSMASQ服务进行配置,

首先备份一下系统默认的文件,然后创建一个新的配置文件。

mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
vi /etc/dnsmasq.conf

具体的配置信息如下所示:

interface=wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=10.10.0.2,10.10.0.254,12h
  • 设置ipv4 转发
vi /etc/sysctl.conf

修改

net.ipv4.ip_forward=1

下一次重启后会生效,如果不想现在重启,可以直接输入如下指令直接生效。

echo 1 >/proc/sys/net/ipv4/ip_forward
  • 配置iptables防火墙

为了实现PI3以太网接口共享给wlan0上网,需要配置NAT,需先执行如下防火墙命令

iptables -t nat -APOSTROUTING -o eth0 -j MASQUERADE  
iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT  
iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

保存规则

iptables-save> /etc/iptables.rules

创建文件,添加以下内容,使防火墙开机启动

vi /etc/network/if-pre-up.d/iptables 

添加

#!/bin/bash
/sbin/iptables-restore < /etc/iptables.rules

添加执行权限

chmod +x /etc/network/if-pre-up.d/iptables
  • 最后需要重启服务,重启系统
service hostapd start
service dnsmasq start
reboot