0. 准備工作
硬件:
- 樹莓派 1個
- 網線 1根
- 電腦 1個
- TF卡 1張 (推薦16G及以上)
- 讀卡器 1個
軟件
- advanced_ip_scanner.exe IP檢索軟件
- win32diskimager.exe 燒寫軟件
- putty.exe 遠程登錄軟件
- tightvncviewer.exe 遠程桌面軟件
1. 下載鏡像
登錄樹莓派官網,下載官方鏡像,並解壓為 .img 文件
注:DESKTOP 版本是有桌面環境的,Lite 版本沒有桌面環境
2. 燒寫鏡像
win32diskimager 是鏡像讀寫軟件,使用此軟件燒錄樹莓派系統。
由於后續需要遠程登錄系統,需要開啟系統的ssh服務,默認是未開啟的。
操作:打開讀卡器根路徑,新建文本文檔並改名為 ssh (無后綴),即可。
3. 連接網絡
注意:此處默認PC機都是有無線網絡和有線網絡的,如果是台式機,沒有無線網絡。需要通過手機的USB共享網絡功能,讓樹莓派連接到手機網絡中,通過juiceSSH等軟件進行遠程登錄操作,可查閱相關資料,在此不做贅述。
由於需要樹莓派和PC機同網絡,因此,將無線網絡分享給有線網絡,然后用網線將PC和樹莓派連接起來。
連接圖如下:
在電腦上設置把無線網絡共享給有線網絡(以win7為例):
打開控制面板,找到網絡連接,右擊 無線網絡連接-屬性,點擊共享選項卡,選擇共享到本地連接,勾選允許,確定。
打開命令行工具(win徽標鍵+R,輸入cmd)。輸入ipconfig 查看本地網絡ip
此時的樹莓派和PC機是共享了同樣的有線網,但是如何知道樹莓派的地址呢?
下面就要使用 advance_ip_scanner.exe 查看樹莓派的IP地址了。
查看結果如下圖:
制造商帶有 Raspberry Pi 字樣的 IP地址就是我們要找的IP。
4. 遠程登錄樹莓派
上述中,192.168.137.69 就是我們要登錄的IP地址。此處我們使用putty登錄IP地址。
當然也可以使用其他的遠程登錄軟件,只是putty輕便小巧,無記錄。
首次登錄,彈出安全警告,點擊確認。
默認的用戶名是 pi
密碼是 raspberry (注意:linux系統中,密碼都是不可見的,不要以為沒有輸入進去而多次輸入。)
回車后,登錄成功。
5. 設置WiFi網絡
此處選擇最簡單的設置方法,因此使用樹莓派自帶的配置界面進行網絡設置。
命令行中輸入 sudo raspi-config
pi@raspberrypi:~ $ sudo raspi-config
彈出設置GUI界面如下:
選擇 2 NetWork Options ,回車。
選擇 N2 Wi-fi ,回車。
選擇國家,輸入 C ,定位到 CN China ,回車。
點擊OK。
輸入wifi名稱,按↓箭頭,選擇OK,回車。
輸入密碼,選擇OK,回車。
返回菜單首頁,tab鍵選擇Finish。回車。完成設置。
輸入 ifconfig 。查看當前網絡。
pi@raspberrypi:~ $ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.137.69 netmask 255.255.255.0 broadcast 192.168.137.255 inet6 fe80::ebcd:2a3e:5a3e:d31 prefixlen 64 scopeid 0x20<link> ether b8:27:eb:ca:cd:4e txqueuelen 1000 (Ethernet) RX packets 3197 bytes 196291 (191.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 970 bytes 247748 (241.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.103 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::e91c:4f1e:5794:fc0a prefixlen 64 scopeid 0x20<link> ether b8:27:eb:9f:98:1b txqueuelen 1000 (Ethernet) RX packets 38 bytes 5328 (5.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 36 bytes 5971 (5.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 pi@raspberrypi:~ $
找到wlan0,會發現網絡已連接,IP為192.168.0.103
再打開putty,輸入192.168.0.103 發現已經可以正常連接。
終於,我們可以拔掉網線,放飛樹莓派,讓它自由飛翔。(大霧)
至此,我們可以隨心所欲的操作我們的raspberry pi了。但是,丑媳婦總要見公婆的。我們沒有顯示屏,也想看看這媳婦怎么辦?
6. 換源並更新軟件
樹莓派軟件源地址在國外,由於大家都懂的原因,想要下載順暢絲滑,需要更換國內源,此處以阿里雲的源為例更改。
6.1 修改源列表
命令行中輸入:
pi@raspberrypi:~ $ sudo nano /etc/apt/sources.list
修改文本內容:
# deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi # Uncomment line below then 'apt-get update' to enable 'apt-get source' #deb-src http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi deb http://mirrors.aliyun.com/raspbian/raspbian/ stretch main contrib non-free rpi
命令行中輸入:
pi@raspberrypi:~ $ sudo nano /etc/apt/sources.list.d/raspi.list
修改文本內容:
# deb http://archive.raspberrypi.org/debian/ stretch main ui # Uncomment line below then 'apt-get update' to enable 'apt-get source' #deb-src http://archive.raspberrypi.org/debian/ stretch main ui deb http://mirrors.aliyun.com/raspbian/raspbian/ stretch main ui
注意:可以使用以下任意源,但是需要明確自己是什么版本的系統,此處的樹莓派系統是 stretch 相應更換此代號即可。
感謝: 此博客[1] 提供的源。
目前最新的樹莓派中國大陸地區的軟件源: 中國科學技術大學 Raspbian http://mirrors.ustc.edu.cn/raspbian/raspbian/ 阿里雲 Raspbian http://mirrors.aliyun.com/raspbian/raspbian/ 清華大學 Raspbian http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ 華中科技大學 Raspbian http://mirrors.hustunique.com/raspbian/raspbian/ Arch Linux ARM http://mirrors.hustunique.com/archlinuxarm/ 華南農業大學(華南用戶) Raspbian http://mirrors.scau.edu.cn/raspbian/ 大連東軟信息學院源(北方用戶) Raspbian http://mirrors.neusoft.edu.cn/raspbian/raspbian/ 重慶大學源(中西部用戶) Raspbian http://mirrors.cqu.edu.cn/Raspbian/raspbian/ 新加坡國立大學 Raspbian http://mirror.nus.edu.sg/raspbian/raspbian 牛津大學 Raspbian http://mirror.ox.ac.uk/sites/archive.raspbian.org/archive/raspbian/ 韓國KAIST大學 Raspbian http://ftp.kaist.ac.kr/raspbian/raspbian/
6.2 換取最新軟件包
輸入 sudo apt-get update 命令,獲取最先軟件包。
pi@raspberrypi:~ $ sudo apt-get update Get:1 http://mirrors.aliyun.com/raspbian/raspbian stretch InRelease [15.0 kB] Get:2 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf Packages [11.7 MB] Get:3 http://mirrors.aliyun.com/raspbian/raspbian stretch/contrib armhf Packages [56.9 kB] Get:4 http://mirrors.aliyun.com/raspbian/raspbian stretch/non-free armhf Packages [95.2 kB] Get:5 http://mirrors.aliyun.com/raspbian/raspbian stretch/rpi armhf Packages [1,360 B] Fetched 11.8 MB in 10s (1,151 kB/s) Reading package lists... Done W: Target Packages (main/binary-armhf/Packages) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4 W: Target Packages (main/binary-all/Packages) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4 W: Target Translations (main/i18n/Translation-en_GB) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4 W: Target Translations (main/i18n/Translation-en) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4 W: Target Packages (main/binary-armhf/Packages) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4 W: Target Packages (main/binary-all/Packages) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4 W: Target Translations (main/i18n/Translation-en_GB) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4 W: Target Translations (main/i18n/Translation-en) is configured multiple times in /etc/apt/sources.list:4 and /etc/apt/sources.list.d/raspi.list:4
6.3 升級並安裝所有軟件包
pi@raspberrypi:~ $ sudo apt-get upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages will be upgraded: libjavascriptcoregtk-4.0-18 libruby2.3 libssh-gcrypt-4 libwebkit2gtk-4.0-37 ruby2.3 5 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 14.8 MB of archives. After this operation, 2,301 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf libwebkit2gtk-4.0-37 armhf 2.18.6-1~deb9u1+rpi1 [9,709 kB] Get:2 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf libjavascriptcoregtk-4.0-18 armhf 2.18.6-1~deb9u1+rpi1 [1,846 kB] Get:3 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf libruby2.3 armhf 2.3.3-1+deb9u3+rpi1 [2,865 kB] Get:4 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf libssh-gcrypt-4 armhf 0.7.3-2+deb9u1 [144 kB] Get:5 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf ruby2.3 armhf 2.3.3-1+deb9u3+rpi1 [188 kB] Fetched 14.8 MB in 4s (3,348 kB/s) Reading changelogs... Done (Reading database ... 105923 files and directories currently installed.) Preparing to unpack .../libwebkit2gtk-4.0-37_2.18.6-1~deb9u1+rpi1_armhf.deb ... Unpacking libwebkit2gtk-4.0-37:armhf (2.18.6-1~deb9u1+rpi1) over (2.16.6-0+deb9u1+rpi1) ... Preparing to unpack .../libjavascriptcoregtk-4.0-18_2.18.6-1~deb9u1+rpi1_armhf.deb ... Unpacking libjavascriptcoregtk-4.0-18:armhf (2.18.6-1~deb9u1+rpi1) over (2.16.6-0+deb9u1+rpi1) ... Preparing to unpack .../libruby2.3_2.3.3-1+deb9u3+rpi1_armhf.deb ... Unpacking libruby2.3:armhf (2.3.3-1+deb9u3+rpi1) over (2.3.3-1+deb9u1+rpi1) ... Preparing to unpack .../libssh-gcrypt-4_0.7.3-2+deb9u1_armhf.deb ... Unpacking libssh-gcrypt-4:armhf (0.7.3-2+deb9u1) over (0.7.3-2) ... Preparing to unpack .../ruby2.3_2.3.3-1+deb9u3+rpi1_armhf.deb ... Unpacking ruby2.3 (2.3.3-1+deb9u3+rpi1) over (2.3.3-1+deb9u1+rpi1) ... Setting up libssh-gcrypt-4:armhf (0.7.3-2+deb9u1) ... Processing triggers for libc-bin (2.24-11+deb9u3) ... Processing triggers for man-db (2.7.6.1-2) ... Setting up libjavascriptcoregtk-4.0-18:armhf (2.18.6-1~deb9u1+rpi1) ... Setting up libruby2.3:armhf (2.3.3-1+deb9u3+rpi1) ... Setting up libwebkit2gtk-4.0-37:armhf (2.18.6-1~deb9u1+rpi1) ... Setting up ruby2.3 (2.3.3-1+deb9u3+rpi1) ... Processing triggers for libc-bin (2.24-11+deb9u3) ... pi@raspberrypi:~ $ sudo apt-get upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
7. 安裝VNC Server
想看“丑媳婦”,就需要借助vnc工具了。
VNC(Virtual Network Console)虛擬網絡控制台,原理是在我們的Pi上安裝Server,然后在PC上安裝Viewer,通過網絡,實現顯示內容的傳輸。
7.1 安裝 tightvncserver
命令行中執行:sudo apt-get install tightvncserver
pi@raspberrypi:~ $ sudo apt-get install tightvncserver Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libxfont1 xfonts-base xfonts-encodings xfonts-utils Suggested packages: tightvnc-java The following packages will be REMOVED: realvnc-vnc-server The following NEW packages will be installed: libxfont1 tightvncserver xfonts-base xfonts-encodings xfonts-utils 0 upgraded, 5 newly installed, 1 to remove and 0 not upgraded. Need to get 7,225 kB of archives. After this operation, 24.0 MB disk space will be freed. Do you want to continue? [Y/n] y Get:1 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf libxfont1 armhf 1:1.5.2-4 [120 kB] Get:2 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf tightvncserver armhf 1:1.3.9-9 [550 kB] Get:3 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf xfonts-encodings all 1:1.0.4-2 [574 kB] Get:4 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf xfonts-utils armhf 1:7.7+4 [70.4 kB] Get:5 http://mirrors.aliyun.com/raspbian/raspbian stretch/main armhf xfonts-base all 1:1.0.4+nmu1 [5,911 kB] Fetched 7,225 kB in 3s (2,155 kB/s) (Reading database ... 105924 files and directories currently installed.) Removing realvnc-vnc-server (6.3.1.36657) ... Selecting previously unselected package libxfont1:armhf. (Reading database ... 105847 files and directories currently installed.) Preparing to unpack .../libxfont1_1%3a1.5.2-4_armhf.deb ... Unpacking libxfont1:armhf (1:1.5.2-4) ... Selecting previously unselected package tightvncserver. Preparing to unpack .../tightvncserver_1%3a1.3.9-9_armhf.deb ... Unpacking tightvncserver (1:1.3.9-9) ... Selecting previously unselected package xfonts-encodings. Preparing to unpack .../xfonts-encodings_1%3a1.0.4-2_all.deb ... Unpacking xfonts-encodings (1:1.0.4-2) ... Selecting previously unselected package xfonts-utils. Preparing to unpack .../xfonts-utils_1%3a7.7+4_armhf.deb ... Unpacking xfonts-utils (1:7.7+4) ... Selecting previously unselected package xfonts-base. Preparing to unpack .../xfonts-base_1%3a1.0.4+nmu1_all.deb ... Unpacking xfonts-base (1:1.0.4+nmu1) ... Processing triggers for mime-support (3.60) ... Processing triggers for desktop-file-utils (0.23-1) ... Setting up tightvncserver (1:1.3.9-9) ... update-alternatives: using /usr/bin/tightvncserver to provide /usr/bin/vncserver (vncserver) in auto mode update-alternatives: using /usr/bin/Xtightvnc to provide /usr/bin/Xvnc (Xvnc) in auto mode update-alternatives: using /usr/bin/tightvncpasswd to provide /usr/bin/vncpasswd (vncpasswd) in auto mode Setting up xfonts-encodings (1:1.0.4-2) ... Setting up libxfont1:armhf (1:1.5.2-4) ... Processing triggers for libc-bin (2.24-11+deb9u3) ... Processing triggers for man-db (2.7.6.1-2) ... Processing triggers for shared-mime-info (1.8-1+deb9u1) ... Processing triggers for gnome-menus (3.13.3-9) ... Processing triggers for hicolor-icon-theme (0.15-1) ... Processing triggers for fontconfig (2.11.0-6.7) ... Setting up xfonts-utils (1:7.7+4) ... Setting up xfonts-base (1:1.0.4+nmu1) ...
7.2 執行 tightvncserver
命令行中輸入:tightvncserver
pi@raspberrypi:~ $ tightvncserver You will require a password to access your desktops. Password:輸入密碼 Warning: password truncated to the length of 8. Verify:再次輸入密碼 Would you like to enter a view-only password (y/n)? n # 此處問是否需要創建可遠觀而不可褻玩的密碼,我選擇否,如果需要給別人開放一個查看密碼,選擇y New 'X' desktop is raspberrypi:1 Creating default startup script /home/pi/.vnc/xstartup Starting applications specified in /home/pi/.vnc/xstartup Log file is /home/pi/.vnc/raspberrypi:1.log
7.3 PC端登錄
可以使用 tightvncviewer 軟件。
打開軟件,輸入IP地址和端口(端口默認為5901)點擊Connect連接。
輸入剛才填寫的密碼,OK確認。
終於讓我們揭開了兒媳婦的面紗(這媳婦還挺俊hhhhhh)。
然后,我們就可以任意的操作樹莓派的系統了。
本文已完。。。。。。。。那是不可能的。
一旦我們重啟樹莓派,再次登錄,就沒法登入vnc界面了。
這是為什么呢?
因為我們沒有讓 tightvncserver 開機自啟動。
7.4 讓 tightvncserver 開啟自啟動[2]
7.4.1 新建腳本
pi@raspberrypi:~ $ sudo nano /etc/init.d/tightvncserver
編輯輸入:
#!/bin/sh
### BEGIN INIT INFO
# Provides: tightvncserver
# Required-Start: $syslog $remote_fs $network
# Required-Stop: $syslog $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts VNC Server on system start.
# Description: Starts tight VNC Server. Script written by James Swineson.
### END INIT INFO
# /etc/init.d/tightvncserver
VNCUSER='pi'
case "$1" in
start)
su $VNCUSER -c '/usr/bin/tightvncserver :1'
echo "Starting TightVNC Server for $VNCUSER"
;;
stop)
pkill Xtightvnc
echo "TightVNC Server stopped"
;;
*)
echo "Usage: /etc/init.d/tightvncserver {start|stop}"
exit 1
;;
esac
exit 0
7.4.3 依次執行
pi@raspberrypi:~ $ sudo chmod 755 /etc/init.d/tightvncserver # 添加執行權限 pi@raspberrypi:~ $ sudo update-rc.d tightvncserver defaults # 設置開啟啟動 pi@raspberrypi:~ $ sudo reboot # 重啟
7.4.5 取消開機啟動
pi@raspberrypi:~ $ sudo update-rc.d -f tightvncserver remove
至此,本文真的葯丸了。
關於其他的關於樹莓派的食用記錄:
參考文獻:
[1]: 樹莓派更換中國大陸地區軟件源