192.168.9.229 pi / f2f 192.168.9.229/phpmyadmin admin / f2f ============================ 軟體更新 && 軟體套件升級 sudo apt update -y && sudo apt upgrade -y apt update -y && apt upgrade -y IP : sudo nano /etc/dhcpcd.conf sudo ifconfig eth0 down sudo ifconfig eth0 up 設定樹莓派系統參數 sudo raspi-config 1. i2c 2. ttl 3. View WiFi sudo apt install wavemon -y ============= CRT DS3231 ============== nano /boot/config.txt dtoverlay=i2c-rtc,ds3231 ======================= network-manager (會當機 Pi3 B) ======================= sudo apt-get install network-manager -y sudo nmtui ================================== Node-RED安裝 https://hackmd.io/@LHB-0222/Node-RED ================================= bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) 執行Node-RED 由於Raspberry Pi的內存有限 可以使用附加參數啟動Node-RED node-red-pi --max-old-space-size=256 node-red-pi --max-old-space-size=128 作為伺服器運行 如果要在打開或重新啟動Pi時運行Node-RED執行下方指令 sudo systemctl enable nodered.service sudo service nodered restart 但還要把Node-RED重啟,才能讓此服務啟動 $ node-red-stop ====================== 改 Port 1880 --> 18180 ====================== nano /root/.node-red/settings.js Search for the line Ctrl + W --> find text uiPort: process.env.PORT || 1880, and change it to uiPort: process.env.PORT || 18180 Search for the line Ctrl + W --> find adminAuth Raspberry $2b$08$gyS8xtP1E1ioUJQaaUNpqObnhUpLQaP0RXPdvbbgkWdsgZEl0Iosi ccsccs Debian Password $2b$08$pGrYgKosP5sfaGwn6nsgyuHaWreNJ9qHQ75uuR/JpZqM1ggSDcNSy ccsccs 連線 從另一台電腦瀏覽時 應使用Pi的IP地址 http://:1880 您可以通過hostname -I在Pi上運行找到IP地址。 到settings.js文件中編輯用戶管理及權限。 $ nano ~/.node-red/settings.js 在這個檔案中,我們應該可以找到一段adminAuth開頭的註釋段。它看起來像底下這樣: adminAuth: { type: "credentials", users: [{ username: "admin", password:"$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.", permissions: "*" }] } (用戶名預設為admin、密碼預設為password) 取消註釋,並將剛剛複製加密的密碼貼到password欄位後””之間,保存settings.js(CTRL+O),然後退出(CTRL+X),到此我們就完成設定了。 但還要把Node-RED重啟,才能讓此服務啟動 $ node-red-stop $ node-red-start 這邊再說說一些設定 Permissions欄位有兩個選擇 * - full access可寫可讀 read - read-only access只能讀 而如果想讓大家都可以進來但只能讀,可以如下設定 adminAuth: { type: "credentials", users: [ /* list of users */ ], default: { permissions: "read" } } '大同_中山_淡水_陽明'.includes(convert.SiteName) 陳信嘉老師 https://hackmd.io/@Shinjia/Sko2zzDQE?type=view#-Class-01-20190226 $2b$08$gyS8xtP1E1ioUJQaaUNpqObnhUpLQaP0RXPdvbbgkWdsgZEl0Iosi ccsccs [ { username: "admin", password: "$2b$08$gyS8xtP1E1ioUJQaaUNpqObnhUpLQaP0RXPdvbbgkWdsgZEl0Iosi", permissions: "*" }, { username: "lou", password: "$2b$08$afC1oVTO4ssoC/.qQGnf1e1gKbsL9RQM/YIh6mPPNbC.S25zqVIzW", permissions: "*" }, { username: "yunlou", password: "$2b$08$yVvjny8qy261C9jjtjoJNe0R5H3gpvRsAD9vSDHnGk.lhQ02kiWNG", permissions: "*" } init http://192.168.8.152:1880/ ===================================== 更新核心和韌體到最新版本 sudo rpi-update sudo init 6 (一定要) =============================== sudo apt install apache2 -y ===> index.html apache --> ok cd /var/www/html 取得 IP hostname -I sudo service apache2 restart sudo apt install php -y apt install apache2 -y && service apache2 restart && apt install php -y && service apache2 restart && apt install mariadb-server -y && service apache2 restart && apt install sqlite3 php-sqlite3 -y && service apache2 restart 資料庫連線權限 sudo mysql_secure_installation sudo mysql create user admin@localhost identified by 'f28203416f'; grant all privileges on *.* to admin@localhost; create user lou@192.168.9.168 identified by 'f28203416f'; grant all privileges on *.* to lou@192.168.9.168; create user lou@192.168.9.99 identified by 'f28203416f'; grant all privileges on *.* to lou@192.168.9.99; create user lou@192.168.77.110 identified by 'f28203416f'; grant all privileges on *.* to lou@192.168.77.110; FLUSH PRIVILEGES; exit; # # 要先建 帳號 # sudo apt install phpmyadmin -y sudo service apache2 restart # SQLite sudo apt install sqlite3 (SQLite 資料庫) sudo apt install php-sqlite3 (PHP SQLite 資料庫) apt install sqlite3 php-sqlite3 -y && service apache2 restart sudo service apache2 restart ========================================================================================= 防火牆 ========== sudo iptables -A INPUT -i eth0 -p tcp -s 192.168.9.0/24 --dport 3306:3306 -j ACCEPT sudo iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT ========================================================================================= netstat -tulnpu apt install ufw -y ufw是一個主機端的iptables類防火牆配置工具 ufw allow from 192.168.9.0/24 to any port 3306 ufw allow from 192.168.71.0/24 to any port 3306 ufw allow from 192.168.31.0/24 to any port 3306 ufw enable 啟用 sudo systemctl enable ufw.service sudo ufw allow from 10.0.0.0/8 sudo ufw allow from 172.16.0.0/12 sudo ufw allow from 192.168.9.0/24 sudo ufw allow from 192.168.71.0/24 sudo ufw allow from 192.168.31.0/24 sudo ufw allow 3306 sudo ufw allow 80 允許外部訪問80端口 sudo ufw delete allow 80 禁止外部訪問80 端口 sudo ufw allow from 192.168.1.1 允許此IP訪問所有的本機端口 sudo ufw deny smtp 禁止外部訪問smtp服務 sudo ufw delete allow smtp 刪除上面建立的某條規則 sudo ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 要拒絕所有的流量從TCP的10.0.0.0/8 到端口22的地址192.168.0.1 sudo ufw allow from 192.168.9.99 取得溫度資訊 vcgencmd measure_temp 取得電壓資訊 vcgencmd measure_volts core 開關機 sudo reboot #重開機 sudo halt #關機(比sudo poweroff安全) 套件管理 sudo apt-get update #更新軟體列表 sudo apt-get upgrade #更新軟體包 sudo apt-get install <軟體名稱> #安裝套件 sudo apt-get remove <軟體名稱> #移除套件 查看SD卡空間狀況 df -h 設定樹莓派系統參數 sudo raspi-config 查看溫度 vcgencmd measure_temp 查看CPU和GPU記憶體 vcgencmd get_mem arm #CPU vcgencmd get_mem gpu #GPU 查看韌體版本 vcgencmd version 執行Python程式 sudo python abc.py 安裝軟體 例如: sudo apt-get install GIMP sudo apt-get install VLC sudo apt-get install thunderbird 移除軟體 dpkg --list #顯示已安裝的軟體 sudo apt-get --purge remove XXX #刪除上面指令清單中的XXX軟體 使用pip安裝Python套件 sudo pip install <套件名稱> 安裝Python常用套件 apt install zip unzip (壓縮解壓縮) apt install php-sqlite3 pip3 install ipython (Python Shell) pip3 install requests (網頁擷取) pip3 install selenium (自動化工具) pip3 install django (網頁開發框架) sudo apt-get install sqlite3 (SQLite 資料庫) sudo apt-get install r-base (R 語言) sudo apt-get install nginx (Web 伺服器) sudo pip3 install uWSGI (WSGI 伺服器) pip3 install scipy (科學計算) pip3 install matplotlib (2D 繪圖) pip3 install pandas (數據處理) pip3 install scikit-learn (機器學習) 安裝截圖軟體scrot sudo apt-get install scrot 預設抓取的目錄在 /home/pi/ 中 使用 終端機輸入 scrot #抓取整個桌面 scrot -s #滑鼠選取範圍 scrot -u #抓目前視窗 安裝 nano 編輯器 sudo apt-get install nano 安裝PIP套件管理 sudo apt install python3-pip ===== python -m pip install --upgrade pip ==== pip install numpy (科學計算) pip install pandas (數據處理) pip install paho-mqtt pip install requests pip install beautifulsoup4 (網頁解析) pip3 install paho-mqtt requests beautifulsoup4 pip3 install numpy pandas pip3 install paho-mqtt requests beautifulsoup4 numpy pandas **** pip install 無效時使用 **** sudo apt-get install python3-pip -y ========================================================================== 樹莓派 使用(或安裝)進行安裝時,Raspberry Pi 設置有時會報告問題。 這些通常會提到:pip3 installpip libf77blas.so.3: cannot open shared object file: No such file or directory 解決方案將是: sudo apt-get install libatlas-base-dev 安裝自編譯的NumPy所期望的缺失庫(ATLAS是線性代數的可能提供者)。 或者使用Raspbian提供的NumPy。在這種情況下,請運行: pip3 uninstall numpy # remove previously installed version apt install python3-numpy ======================================================================= pip3 install numpy pandas pip3 install requests paho-mqtt beautifulsoup4 pyinstaller numpy pandas ipython ============== MQTT Server ============= sudo apt install mosquitto mosquitto-clients -y 4)要安裝Mosquitto Broker,請輸入以下以下命令: sudo apt install -y mosquitto mosquitto-clients 5)要使Mosquitto在Raspberry Pi啟動時自動啟動,您需要運行以下命令(這意味著Mosquitto代理將在Raspberry Pi啟動時自動啟動): sudo systemctl enable mosquitto.service 6)現在,通過執行以下命令測試安裝: mosquitto -v 這將返回當前在Raspberry Pi中運行的 Mosquitto 版本。它將是2.0.11或更高版本。 7) 執行以下命令以打開mosquitto.conf檔。 sudo nano /etc/mosquitto/mosquitto.conf 8)使用箭頭鍵移動到檔末尾,然後粘貼以下兩行: # 設定帳號密碼檔案 password_file /etc/mosquitto/passwd socket_domain ipv4 # 禁止匿名登入 allow_anonymous false # allow_anonymous true listener 18883 pi.ip.25u.com listener 18883 192.168.71.149 listener 18883 192.168.99.190 ======================================================== http://www.steves-internet-guide.com/mqtt-username-password-example/ Crade ---> Batch file password ex : nano pass jonggo:0920900090 lou:f28203416f joo:28859365 lily:28203416 test:12345678 admin:f28203416f sunny:0920007328 andy:13152991 mark:0926977990 admin:37545969 lou:f28203416f ping:26214477 may:25552490 test:123456789 產生密碼亂數 mosquitto_passwd -U passwordfile ex: mosquitto_passwd -U pass copy to mosquitto ex: cp pass /etc/mosquitto/pass ======== 單筆 --> file mosquitto_passwd -c passwordfile user ========= append 單筆 mosquitto_passwd -b /etc/mosquitto/passwd yunlou ccsccs cp pass /etc/mosquitto/passwd # 設定帳號密碼檔案 password_file /etc/mosquitto/passwd socket_domain ipv4 # 禁止匿名登入 allow_anonymous false # allow_anonymous true listener 18883 pi.ip.25u.com listener 18883 192.168.71.149 listener 18883 192.168.99.190 9)然後,按CTRL-X退出並保存檔。按 Y 並按回車鍵。 10) 使用以下命令重新啟動樹莓派,以使更改生效。 pi@raspberry:~$ sudo reboot CRT Enable I2C Open the /boot/config.txt using the below command. sudo nano /boot/config.txt The above command opens the config.tx file. If you are not familiar with the vi editor you can use whatever you want. Then add the below two lines. dtparam=i2c_arm=on dtoverlay=i2c-rtc,ds3231 Save the /boot/config.txt file and exit. Reboot your Raspberry Pi. Install I2C Tools Then install i2c-tools using the below command. sudo apt install i2c-tools -y Then check the DS3231 is detected or not using the installed i2c-tools. sudo i2cdetect -y 1 # show time sudo timedatectl sudo raspi-config 讀取到 RTC 的時間資訊: sudo hwclock -r 可以用 ntp 校時: sudo service ntp start 或手動使用 ntpdate: sudo ntpdate 0.tw.pool.ntp.org 如果沒有網路,也可以用 date 手動調整時間: date -s "2016-11-23 08:30:25" 時間寫入硬體的 RTC 中: ( Linux --> DS3231 ) sudo hwclock -w 接著在讀取硬體 RTC 的時間,看看是否正確: sudo hwclock -r 若要將 Linux 的系統時間設定為硬體 RTC 的時間,可以執行: (DS3231 --> Linux) sudo hwclock -s ===== 確認所有的時間都正確之後,接下來就是要設定讓樹莓派在開機時,可以自動啟用 DS3231 RTC,編輯 /etc/rc.local 設定檔, 在 exit 0 之前加入指令, sudo nano /etc/rc.local Rev 2 的板子則加入: sudo hwclock -s ========================= 固定 IP 固定 IP 的話不要改這個檔案,而是修改 /etc/dhcpcd.conf ========================= sudo nano /etc/dhcpcd.conf ========================= 設定擁有者與群組為 apache;目錄權限為 2755(2 表示新建子目錄的權限同父目錄): 2755 ================== DDNS --> ChangeIP ================== https://domoticproject.com/accessing-raspberry-ddns/ ================= sudo apt-get install ddclient -y nano /etc/ddclient.conf # Configuration file for ddclient generated by debconf # # /etc/ddclient.conf protocol=changeip \ use=web, web=ip.changeip.com \ login=ruru \ password='ccsccs' \ http://nic.ChangeIP.com/nic/update?system=dyndns&u=ruru&p=ccsccs&hostname=pi.ip.25u.com sudo service ddclient start sudo service ddclient status sudo ddclient -daemon=0 -verbose -noquiet -debug sudo ddclient -query ============ ip config ============ sudo nano /etc/dhcpcd.conf ifconfig eth0 down ifconfig eth0 up ==================== pppoe http://yhhuang1966.blogspot.com/2021/08/ppoe-ip.html =================== sudo apt-get -y install pppoeconf sudo pppoeconf home572 Hinet 76097663@hinet.net / lefihmax ========================================= View WiFi ========== sudo apt install wavemon -y ================= 如何在Ubuntu中設置網絡連接的優先級 最簡單命令:ifmetric。可以使用 sudo apt-get install ifmetric進行安裝。此命令可用於更改任何接口的度量。具有較低度量標準的接口是Internet的首選。 https://ubuntuqa.com/zh-tw/article/8377.html ================ sudo apt install ifmetric 使用route命令查看指 route 置網絡連接的優先級u sudo ifmetric wlan0 500 sudo ifmetric eth0 100 ======================== 把/etc/hostname的主機名改為跟/etc/hosts的一致 ======================== sudo nano /etc/hosts nano /etc/hosts sudo nano /etc/hostname nano /etc/hostname 127.0.0.1 localhost 127.0.0.1 abc ======================= ================ Add Service delay start ExecStartPre=/bin/sleep 30 ================= nano /lib/systemd/system/fb_cnt.service ===== [Unit] Description=FaceBook count service After=multi-user.target [Service] ExecStart=/home/pi/fb_cnt/dist/fb_yunlou & WorkingDirectory=/home/pi/fb_cnt/dist/ ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartPreventExitStatus=255 Type=notify [Install] WantedBy=multi-user.target Alias=fb_cnt.service ======= 啟用service systemctl enable fb_cnt.service 查詢狀態 systemctl status fb_cnt.service 停止service systemctl disable fb_cnt.service 在/etc/systemd/system 中看到 fb_cnt.service 的連結 cd /lib/systemd/system/ ls -l fb* 列出全部的process後查找你設定的service systemctl -a ============================================================== ssl Pi3 --> 自發憑證 blog.yunlou.net ====================== Let's Encrypt是為您的Raspberry Pi完全免費輕鬆獲得安全且經過認證的SSL證書 1. DNS , 2 real IP (pppOe or static ) 2.1 pppOe + ddns (ddclient) 2.2 static dsn (itn , hinet, 亞太 ....) 3. Python 4. Apache or nginx ======================= 有 apache + python ======================= sudo apt install python3-certbot-apache sudo certbot --apache 1. email --> 2. add server --> Y 3. read pdf --> Y 3. domain --> pi.ip.25u.com =================================== 有 apache + nginx =========== blog.yunlou.net https://fedingo.com/how-to-install-ssl-certificate-raspberry-pi/ ==================================== Let's Encrypt 只有 python 待續 沒有 nginx , Apache sudo apt install certbot ============================= monitor cpu =========== text sudo apt install htop -y ======================== windown ======= sudo apt install conky -y =========================== sudo apt-get install ufw -y sudo ufw default allow # 預設允許 sudo ufw default deny # 預設封鎖 sudo ufw allow 22 # 允許外部訪問 22 端口 sudo ufw enable 作用:開啟了防火牆並隨系統啟動同時關閉所有外部對本機的訪問(本機訪問外部正常)。 這樣設置已經很安全,如果有特殊需要,可以使用 sudo ufw allow 開啟相應服務。 關閉 sudo ufw disable 查看防火牆狀態 sudo ufw status 開啟/禁用相應端口或服務舉例 sudo ufw allow 80 允許外部訪問80端口 sudo ufw delete allow 80 禁止外部訪問80 端口 sudo ufw allow from 192.168.1.1 允許此IP訪問所有的本機端口 sudo ufw deny smtp 禁止外部訪問smtp服務 sudo ufw delete allow smtp 刪除上面建立的某條規則 要拒絕所有的流量從TCP的10.0.0.0/8 到端口 22 的地址 192.168.0.1 sudo ufw deny prot tcp from 10.0.0.0/8 to 192.168.0.1 port 如果你只是不想讓某個小明偷偷連到你的 SSH Port,你也可以針對他封鎖: sudo ufw deny from 192.168.11.7 to any port 22 甚至可以一次允許一個範圍的 port: sudo ufw allow 6000:6007/tcp # 允許 TCP 6000~6007 sudo ufw allow 6000:6007/udp # 允許 UDP 6000~6007 ufw allow from 10.0.0.0/8 ufw allow from 172.16.0.0/12 ufw allow from 192.168.71.0/24 ufw allow from 192.168.9.0/24 ufw allow from 192.168.31.0/24 sudo ufw allow 80 # 允許 80 sudo ufw allow 443 # 允許 443 sudo ufw deny 3389 # 封鎖 3389 sudo ufw deny 21 # 封鎖 21 sudo ufw allow from 192.168.11.10 # 允許 192.168.11.10 的所有連線 sudo ufw allow from 192.168.11.0/24 # 允許 192.168.11.1~192.168.11.255 的所有連線 sudo ufw deny from 192.168.11.4 # 封鎖 192.168.11.4 的所有連線 ========================== sudo ufw status numbered 這個指令會幫你把規則前面加上編號: sudo ufw delete 3 那個規則就不見囉! UFW 防火牆:擋掉 Ping sudo nano /etc/ufw/before.rules # ok icmp codes -A ufw-before-input -p icmp --icmp-type destination-unreachable -j DROP -A ufw-before-input -p icmp --icmp-type source-quench -j DROP -A ufw-before-input -p icmp --icmp-type time-exceeded -j DROP -A ufw-before-input -p icmp --icmp-type parameter-problem -j DROP -A ufw-before-input -p icmp --icmp-type echo-request -j DROP 最後再重新啟動 ufw 即可: sudo ufw disable ; sudo ufw enable =========================== ufw allow 3306 && ufw allow 443 && ufw allow 22 && ufw allow 80 && ufw allow 18180 && ufw allow from 192.168.71.0/24 && ufw allow from 192.168.31.0/24 && ufw allow from 192.168.9.0/24 =============== ntp Server ============== 步驟 2:使用 apt-get 安裝 NTP 伺服器 請以 sudo 身份執行以下命令,以便從 APT 儲存庫安裝 NTP 伺服器守護程式: $ sudo apt install ntp -y 步驟 3:驗證安裝(可選) 您可以透過終端端中執行以下命令來驗證 NTP 安裝並檢查版本號: $ sntp --version 步驟 4:切換到離您的位置最近的 NTP 伺服器池 安裝 NTP 伺服器時,它主要配置為獲取適當的時間。但是,您可以將伺服器池切換到離您的位置最近的伺服器池。 這包括在 /etc/ntp.conf 文件中進行一些更改。 通過執行以下命令,在 nano 編輯器中以 sudo 身份開啟該檔案: $ sudo nano /etc/ntp.conf 台灣 NTP Server time.stdtime.gov.tw clock.stdtime.gov.tw pool time1.google.com iburst pool time2.google.com iburst pool time-a.nist.gov iburst pool time-b.nist.gov iburst pool tock.usno.navy.mil iburst 步驟 5:重新啟動 NTP 伺服器 為了使上述更改生效,您需要重新啟動NTP伺服器。為此,請以 sudo 身份執行以下命令: $ sudo service ntp restart 步驟 6:驗證 NTP 伺服器是否正在運行 現在,通過以下命令檢查 NTP 服務的狀態: $ sudo service ntp status 步驟 7:配置防火牆,以便用戶端可以訪問 NTP 伺服器 最後,是時候配置系統的UFW防火牆了,以便傳入連接可以訪問UDP埠號為123的NTP伺服器。 以 sudo 身份執行以下命令,為傳入流量打開埠 123: $ sudo ufw allow from any to any port 123 proto udp ntpq -p ddsn restart sudo service ddclient restart pppoe http://blog.itist.tw/2015/02/raspberry-pi-pppoe-connection.html 斷線 poff 重新撥接連線可以用「pon」指令。 sudo pon dsl-provider 查詢一下目前撥接的狀態。 plog