What this guide covers
When you finish, the terminal prompt shows which machine you are on, PATH is correct in both interactive and background use, and a copy of your config sits in your own repository. You need: basic terminal skills.
Steps
Confirm the default shell and do not bother replacing it
echo $SHELLshould print/bin/zsh, the macOS default. Keep it. The zsh that ships with the system is maintained through system updates and does not depend on anything you install. If you switch the login shell to the Homebrew version and Homebrew then breaks, you cannot open a working terminal on the remote machine at all, and sorting that out is far more trouble than on your own computer.What each of the two files is for
~/.zprofileis read once when a login shell starts. It suits environment settings such as PATH, and the Homebrew shellenv line goes here.~/.zshrcis read every time an interactive terminal opens. It suits aliases, the prompt, completion and version manager initialization. The system does not create either file for you by default, sotouchone if it is missing.Remember: background jobs do not read .zshrc
Scheduled jobs, jobs that start at login and scripts launched by other programs are not interactive terminals and do not read
~/.zshrc. On a remote machine, unattended jobs like these are the bulk of the work. So put any PATH settings that background jobs also need in~/.zprofileand have the jobs start throughzsh -lc, or simply write full command paths in the job. Also avoid putting commands in.zshrcthat print text or wait for input, because they interfere with whatever is calling the shell.Make the prompt say this is the remote machine
When the local and remote terminals look the same, it is easy to type a delete or push command in the wrong place. Add a line to
~/.zshrcthat shows the username and hostname and sets them apart with color:PROMPT='%F{yellow}[remote]%f %n@%m %1~ %# '%nis the username,%mis the hostname, and%1~is the name of the current directory. Do not change the system's computer name for the sake of appearance; the platform uses the machine name to identify the device.History: long enough, but with no secrets in it
On a remote machine you come back only every few days, so the history is your working notes. Make it larger and share it across terminals:
HISTSIZE=50000 SAVEHIST=50000 setopt SHARE_HISTORY HIST_IGNORE_SPACEHIST_IGNORE_SPACEmeans a command typed with a leading space is not recorded in history, which helps when a command contains a secret. Better still, do not type secrets on the command line at all.Keep plugins and themes minimal
Prompt themes and plugin frameworks pull scripts from the internet, and those scripts run every time a terminal opens. Install only ones whose source is clear and whose behavior you understand. If startup becomes noticeably slow, measure it with
time zsh -lic exitand comment items out one at a time to find the cause. A fancy prompt has an extra cost in a remote desktop: character art and a frequently refreshing status bar add on-screen changes.Put the config in your own repository
After the rental ends the machine is reclaimed and reinstalled, and the
.zshrc,.zprofile,.tmux.confand.gitconfigyou tuned all disappear. Create a private config repository and put them in it. On the next machine, clone it and create a few symlinks, and you are back where you were. Before committing, check that no secrets or tokens are hard-coded in the files.
FAQ
How do I apply the config after changing it?
source ~/.zshrc reloads it in the current terminal, or just open a new window. If you changed ~/.zprofile, a new window is the safer choice. Old shells inside tmux sessions that are already running do not update automatically.
I broke .zshrc and the terminal shows an error as soon as it opens. What now?
Open the .zshrc in your user directory with Finder or a text editor (press Command+Shift+. in Finder to show hidden files) and fix the offending line. This only affects your own account, and the machine does not need a restart.
Can I change the global shell config under /etc?
There is no need. Your account-level config files already cover everything you need. Leave the system-level files to the system and the platform, in the state they were delivered in.
Related
- How to install Homebrew on a rented remote Mac mini and keep it maintained
- How to manage Node versions with nvm or fnm on a remote Mac mini
- How to store env vars and API keys for multiple projects on a remote Mac mini
- How to schedule and auto-start jobs with user-level LaunchAgents on a remote Mac mini
- What to do on day one with a rented remote Mac mini
Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support