Configuring Static IP on Raspberry Pi using `nmcli`

To set a static IP address on your Raspberry Pi via the command line, use the NetworkManager command-line interface tool `nmcli`.

ifconfig # Get the IP first
 
# To enable SSH
sudo systemctl start ssh
sudo systemctl enable ssh
sudo vi /etc/ssh/sshd_config  # set password to no
 
sudo /etc/init.d/ssh restart
 
nmcli connection show
nmcli connection show "<connection-name>"
nmcli connection modify "<connection-name>" \
    ipv4.addresses 192.168.1.13/24 \
    ipv4.gateway 192.168.1.1 \
    ipv4.dns "8.8.8.8,8.8.4.4" \
    ipv4.method manual \
    autoconnect yes
 
nmcli connection modify "<connection-name>" \
    ipv4.addresses 192.168.1.13/24 \
    ipv4.gateway 192.168.1.1 \
    ipv4.dns "8.8.8.8,8.8.4.4" \
    ipv4.method manual \
    autoconnect yes
 
nmcli connection down "<connection-name>" && nmcli connection up "<connection-name>"
sudo service docker restart

To apply the changes and restart the network interface, run:

nmcli connection down "<connection-name>" && nmcli connection up "<connection-name>"

Replace <connection-name> with the actual network connection name you wish to configure.

Reverting to DHCP on Raspberry Pi using `nmcli`

To change the configuration from a static IP to DHCP on your Raspberry Pi, use the following `nmcli` commands:

nmcli connection modify "<connection-name>" ipv4.method auto
nmcli connection modify "<connection-name>" ipv4.addresses ""
nmcli connection modify "<connection-name>" ipv4.gateway ""
nmcli connection modify "<connection-name>" ipv4.dns ""

Reactivate the networking connection to apply the new settings:

nmcli connection down "<connection-name>" && nmcli connection up "<connection-name>"

For more details about `nmcli`, visit the official documentation.

SSH Port Forwarding and Remote Access

To access your Raspberry Pi via SSH remotely:

  1. Configure your router to forward an external port (e.g., 22000) to the internal SSH port of your Raspberry Pi (port 22).
  2. Connect via SSH using the command:
ssh pi@<Your-External-IP-Address> -p <External-Port>

Replace <Your-External-IP-Address> with your public IP and <External-Port> with the port you chose for external access.