【樹莓派】樹莓派網絡配置:靜態IP、無線網絡、服務等


一.網絡配置之靜態IP:

樹莓派的默認網絡為:

haochuang@raspberrypi:~ $ vi /etc/network/interfaces
# interfaces(
5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d auto lo iface lo inet loopback iface eth0 inet manual allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf allow-hotplug wlan1 iface wlan1 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

這個只要插上網線,即可自動獲取到dhcp地址;

 

但如果需要設置為靜態IP,則需要簡單進行設置一下,如下操作:

haochuang@raspberrypi:~ $ vi /etc/dhcpcd.conf

......
nohook lookup-hostname

interface eth0
 static ip_address=192.168.21.103/24
 static routers=192.168.21.1
 static domain_name_servers=61.134.1.4 218.30.19.40

設置完成之后,重啟網絡

haochuang@raspberrypi:~ $ sudo /etc/init.d/networking restart

 

二.配置無線網絡:

cat /etc/network/interfaces 

auto lo  
  
iface lo inet loopback  
iface eth0 inet dhcp  
  
#allow-hotplug wlan0  
#iface wlan0 inet manual  
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf  
#iface default inet dhcp  
auto wlan0  
allow-hotplug wlan0  
iface wlan0 inet dhcp  
        wpa-ssid "neu"  
        wpa-psk  "lucifer_chn"  

或者

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

#iface eth0 inet manual
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
#iface wlan0 inet manual
#iface wlan0 inet dhcp
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface wlan0 inet static
  wpa-ssid "Chinanet"
  wpa-psk "CTshi555666"
  address 172.27.35.20 
  netmask 255.255.0.0
  gateway 172.27.35.1


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

檢查 /etc/wpa_supplicant/wpa_supplicant.conf 文件:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
  ssid="Chinanet"
  psk="PWD-CTshi555666-passwd"
}

 

修改之后,重啟即可;

補充:

配置無線動態IP:

auto wlan0  
allow-hotplug wlan0  
iface wlan0 inet dhcp  
        wpa-ssid "name"  
        wpa-psk  "passwd" 

 

配置無線靜態IP:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
  wpa-ssid "name"
  wpa-psk "passwd"
  address 192.168.11.80
  netmask 255.255.255.0
  gateway 192.168.11.1

 

 

三.增加用戶的sudo權限:

為普通用戶增加sudo權限的方法,同其他一樣,需要修改visudo文件,如下;

haochuang@raspberrypi:~ $ sudo visudo

  GNU nano 2.2.6                                                  File: /etc/sudoers.tmp                                                                                                            

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
haochuang ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

pi ALL=(ALL) NOPASSWD: ALL





                                                                                         [ Read 30 lines ]
^G Get Help                     ^O WriteOut                     ^R Read File                    ^Y Prev Page                    ^K Cut Text                     ^C Cur Pos
^X Exit                         ^J Justify                      ^W Where Is                     ^V Next Page                    ^U UnCut Text                   ^T To Spell

增加其中標紅的一行,並Ctrl+X,Yes保存退出;

 

三.增加自動啟動服務:

1.編寫自動啟動腳本;

haochuang@raspberrypi:~ $ sudo cat /etc/init.d/osprey 
#!/bin/sh
### BEGIN INIT INFO
# Provides:          osprey
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start: $local_fs
# Required-Stop:
# chkconfig: 12345 99 05
# Short-Description: Start or stop the testapp App.
### END INIT INFO

start(){
    echo -e "\033[32m start testapp \033[0m"
    cd /home/haochuang/webapp/testapp
    su lifeccp -c "sh /home/haochuang/webapp/testapp/start --spring.profiles.active=test &"
}

stop(){
    echo -e "\033[32m stop osprey \033[0m"
    pkill -f testapp

    app_pid=$(pgrep -f testapp)
    if [ "${app_pid}" = "" ]; then
        echo -e  "\033[32m -=stop testapp finished=- \033[0m"
    else
        echo -e  "\033[31m -=stop testapp failed=- \033[0m"
        kill -9 "${app_pid}"
        echo -e  "\033[32m -=kill -9 testapp=- \033[0m"
    fi
}

case $1 in
start)
    start
    ;;
stop)
    stop
    ;;
*)
    echo -e "\033[32m Usage: $0 (start|stop) \033[0m"
    ;;
esac

注意上面腳本中的\033[0m,其實是在為服務控制指令加色,想了解詳細,可參考后面"其他"的補充描述;

 

2.增加到 /etc/init.d中,並修改增加可執行權限;

chomd +x testapp

 

3.增加到啟動項中

chkconfig add /etc/init.d/testapp

 

其他:

顏色特效控制:

printf("\033[1;33m Hello World. \033[0m \n");
顏色如下:
none = "\033[0m"
black = "\033[0;30m"
dark_gray = "\033[1;30m"
blue = "\033[0;34m"
light_blue = "\033[1;34m"
green = "\033[0;32m"
light_green -= "\033[1;32m"
cyan = "\033[0;36m"
light_cyan = "\033[1;36m"
red = "\033[0;31m"
light_red = "\033[1;31m"
purple = "\033[0;35m"
light_purple = "\033[1;35m"
brown = "\033[0;33m"
yellow = "\033[1;33m"
light_gray = "\033[0;37m"
white = "\033[1;37m"

字背景顏色范圍: 40--49 字顏色: 30--39
40: 黑 30: 黑
41:紅 31: 紅
42:綠 32: 綠
43:黃 33: 黃
44:藍 34: 藍
45:紫 35: 紫
46:深綠 36: 深綠
47:白色 37: 白色

 

輸出特效格式控制:

\033[0m 關閉所有屬性
\033[1m 設置高亮度
\03[4m 下划線
\033[5m 閃爍
\033[7m 反顯
\033[8m 消隱
\033[30m -- \033[37m 設置前景色
\033[40m -- \033[47m 設置背景色

 

光標位置等的格式控制:

\033[nA 光標上移n行
\03[nB 光標下移n行
\033[nC 光標右移n行
\033[nD 光標左移n行
\033[y;xH設置光標位置
\033[2J 清屏
\033[K 清除從光標到行尾的內容
\033[s 保存光標位置
\033[u 恢復光標位置
\033[?25l 隱藏光標
\33[?25h 顯示光標

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM