文章目录
  1. 1. 网络配置
  2. 2. 有关服务的启动
  3. 3. Cron配置
  4. 4. MediaTomb配置
  5. 5. Samba配置
  6. 6. IPython Notebook配置
  7. 7. TigerVNC配置
  8. 8. 如何备份

其实网上的关于raspberry pi的manual, document, blog实在是多如牛毛,能怎么折腾的基本上都写完了。写这这篇的目的在于网上关于raspbian的文档实在太乱,同时我在折腾的时候习惯不好,喜欢瞟一眼不懂原理就C-C,C-V,所以在这里整理下(要是个个都像Arch Wiki一样文档写的齐全我会不认真的看吗(╯°Д°)╯︵ ┻━┻))

首先,我入手的价格是这样的

  • 带电源的USB Hub 48.5
  • EDUP 无线网卡 29
  • 带外壳和电源 raspberry pi B版 278

一共354.5RMB

羞涩的来张图
server

现在我在pi上面使用raspbian,开了VNC,SSH,UPnP,Samba,FTP,Ipython notebook,目前很和谐。

请注意,以下的配置基于2015-05-05的raspbian镜像,版本不同配置可能会有微小的差异。好了,现在听我一个个细细道来(摩拳擦掌)。

网络配置

pi可以外接显示器的,可是我这里没有。幸好官方默认开了SSH和DHCP,插上有线你就可SSH进PI了
(请到路由器的配置界面看获得的内网IP)。

我的需求是pi和路由器不能放到一块,而且为了以后安装方便,pi需要

  1. 开机与路由器无线连接
  2. 获得静态IP

raspbian是基于Debian优化的版本,所以配置上大多和Debian是兼容的。

我的配置如下(/etc/network/interfaces)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /home/pi/wlan0.conf
iface default inet static
address 192.168.1.7
gateway 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

关于/etc/network/interfaces的具体内容可以man interaces,在debian network configuration你也可以找到很多的例子

简单的来讲,/etc/network/interfacesifupifdown的配置文件。

带头的行是视为注释所忽略的

该文件的主题是”iface”, “mapping”, “auto”,
“allow-“ 和 “source”所标志的节。

有关服务的启动

请看Init阮一峰的博客还有debian manual

Init是系统中第一个运行的进程,而且是所有进程的parent。它读取/etc/inittab。

然后我们看/etc/inittab,其中有一行是这样的

1
2
3
# Boot-time system configuration/initialization script.
# This is run first except when booting in emergency (-b) mode.
si::sysinit:/etc/init.d/rcS

man inittab中解释inittab的结构是

1
id:runlevels:action:process

而sysinit的意思是开机启动,此时这里的runlevel可以忽略。继续向下看,我们看到这几行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# /etc/init.d executes the S and K scripts upon change
# of runlevel.
#
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6

wait表示当一旦特定runlevel进入之后,该线程才会启动。

然后你看看/etc/init.d/rc的内容,充满了对/etc/rc$runlevel.d脚本的调用。而rc$runlevel.d就是我们接下来要讨论的

