How to set Raspberry Pi for remote access with VNC

บทนำ (Overview)

โดยปกติการใช้งาน “Raspberry PI” ถ้าไม่มีการต่อออกจอโดยใช้สาย “HDMI” หรือจอเฉพาะแล้วเราก็สามารถใช้งานได้โดยผ่าน “Remote Desktop” (เหมือนกับการเอาหน้าจอเครื่องคอมพิวเตอร์อีกเครื่องมาไวที่หน้าจอของเรา) จากเครื่องคอมพิวเตอร์ของเรา โดยที่ตัว “Raspberry PI” เพียงแค่ต่อ “Network” เข้ากับ “Wifi” หรือเสียบสาย “Lan” แค่นั้นเอง และไม่จำเป็นต้องต่อ “Keyboard” หรือ “Mouse” ที่ “Raspberry PI” เลย

ขั้นตอน (Steps)

  1.  ต่อสาย “Lan” เข้า “Raspberry Pi”
  2. เมื่อ “Boot” เครื่องเสร็จสิ้นเราสามารถใช้ โปรแกรม “Putty” บน “Windows” เพื่อ “remote access” ได้จากนั้นให้ “Login”
    login as: root
    root@192.168.0.23's password:
    Linux kali 3.18.11-v7 #1 SMP PREEMPT Tue Apr 7 12:12:57 CDT 2015 armv7l
    
    The programs included with the Kali GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Kali GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Fri Jun 26 03:33:32 2015
    
  3. จากนั้น “upgrade module” ต่าง ๆ โดยใช้คำสั่ง
    sudo apt-get update
    
  4. เมื่อ “upgrade” เสร็จสิ้นให้ “reboot” เครื่องโดยใช้คำสั่ง
    reboot
    
  5. จากนั้นดาวน์โหลด “VNC server” บน “Raspberi Pi”
    sudo apt-get install tightvncserver
    
  6. ตามด้วยติดตั้ง “Server” โดยใช้คำสั่ง “vncserver” แล้วจะบังคับให้เราตั้งรหัสผ่าน โดยรหัสผ่านจะต้องไม่สั้นไป สุดท้ายให้เลือก “n”
    root@kali:/etc# vncserver
    
    You will require a password to access your desktops.
    
    Password:
    Verify:
    Would you like to enter a view-only password (y/n)? n
    
    New 'X' desktop is kali:1
    
    Creating default startup script /root/.vnc/xstartup
    Starting applications specified in /root/.vnc/xstartup
    Log file is /root/.vnc/kali:1.log
    
    
  7. สร้างไฟล์สำหรับ start VNC server โดยใช้คำสั่ง
    sudo nano /etc/init.d/tightvncserver
    
  8. ถ้าไม่ถนัดให้ใช้ “WinSCP” แล้วสร้างไฟล์ชื่อ “tightvncserver” แล้วทั้งข้อ (7) และ (8) เพิ่มคำสั่งเช่นเดียวกันดังนี้
    #!/bin/sh
    # /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
    
    
  9. โดยไฟล์ “tightvncserver” จะต้องอยู่ในโฟลเดอร์ “/etc/ini.d” raspberrypivnc1
  10. เปลี่ยนสิทธิให้สามารถใช้งาน “VNC server”  ได้
    sudo chmod 755 /etc/init.d/tightvncserver
    
  11. โดยเราสามารถ เปิดหรือปิด “Server” ได้โดยใช้คำสั่งดังต่อไปนี้
    sudo /etc/init.d/tightvncserver start
    sudo /etc/init.d/tightvncserver stop
    
  12. จากนั้นที่เครื่อง client ให้ดาวน์โหลดโปรแกรม  “TightVNC Java Viewer” (www.tightvnc.com/download/2.7.2/tvnjviewer-2.7.2-bin.zip)
  13. ใช้โปรแกรม “nmap” ตรวจสอบ “port” ที่ติดตั้ง
    root@kali:~# nmap -sS 127.0.0.1
    
    Starting Nmap 6.47 ( http://nmap.org ) at 2015-06-26 05:12 UTC
    Nmap scan report for kali (127.0.0.1)
    Host is up (0.000091s latency).
    Not shown: 997 closed ports
    PORT     STATE SERVICE
    22/tcp   open  ssh
    5901/tcp open  vnc-1
    6001/tcp open  X11:1
    
    Nmap done: 1 IP address (1 host up) scanned in 0.77 seconds
    
    
  14. เปิดโปรแกรมกรอบ remote host (ip ของ raspberry pi) และ “port” ที่เราหามา (5901) raspberrypivnc2
  15. จากนั้นคลิก “connect” raspberrypivnc3
  16. สามารถ “remote desktop” ได้ raspberrypivnc4

Note:

  • ในกรณี Kali ไม่สามารถติดต่อกับ Network ได้ให้ใช้คำสั่ง
    sudo /etc/init.d/networking stop
    sudo /etc/init.d/networking start
    
  • ในกรณีเมื่อ restart และ port ไม่เปิด
    vncserver
    sudo /etc/init.d/tightvncserver start
    

ใส่ความเห็น