No internet connection
  1. Home
  2. Technical Support

how to set a static IP on 2.5.0 without Internet access

By Jeff Crews @jeffcrews
    2022-12-29 23:15:17.892Z

    I just reflashed my TinyPilot Pro to 2.5.0 after I could not get it to work
    I wanted to set a static IP but I did not want to have to put this TinyPilot on the Internet just to download a script to apply the static IP address as shown in this post:
    https://tinypilotkvm.com/faq/static-ip

    I went to this post:
    Setting static IP address
    and see to edit /etc/dhcpd.conf and add some lines to this file. Is this /etc/dhcpd.conf file suppose to already exist? Or did this change between this post and the 2.5.0 release of TinyPilot

    I really feel that turning static IP on and off using command line is fine, but we need to be able to do this without being on the Internet.

    • 2 replies
    1. J
      Jeff Crews @jeffcrews
        2022-12-29 23:44:54.741Z

        I just downloaded the script on another machine, copied and pasted it to the TinyPilot over SSH and got the static IP changed.

        Seems like this script should be included on the TinyPilot software.

        This is the contents of set-static-ip.sh

        #!/bin/bash
        
        # Usage
        #
        # Set a static IP:
        #   $ export TINYPILOT_STATIC_IP="10.0.0.223" # Set to the desired IP address
        #   $ export ROUTERS="10.0.0.1" # Set to your router's IP address
        #   $ export DNS="${ROUTERS}" # Set to your preferred DNS server IP address.
        #   $ ./set-static-ip.sh
        #
        # Remove a previously set static IP:
        #   $ unset TINYPILOT_STATIC_IP
        #   $ ./set-static-ip.sh
        
        set -eu
        
        readonly CONFIG_FILE="/etc/dhcpcd.conf"
        readonly MARKER_START='# --- AUTOGENERATED BY TINYPILOT - START ---'
        readonly MARKER_END='# --- AUTOGENERATED BY TINYPILOT - END ---'
        
        dhcpcd_original=()
        is_in_marker_section='false'
        while IFS= read -r line; do
          if "${is_in_marker_section}" && [[ "${line}" == "${MARKER_END}" ]]; then
            is_in_marker_section='false'
            continue
          fi
          if "${is_in_marker_section}" || [[ "${line}" == "${MARKER_START}" ]]; then
            is_in_marker_section='true'
            continue
          fi
          dhcpcd_original+=("${line}")
        done < "${CONFIG_FILE}"
        
        if "${is_in_marker_section}"; then
          echo 'Unclosed marker section' >&2
          exit 1
        fi
        
        # Convert array of lines to a single string.
        readonly OLD_CONFIG_FILE=$(printf "%s\n" "${dhcpcd_original[@]}")
        
        set +u
        
        # Write original file contents back to the file.
        echo "${OLD_CONFIG_FILE}" | sudo tee "${CONFIG_FILE}" > /dev/null
        
        # If the user has specified a static IP, append a section defining the
        # static IP.
        if [[ ! -z "${TINYPILOT_STATIC_IP}" ]]; then
          {
            echo "${MARKER_START}"
            echo "interface eth0"
            echo "static ip_address=${TINYPILOT_STATIC_IP}"
            echo "static routers=${ROUTERS}"
            echo "static domain_name_servers=${DNS}"
            echo "${MARKER_END}"
          } | sudo tee --append "${CONFIG_FILE}" > /dev/null
          echo "Set static IP to ${TINYPILOT_STATIC_IP}"
        else
          echo "Reverted static IP changes to ${CONFIG_FILE}"
        fi
        
        
        1. C
          In reply tojeffcrews:

          Hi Jeff, thank you for your great question about setting a static IP address.

          I'm pleased to hear that you were able to solve this already. Your suggestion of distributing the script along with the TinyPilot software is very sensible, so we've opened an issue to add it in. You can subscribe to the issue if you'd like to be notified when it's done.

          Thanks again for sharing your solution and for the feedback!