Headless Raspberry Pi - Setup WiFi and Static IP Address

Objective
Here we will learn how to Setup headless Raspberry Pi. We want to setup Raspberry Pi headless to access it via PuTTY (ssh) on a different machine or PC (using the same WiFi 2.4GHz network) with a static IP address.

방법 1



Follow Step A to Step D for setup Raspberry Pi without connecting it to a keyboard and monitor.

Step-A Setup wifi manually



Once we wrote image to SD card(see, Step 1 Download and Write Image to SD card). We need to setup wifi manually for Headless Raspberry Pi. Create wpa_supplicant.conf file, enter country, ssid, psk (password) details.
# In this example I am using macOS
cd /Volumes/boot/
nano wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=«your_ISO-3166-1_two-letter_country_code»

network={
    ssid="«your_SSID»"
    psk="«your_PSK»"
}

Step-B Enable SSH



To enable SSH on Raspberry Pi, please create an empty file with the name ssh (without any extension). Place this file in the root directory of the SD card.
# In this example I am using macOS
cd /Volumes/boot/
touch ssh

Step-C Scan IP address



Insert sd card in Raspberry Pi and power on. Wait for a minute.
Now search IP through one of the following app
(windows): htps //w w. 아 d ゔ ㄱ せ d ㅇ ps 칸 r. 코m/
(macOS): IP Scanner app (download from App Store)
ex.
# In this example I am using macOS



Step-D: Connect via ssh



Connect to your Raspberry Pi via ssh, use the IP address given in your result (Step-C)
# In this example I am using macOS (terminal app), on Windows you can use PuTTY.
ssh [email protected] -p 22

Password 'raspberry'


Method 2



If above method is not working for you, you need to follow Step 1 to 7 by connecting Raspberry Pi to a keyboard and a monitor.

Step 1 Download and Write Image to SD 카드



Download 'RASPBIAN STRETCH LITE' 이미지

  • htps //w w. 등 sp 벨 ry 피. 오 rg / 도 w 응 아 ds / 등 sp 비안 /

  • Using 'Etcher' software to write 'RASPBIAN STRETCH LITE' Image to the SD card
  • Download Etcher: h tps : // 에 t ぇ r. 이오/

  • Step 2 Power On


  • Plug in keyboard, monitor HDMI cable and insert SD card into Pi
  • Plug the power cable into Pi
  • Login with user name 'pi' and password 'raspberry'

  • Step 3 Switch to 'Root' 사용자



    To access Pi with 'Root' user, we will setup a new Password for the 'Root' user.
    #login as a root user
    pi@raspberrypi:~ $ sudo -i
    
    #see the user name 'root'
    root@raspberrypi:~#
    
    #set a new password for the root user
    root@raspberrypi:~# passwd
    
    #Enter new UNIX password:
    #Retype new UNIX password:
    #passwd: password updated successfully
    root@raspberrypi:~#
    

    To access pi as a 'Root' user via ssh, we need to allow 'PermitRootLogin'.
    #set 'PermitRootLogin' as 'yes'
    root@raspberrypi:~# nano /etc/ssh/sshd_config
    
    #Find 'PermitRootLogin prohibit-password' text and replace it as 'PermitRootLogin yes'
    PermitRootLogin yes
    
    #save sshd_config file (ctrl + x)
    

    Step 4 Enable SSH Server



    Enable remote command line access to your Pi using SSH
    root@raspberrypi:~# raspi-config
    
    #select -> '5 Interfacing Options Configure connections to peripherals'
    #select -> 'P2 SSH Enable/Disable remote command line access to your Pi using SSH'
    #'Would you like the SSH server to be enabled?' Yes
    
    #'The SSH server is enabled' Ok
    

    Step 5 Setup WiFi Network



    Find SSID (You can use your mobile or computer)


    You can also try the following command to find SSID via your Raspberry Pi.
    root@raspberrypi:~# iwlist scan | grep SSID
    
                        ESSID:"HUMAX-58FAB"
                        ESSID:"HUMAX-59092"
    lo        Interface doesn't support scanning.
    
    eth0      Interface doesn't support scanning.
    
                        ESSID:"HUMAX-C86EB"
    
    root@raspberrypi:~# raspi-config
    
    # Select -> '2. Network Options Configure network settings'
    # Select -> 'N2 Wi-fi Enter SSID and passphrase'
    # 'Please enter SSID' 
    # Ok
    # 'Please enter passphrase. Leave it empty if none.'
    # Ok
    # Finish
    
    #Reboot your Pi (see at the left bottom and type 'reboot' command) 
    root@raspberrypi:~# reboot
    
    # Login with user 'root' and Enter the password.
    
    # Test outgoing traffic
    root@raspberrypi:~# ping google.com
    #64 bytes from xxxx-in-xx.xx.net (xxx): icmp_seq=1 ttl=53 time=49.9 ms
    # press (ctrl + c)
    

    Step 6 Setup static IP


    #1. Enter following command
    root@raspberrypi:~# ip -4 addr show dev wlan0 | grep inet
    #output, ex.
    inet 192.168.179.1/24 brd 192.168.179.255 scope global wlan0
    
    #2. Enter Following command
    root@raspberrypi:~# cat /etc/resolv.conf
    # Generated by resolvconf, ex.
    # nameserver 192.168.179.1
    
    #3. Enter Following command to create a backup file
    root@raspberrypi:~# cp /etc/dhcpcd.conf /etc/dhcpcd.conf.backup
    
    #4. Open dhcpcd.conf file
    root@raspberrypi:~# nano /etc/dhcpcd.conf 
    
    #Based on the above information (#1 and #2), we will add following lines to the top of dhcpcd.conf
    # We want to use static ip_address as 192.168.179.111 (based on #1, it should be in between lower address bound 192.168.179.1 and upper address bound 192.168.179.255), values of static routers and static domain_name_servers (based on #2)
    
    interface wlan0
    static ip_address=192.168.179.111
    static routers=192.168.179.1
    static domain_name_servers=192.168.179.1
    
    #save the file (Ctrl + x) and reboot your pi
    root@raspberrypi:~# reboot
    

    Step 7 Connect via an another machine



    We can try to access Pi (Connection Type: SSH) with "Host Name: 192.168.179.111"and "Port: 22"via an another machine but using the same wi-fi network. You can use PuTTY. In the following example I am using 'Terminal' app on my Mac computer.
    ssh [email protected] -p 22
    
    #The authenticity of host '192.168.179.111 (192.168.179.111)' can't be established.
    #ECDSA key fingerprint is SHA256:A32M**************************************.
    # Are you sure you want to continue connecting (yes/no)? yes
    
    #Enter the password (That we set in Step 3 for the 'root' user)
    [email protected]'s password: 
    
    root@raspberrypi:~# 
    



    Happy Coding!
    My Website - htps //w w. Py 보 ys. 코m/

    좋은 웹페이지 즐겨찾기