If you have any reason to want to connect to your Raspbery Pi when you're away from home, you're going to want a dynamic DNS address. A dynamic DNS address lets you use a constant address like raspberry.johnsmith.com instead of an ever-changing IP address. This is useful if you've set up a VPN server on your Raspberry Pi or just like to tinker over SSH while you're away.
This tutorial hinges on the assumption that you have a domain name registered with Google Domains. If you have a domain with another registrar, then you should either move your domains to Google, or find another tutorial for dynamic DNS on a Raspberry Pi.
This tutorial lets you use a subdomain of one of your domains to point to your raspberry pi. After forwarding the correct ports from your router you your Raspberry Pi, you'd then be able to access your Raspberry Pi from anywhere in the world.
The first step is to go to the DNS records for the domain you want to use for your dynamic DNS address. There, scroll down to the Synthetic records section and add a Dynamic DNS entry. For me, my dynamic DNS entry is "raspi.kurttomlinson.com".
Next, expand the Dynamic DNS section of that web page and click "View credentials". This will show you a randomly generated username and password that you'll need later.
Next, ssh into your raspberry pi and install ddclient: sudo apt-get install ddclient. Once it's installed, a few prompts will pop up. It doesn't really matter what you enter into the prompts. The prompts are used to created a configuration file for you, but we're going to replace that configuration file anyway.
sudo apt-get install ddclient
To edit ddclient's configuration file, enter sudo nano /etc/ddclient.conf. Make the configuration file look like the image, but replace the username and password with the username and password you got from Google Domains earlier. Replace the last line with the dynamic domain you created earlier on Google Domains. Here's the content of the config file for your convenience:
sudo nano /etc/ddclient.conf
# Configuration file for ddclient generated by debconf
Finally, make sure that ddclient gets run at boot. You can add ddclient to your rc.local file to do this.
Photo by The Preiser Project