basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Same shell setup on every server: dotfiles in git

    Keep aliases, your color-coded prompt, and tmux and nano configs in one git repository and link them with symlinks. A new server takes git clone and one script; a change on one machine pulls down to the rest with git pull.

    Why configs drift across servers

    With one server, scp ~/.bash_aliases web2: is enough. With three or four servers, files drift fast: web1 gets a new alias and a prompt color for web3, web2 gets skipped, and db1 still has last year’s copy. A month later you can’t tell which host holds the latest file, and spinning up a new server means hunting down which configs to copy from where.

    One ~/dotfiles repository solves both problems. The files live in ~/dotfiles, and symlinks in your home directory point to them (Symlinks and hardlinks). The prompt block from Bash prompt is already built to be shared across servers: it picks the hostname color from ${HOSTNAME%%.*}.

    What goes into dotfiles and what stays on the server

    Put in the repo Keep out of the repo
    ~/.bash_aliases — aliases, functions, and prompt (Everyday aliases, Bash prompt) private SSH keys (~/.ssh/id_*), tokens, .env (Server secrets in git)
    ~/.bash_completion — Tab completion for aliases (Tab after an alias) ~/.bash_history — session history, which catches one-off commands and secrets
    ~/.inputrc — Readline settings all of ~/.bashrc: Ubuntu 24.04 and 26.04 ship different versions, and .bash_aliases is sourced already
    ~/.nanorc, ~/.vimrc, ~/.tmux.conf — editors and tmux history settings inside ~/.bashrc (HISTSIZE, HISTFILESIZE — Command history)

    If one specific server needs extra aliases or variables, append three lines to bash_aliases:

    if [ -f ~/.bash_aliases.local ]; then
      . ~/.bash_aliases.local
    fi

    ~/.bash_aliases.local stays on that server alone and never enters git.

    Repository layout and install script

    Inside ~/dotfiles, store files without a leading dot so plain ls shows them without -a. install.sh adds the dot when it creates the symlinks in ~:

    ~/dotfiles/
    ├── bash_aliases      ◄── ~/.bash_aliases
    ├── bash_completion   ◄── ~/.bash_completion
    ├── nanorc            ◄── ~/.nanorc
    ├── tmux.conf         ◄── ~/.tmux.conf
    └── install.sh
    ~/dotfiles/install.sh
    #!/usr/bin/env bash
    set -euo pipefail
    
    DOTFILES_DIR="$(cd "$(dirname "$0")" && pwd)"

    Exits on any error or unset variable. DOTFILES_DIR resolves the absolute path to the script’s own directory, so you can run it from anywhere and every symlink still gets a full path.

    FILES=(bash_aliases bash_completion nanorc tmux.conf)

    List of files to link into your home directory. When you add a new config to the repo, add its name here and re-run install.sh.

    for name in "${FILES[@]}"; do
      src="$DOTFILES_DIR/$name"
      target="$HOME/.$name"
      [ -e "$src" ] || continue
    
      if [ -e "$target" ] && [ ! -L "$target" ]; then
        mv "$target" "$target.bak"
      fi
    
      ln -sfn "$src" "$target"
    done
    Key part

    Skips any file not yet in the repo so it never creates broken symlinks. If a regular file (not a symlink) already exists in ~, moves it to *.bak so old settings aren’t lost. Re-running the script won’t overwrite the backup.

    For root (sudo -i, sudo -s), don’t symlink ~/dotfiles/bash_aliases. A regular user can edit their own file without a password, and that file would then execute as root on the next sudo -i. Give root a separate copy owned by root:root:

    sudo install -o root -g root -m 644 ~/dotfiles/bash_aliases /root/.bash_aliases

    Rolling it out to servers

    1. Assemble the repository

      mkdir -p ~/dotfiles
      cd ~/dotfiles
      git init -b main
      cp ~/.bash_aliases ~/dotfiles/bash_aliases

      If you have ~/.bash_completion, ~/.nanorc, or ~/.tmux.conf, copy them in without the leading dot. Save the script above as ~/dotfiles/install.sh and make it executable:

      chmod +x ~/dotfiles/install.sh
    2. Test on the first machine and push to git

      ~/dotfiles/install.sh
      exec bash

      Check that your aliases and prompt work, then commit and push to a remote repository:

      cd ~/dotfiles
      git add bash_aliases install.sh
      git commit -m "Initial dotfiles"
      git remote add origin [email protected]:example-user/dotfiles.git
      git push -u origin main

      If bash_aliases contains nothing private, you can keep the repo public and clone over HTTPS on new servers with no SSH keys. To hide server names in case, keep it private and use a read-only Deploy Key.

    3. Set up a new server

      git clone https://github.com/example-user/dotfiles.git ~/dotfiles
      ~/dotfiles/install.sh
      exec bash

      If you also want the same aliases and colored prompt under sudo -i:

      sudo install -o root -g root -m 644 ~/dotfiles/bash_aliases /root/.bash_aliases
    4. Update configs across servers

      Make edits on your machine (or one server), commit, and run git push. On every other server:

      git -C ~/dotfiles pull --ff-only
      exec bash

      --ff-only refuses to merge if someone edited a file directly on the server. When you add a new file to the repo, run ~/dotfiles/install.sh after pull to create the new symlink. Whenever bash_aliases changes, refresh root’s copy too.

    Other ways to manage dotfiles

    If… Use
    2–6 files on Linux servers with no extra packages to install ~/dotfiles and install.sh from this guide
    many nested configs under ~/.config/ and you’d rather not maintain a file list GNU Stow (sudo apt install stow, then stow -d ~/dotfiles -t ~ bash)
    templates across Linux, macOS, and Windows plus secret injection chezmoi

    There’s also the bare-repository trick (git init --bare ~/.dotfiles with an alias for git --git-dir=~/.dotfiles --work-tree=~): files live directly in ~ without symlinks. Forget status.showUntrackedFiles no, though, and status dumps your entire home directory; on a fresh server, checkout collides with the default .bashrc and .profile. For servers, ~/dotfiles with symlinks is more predictable.

    If it doesn’t work

    git -C ~/dotfiles pull replies error: Your local changes to the following files would be overwritten by merge

    Cause
    A file was edited directly on the server without committing: through the symlink, the edit landed in ~/dotfiles.
    Fix
    Inspect the diff with git -C ~/dotfiles diff. Commit and push a real change, or discard an accidental one: git -C ~/dotfiles checkout -- bash_aliases.

    After git pull, a new file (tmux.conf) appeared in ~/dotfiles, yet tmux ignores it

    Cause
    git pull updates files inside ~/dotfiles, but doesn’t create the ~/.tmux.conf symlink.
    Fix
    Run ~/dotfiles/install.sh — it creates missing symlinks and leaves existing ones untouched.

    ls -la ~/.bash_aliases shows a red (broken) symlink

    Cause
    The symlink was created with a relative path (ln -s bash_aliases ~/.bash_aliases), which resolves from ~ rather than ~/dotfiles.
    Fix
    Recreate it with an absolute path by running ~/dotfiles/install.sh.

    Under sudo -i, git pull left the old aliases and prompt in place

    Cause
    Root uses its own copy at /root/.bash_aliases, not a symlink to your file. Pointing root at a regular user’s file is unsafe: edits made without root privileges would execute on the next sudo -i.
    Fix
    Update root’s copy: sudo install -o root -g root -m 644 ~/dotfiles/bash_aliases /root/.bash_aliases.

    On a new server, git clone on a private repo asks for a GitHub password

    Cause
    Cloning a private repo over HTTPS needs a token, and copying your main write-enabled SSH key onto every server is unnecessary exposure.
    Fix
    Add a read-only Deploy Key to the dotfiles repo (leave “Allow write access” unchecked) and clone over SSH (Git and SSH keys).

    How to check

    ls -la ~/.bash_aliases
    # lrwxrwxrwx 1 example-user example-user 40 Nov 24 10:15 /home/example-user/.bash_aliases -> /home/example-user/dotfiles/bash_aliases
    git -C ~/dotfiles status
    # On branch main
    # nothing to commit, working tree clean
    bash -lic 'type ports'
    # ports is aliased to `ss -tulpn'

    The symlink points to an absolute path inside ~/dotfiles, the working tree is clean, and a fresh SSH login loads both aliases and prompt.