和我一起玩树莓派3B+--Raspbian网络配置(三)

一、网络配置

  1. 配置网络
root@raspberrypi:~# sudo nano /etc/dhcpcd.conf

#配置网卡IP
interface eth0
static ip_address=192.168.1.218/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8

#配置无线网卡静态IP
interface wlan0
static ip_address=192.168.1.118/24
static routers=192.168.1.1
static domain_name_servers=114.114.114.114

root@raspberrypi:~# sudo nano /etc/sysctl.conf

#禁用IPv6
net.ipv4.ip_forward=1
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1

静态IP配置:

#修改/etc/network/interfaces文件
iface eth0 inet static

address 192.168.1.88
netmask 255.255.255.0
gateway 192.168.1.1

#修改DNS
root@raspberrypi:~# sudo cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 208.67.220.220
nameserver 208.67.222.222
nameserver 10.10.10.10
  1. 查看网络信息:
root@raspberrypi:~# ifconfig
eth0: flags=4163  mtu 1500
        inet 192.168.1.218  netmask 255.255.255.0  broadcast 192.168.1.255
        ether b8:27:eb:70:6e:7e  txqueuelen 1000  (Ethernet)
        RX packets 664  bytes 55451 (54.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 762  bytes 56915 (55.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 314  bytes 29830 (29.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 314  bytes 29830 (29.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4099  mtu 1500
        ether b8:27:eb:25:3b:2b  txqueuelen 1000  (Ethernet)
        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

三、无线网络配置

  1. 查找WIFI:
root@raspberrypi:~# sudo iwlist wlan0 scan

root@raspberrypi:~#  sudo iwlist scan | grep ESSID
eth0      Interface doesn't support scanning.

lo        Interface doesn't support scanning.

                    ESSID:"TP-LINK_98C0"
                    ESSID:"404"
                    ESSID:"CMCC-qVYW"
                    ESSID:"ChinaNet-304-5G"
                    ESSID:"ChinaNet-304"
                    ESSID:"fcx"
                    ESSID:"Tenda_479220"
                    ESSID:"Honor 10"
                    ESSID:"@PHICOMM_BC"

SSID 是你的无线名称
PSK是你的无线密码

root@raspberrypi:~#  sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=CN

network={
   #你的无线网络ssid
    ssid="xxxxxx"
   #你wifi的密码
    psk="xxxxxxxx"
    key_mgmt=WPA-PSK
    priority=1
}
root@raspberrypi:~# ifconfig wlan0
# 重启网络
root@raspberrypi:~# sudo /etc/init.d/networking restart

注意:最好重启机子。
2.连接WiFi

root@raspberrypi:~# sudo ifup wlan0

3.断开WiFi

root@raspberrypi:~# sudo ifdown wlan0
  1. 连接外网
root@raspberrypi:~# ping www.baidu.com
PING www.wshifen.com (103.235.46.39) 56(84) bytes of data.
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=1 ttl=46 time=275 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=2 ttl=46 time=306 ms

二 、开启SSH

root@raspberrypi:~# sudo systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
或
root@raspberrypi:~# sudo systemctl start ssh
#查看状态
pi@raspberrypi:~ $ service ssh status
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-04-10 23:31:28 CST; 1min 23s ago
  Process: 421 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
 Main PID: 462 (sshd)
   CGroup: /system.slice/ssh.service
           └─462 /usr/sbin/sshd -D

4月 10 23:31:28 raspberrypi systemd[1]: Starting OpenBSD Secure Shell server...
4月 10 23:31:28 raspberrypi sshd[462]: Server listening on 0.0.0.0 port 22.
4月 10 23:31:28 raspberrypi sshd[462]: Server listening on :: port 22.
4月 10 23:31:28 raspberrypi systemd[1]: Started OpenBSD Secure Shell server.
4月 10 23:32:35 raspberrypi sshd[870]: Accepted password for pi from 192.168.1.9 port 8492 ssh2
4月 10 23:32:35 raspberrypi sshd[870]: pam_unix(sshd:session): session opened for user pi by (uid=0)

三、远程连接树莓派

远程连接树莓派

四、树莓派启用root账户

pi@raspberrypi:~$ sudo passwd root
Enter new UNIX password:   #输入第一遍密码
Retype new UNIX password:  #输入第二遍密码

 #解锁
root@raspberrypi:~# sudo passwd --unlock root
passwd: password expiry information changed.
# 如果出错,原因是 新版本ssh默认关闭root登陆 你可以修改一下ssh的配置文件

# Ctrl + W 快捷键 搜索 PermitRootLogin without-password
# 修改 PermitRootLogin without-password 为 PermitRootLogin yes
# Ctrl + O 快捷键 保存
# Ctrl + O 快捷键 退出 Nano 编辑器
PermitRootLogin yes

PermitRootLogin

五、VNC远程控制

  1. 下载地址:
    Server
    https://www.realvnc.com/en/connect/download/vnc/
    viewer
    https://www.realvnc.com/en/connect/download/viewer/
    下载Viewer:
    下载Viewer

Server安装:


VNC安装

image.png

image.png

image.png

image.png

image.png

image.png

Viewer安装:


image.png

image.png

image.png
  1. 树莓派板子安装
    第一种方式:
root@raspberrypi:~# sudo raspi-config  

设置:


接口设置

VNC设置

VNC设置

VNC设置
  1. windows连接


    打开VNC

    新建连接

    输入地址与名称

    点击继续

    输入用户密码

    远程界面
  2. VNC配置使用

pi@raspberrypi:~ $ vncserver -geometry 1422x800

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)? y
Password: 
Warning: password truncated to the length of 8.
Verify:   

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

pi@raspberrypi:~ $ tightvncserver

New 'X' desktop is raspberrypi:2

Starting applications specified in /home/pi/.vnc/xstartup
Log file is /home/pi/.vnc/raspberrypi:2.log

运行服务

根据这个服务添写IP地址:
其中:2 为端口号
192.168.1.19:2


设置IP

(1)设置服务自启动:

#添加文件
pi@raspberrypi:~ $ sudo nano /etc/init.d/vncserver
#内容
#!/bin/sh
### BEGIN INIT INFO
# Provides:          vncserver
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop vncserver
### END INIT INFO
 
# More details see:
# http://www.penguintutor.com/linux/vnc
 
### Customize this entry
# Set the USER variable to the name of the user to start vncserver under
export USER='pi'
### End customization required
 
eval cd ~$USER
 
case "$1" in
  start)
    # 启动命令行。此处自定义分辨率、控制台号码或其它参数。
    su $USER -c '/usr/bin/vncserver -depth 16 -geometry 1024x768 :1'
    echo "Starting VNC server for $USER "
    ;;
  stop)
    # 终止命令行。此处控制台号码与启动一致。
    su $USER -c '/usr/bin/vncserver -kill :1'
    echo "vncserver stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/vncserver {start|stop}"
    exit 1
    ;;
esac
exit 0

pi@raspberrypi:~ $ sudo update-rc.d vncserver defaults
# 重启服务器
pi@raspberrypi:~ $ sudo reboot

# 然后给增加执行权限,并设置开机启动:
pi@raspberrypi:~ $ sudo chmod 777 /etc/init.d/vncserver
# 添加开机启动项
pi@raspberrypi:~ $ sudo update-rc.d vncserver defaults
# 取消开机启动
pi@raspberrypi:~ $ sudo update-rc.d -f vncserver remove
启动服务:
pi@raspberrypi:~ $sudo service vncserver start   #启动
pi@raspberrypi:~ $sudo service vncserver stop    #停止
  1. 连接方式:


    连接方式
  2. tightvncserver方式
    VNCServer与tightvncserver二者选其一即可。
#自己安装
pi@raspberrypi:~ $ sudo apt-get install tightvncserver
#置vnc访问密码 
pi@raspberrypi:~ $ sudo   vncpasswd
Using password file /root/.vnc/passwd
Password: 
Warning: password truncated to the length of 8.
Verify:   
Would you like to enter a view-only password (y/n)? y
Password: 
Warning: password truncated to the length of 8.
Verify:   

六、RDP远程控制

  1. 安装
#安装xrdp
pi@raspberrypi:~ $ sudo apt-get install xrdp
#安装tightvncserver
pi@raspberrypi:~ $ sudo apt-get install tightvncserver
# 使能开机自动启动RDP SERVER
pi@raspberrypi:~ $ sudo systemctl enable xrdp
  1. 打开Window系统命令行输入mstsc


    输入命令
远程连接
输入用户名与密码
远程桌面

你可能感兴趣的:(和我一起玩树莓派3B+--Raspbian网络配置(三))