我们试着ll /etc/rc$runlevel.d

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
lrwxrwxrwx 1 root root 20 Jun 29 00:39 K06nfs-common -> ../init.d/nfs-common
lrwxrwxrwx 1 root root 17 Jun 29 00:39 K06rpcbind -> ../init.d/rpcbind
-rw-r--r-- 1 root root 677 Jul 15 2013 README
lrwxrwxrwx 1 root root 18 May 7 06:16 S01bootlogs -> ../init.d/bootlogs
lrwxrwxrwx 1 root root 20 May 7 06:51 S01cgroup-bin -> ../init.d/cgroup-bin
lrwxrwxrwx 1 root root 16 May 7 07:16 S01dhcpcd -> ../init.d/dhcpcd
lrwxrwxrwx 1 root root 17 May 7 06:38 S01ifplugd -> ../init.d/ifplugd
lrwxrwxrwx 1 root root 14 May 7 06:16 S01motd -> ../init.d/motd
lrwxrwxrwx 1 root root 17 May 7 06:38 S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 15 Jun 29 01:41 S01samba -> ../init.d/samba
lrwxrwxrwx 1 root root 14 May 7 06:38 S01sudo -> ../init.d/sudo
lrwxrwxrwx 1 root root 22 May 7 06:38 S01triggerhappy -> ../init.d/triggerhappy
lrwxrwxrwx 1 root root 14 May 7 06:38 S02cron -> ../init.d/cron
lrwxrwxrwx 1 root root 14 May 7 06:38 S02dbus -> ../init.d/dbus
lrwxrwxrwx 1 root root 24 May 7 06:39 S02dphys-swapfile -> ../init.d/dphys-swapfile
lrwxrwxrwx 1 root root 14 Jun 29 00:34 S02kodi -> ../init.d/kodi
lrwxrwxrwx 1 root root 13 May 7 06:38 S02ntp -> ../init.d/ntp
lrwxrwxrwx 1 root root 15 May 7 07:00 S02rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 13 May 7 07:30 S02ssh -> ../init.d/ssh
lrwxrwxrwx 1 root root 22 May 7 06:38 S03avahi-daemon -> ../init.d/avahi-daemon
lrwxrwxrwx 1 root root 17 May 7 07:30 S03lightdm -> ../init.d/lightdm
lrwxrwxrwx 1 root root 19 Jun 29 02:38 S04
tomb -> ../init.d/mediatomb
lrwxrwxrwx 1 root root 18 Jun 29 00:39 S04minidlna -> ../init.d/minidlna
lrwxrwxrwx 1 root root 18 May 7 06:38 S04plymouth -> ../init.d/plymouth
lrwxrwxrwx 1 root root 18 May 7 06:38 S04rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 19 May 7 06:38 S04rmnologin -> ../init.d/rmnologin

这里的运行规则是什么捏?我摘一段

When init(8) or telinit(8) commands goes into the runlevel to ““, the system basically executes the initialization scripts as follows.

The script names starting with a “K” in “/etc/rc.d/“ are executed in alphabetical order with the single argument “stop”. (killing services)

The script names starting with an “S” in “/etc/rc.d/“ are executed in alphabetical order with the single argument “start”. (starting services)

For example, if you had the links “S10sysklogd” and “S20exim4” in a runlevel directory, “S10sysklogd” which is symlinked to “../init.d/sysklogd” would run before “S20exim4” which is symlinked to “../init.d/exim4”.

/etc/init.d/...里面是什么东西?打开看看把

/et/init.d/cron为例,翻到最后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Parse the system's environment
if [ "$READ_ENV" = "yes" ] ; then
...skipping...
start) log_daemon_msg "Starting periodic command scheduler" "cron"
start_daemon -p $PIDFILE $DAEMON $EXTRA_OPTS
log_end_msg $?
;;
stop) log_daemon_msg "Stopping periodic command scheduler" "cron"
killproc -p $PIDFILE $DAEMON
RETVAL=$?
[ $RETVAL -eq 0 ] && [ -e "$PIDFILE" ] && rm -f $PIDFILE
log_end_msg $RETVAL
;;
restart) log_daemon_msg "Restarting periodic command scheduler" "cron"
$0 stop
$0 start
;;
reload|force-reload) log_daemon_msg "Reloading configuration files for periodic command scheduler" "cron"
# cron reloads automatically
log_end_msg 0
;;
status)
status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
;;
*) log_action_msg "Usage: /etc/init.d/cron {start|stop|status|restart|reload|force-reload}"
exit 2
;;
esac
exit 0

挂不得我们重启服务的时候都是sudo /etc/init.d/... restart

那么用service ... restart是什么鬼?你可以看/usr/sbin/service,他自己也说了,serviceA convenient wrapper for the /etc/init.d init scripts.

Cron配置

Cron 是一个强大的任务计划程序,支持 GNU/Linux 和许多其他的操作系>统。它可以通过在给定时间运行命令来自动化周期性的任务。

详情可以见CronHowto

我把他用来定时下载网盘里面的电影

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
45 1 * * * cd /home/pi/media/dlnashare/yun;/home/pi/bypy/bypy.py syncdown| /usr/bin/logger -t error.log

MediaTomb配置

UPnP这个协议恐怕很多人不熟悉,来一段维基

通用即插即用(英语:Universal Plug and >Play,简称UPnP)是由“通用即插即用论坛”(UPnP™ >Forum)推广的一套网络协议。该协议的目标是使家庭网络(数据共享、通信和娱乐>)和公司网络中的各种设备能够相互无缝连接,并简化相关网络的实现。UPnP通过定>义和发布基于开放、因特网通讯网协议标准的UPnP设备控制协议来实现这一目标。

而MediaTomb就是这么一个UPnP服务器。

MediaTomb基本上不用配置,开了就可以用。

Samba配置

网上有关于Samba的介绍和配置Share your Raspberry Pi’s files and folders across a network

IPython Notebook配置

Ipython notebook是写Python的神器,打开网页就可以写python。更加欢乐的是,它支持代码补全,高亮,对齐。对于对IDE依赖不深的Python来说,基本上还是足够了。

装好之后没有带自启动脚本,来来来,我们自己写一个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
### BEGIN INIT INFO
# Provides:ipython3
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Required-Start:
# Required-Stop:
# Short-Description: Start ipython-notebook Server at boot time
# Description: Start ipython-notebook Server at boot time.
### END INIT INFO
USER=pi
HOME=/home/pi
export USER HOME
case "$1" in
start)
echo "Starting ipython-notebook Server"
su -c '/usr/bin/ipython3 notebook --no-browser --notebook-dir=/home/pi/ipython --ip="*"' pi 2&>1
;;
stop)
echo "Stopping ipython-notebook Server"
/usr/bin/killall ipython3
;;
*)
echo "Usage: /etc/init.d/ipython-notebook {start|stop}"
exit 1
;;
esac
exit 0

别忘了/etc/init.d/下面的文件都是755权限

1
sudo chmod 755 ipython3

然后想把link这个脚本到rc-….d?不用啦,有update-rc.d

1
sudo update-rc.d ipython3 defaults

TigerVNC配置

VNC(Virtual Network Computing)是什么东西?
来摘一段鸟哥的吧

VNC server 會在伺服器端啟動一個監聽用戶要求的埠口,一般埠口號碼在 >5901 ~ 5910 之間。當用戶端啟動 X server 連線到 5901 >之後, VNC server 再將一堆預先設定好的 X client >透過這個連線傳遞到用戶端上,最終就能夠在用戶端顯示伺服器的圖形介面了。

然后依旧是写一段/etc/init.d/vncboot脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh
### BEGIN INIT INFO
# Provides: vncboot
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Server at boot time
# Description: Start VNC Server at boot time.
### END INIT INFO
USER=root
HOME=/root
export USER HOME
case "$1" in
start)
echo "Starting VNC Server"
#Insert your favoured settings for a VNC session
/usr/bin/vncserver :0 -geometry 1920x1080 -depth 16 -pixelformat rgb565
;;
stop)
echo "Stopping VNC Server"
/usr/bin/vncserver -kill :0
;;
*)
echo "Usage: /etc/init.d/vncboot {start|stop}"
exit 1
;;
esac
exit 0

如何备份

dd大法好!

1
dd if=/dev/你的sd卡 of=随便哪里 bs=4M

搞定,恢复只要反向dd回来就可以了

文章目录
  1. 1. 网络配置
  2. 2. 有关服务的启动
  3. 3. Cron配置
  4. 4. MediaTomb配置
  5. 5. Samba配置
  6. 6. IPython Notebook配置
  7. 7. TigerVNC配置
  8. 8. 如何备份