====== OhMyZsh: re-use setup as root user ======
===== The problem =====
You have installed [[https://ohmyz.sh/|oh-my-zsh]] as a normal user, but as soon as you log in as ''root'', you don't have oh-my-zsh available. There are probably several solutions to this problem (installing OhMyZsh separately as root, where you may end up with a conflicting environment variable when switching between your normal user account and root account), but I will now focus on a straightforward solution which gives you a copy of the whole OhMyZsh installation, without git (so without updates), which I think is the most straightforward and sufficient solution.
===== The solution =====
This is a step by step tutorial. In this example the normal (non-root) user is called ''normal''. I also suppose you don't have to use ''sudo'' for all admin tasks (which is normally the case in Ubuntu and its derivates), but you have a separate root password. In case you don't have a root password you can simply put the ''sudo'' command in front of every command which I list here, but it would make more sense to first create a root password (which is easy, you can find a solution elsewhere).
Make sure that your root login shell is really zsh, not bash or something. Log in as root and check the name of the active shell:
echo $0
This should print zsh, not any other shell.
In case it's not zsh, then set the default shell with command:
chsh
and enter ''/bin/zsh'' as your new login shell and log out and log in again.
Now to the main part.\\
Copy file ''.zshrc'' from folder ''/home/normal'' to folder ''/root''
Also copy the whole folder ''/home/normal/.oh-my-zsh/'' to ''/root''\\
but don't copy over the git files and directories.
**A list of all the git stuff:**
.git/
.github/
.gitignore
.gitpod.Dockerfile
.gitpod.yml
Now log out and log in again as root. You should see an error similar to the following:
[oh-my-zsh] Insecure completion-dependent directories detected:
drwxr-xr-x 1 normal normal 512 Aug 15 18:26 /root/.oh-my-zsh/plugins
drwxr-xr-x 1 normal normal 512 Aug 15 18:26 /root/.oh-my-zsh/plugins/colored-man-pages
drwxr-xr-x 1 normal normal 512 Aug 15 18:26 /root/.oh-my-zsh/plugins/copyfile
drwxr-xr-x 1 normal normal 512 Aug 15 18:26 /root/.oh-my-zsh/plugins/copypath
drwxr-xr-x 1 normal normal 512 Aug 15 18:26 /root/.oh-my-zsh/plugins/encode64
drwxr-xr-x 1 normal normal 512 Aug 15 18:26 /root/.oh-my-zsh/plugins/extract
-rw-r--r-- 1 normal normal 356 Aug 15 18:26 /root/.oh-my-zsh/plugins/extract/_extract
[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.
[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh] compaudit | xargs chmod g-w,o-w
[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.
However the problem does not have to do with write persmissions for group and others. I didn't want to skip verification through ZSH_DISABLE_COMPFIX on purpose, so let's fix it another way. First make sure you are in the root home directory:
''cd /root'' (or simply ''cd'')
Then set "rw" permission of ''.zshrc'' only to root and also change file ownership:
chmod 600 .zshrc && chown root .zshrc
Then recursively change the owner of directory ''.oh-my-zsh/'' and all subdirectories and all its files to root:
chown -R root .oh-my-zsh/
That's it! Now when you log in again as root, you won't see any errors or warnings by OhMyZsh and you will get the same user experience as the "normal" user from whom you've taken the OhMyZsh installation and settings.
==== Here is the main part of a .zshrc file which can be used for both normal user and root ====
export PATH="$HOME/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\
/usr/games:/usr/local/games"
export EDITOR='nano'
export HISTSIZE=200
export SAVEHIST=3000
export ZSH_CACHE_DIR="$HOME/.cache/ohmyzsh"
# Reloads Zsh config (running source ~/.zshrc is deprecated)
alias reload="omz reload"
alias editzshrc="nano ~/.zshrc"
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="candy"
HIST_STAMPS="yyyy-mm-dd"
zstyle ':omz:update' mode disabled # disable automatic updates
plugins=(copyfile copypath encode64 extract)
source $ZSH/oh-my-zsh.sh
export LANG='en_GB.UTF-8'
==== Alternative solution ====
A completely different alternative solution would be to switch to root with ''sudo -Es'' command. This will start zsh as root with your regular rc files and $HOME will point to your regular home directory. However you will always see the warning "Insecure completion-dependent directories detected", so to get rid of it you could skip the verification of insecure directories by setting the variable ''ZSH_DISABLE_COMPFIX'' to "true" before oh-my-zsh is sourced in your ''.zshrc'' file.
Article date: 2022-09-09