[英]installing oh-my-zsh for a different user as root in cloud-init script
我正在尝试使用为 ubuntu 用户安装的 oh-my-zsh 来引导我的 AWS EC2 ubuntu 服务器。 我有一个以 root 用户身份(使用 sudo)运行的 cloud-init 脚本(更多信息在这里)。 因此,在我的脚本中,我以 ubuntu 用户身份运行 oh-my-zsh 安装。
#cloud-config
runcmd:
# omitted other commands specific to my server, install zsh at the end
- apt-get install -y zsh
- su ubuntu -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/coreycole/oh-my-zsh/master/tools/install.sh)"'
- chsh -s $(which zsh) ubuntu
# change the prompt to include the server hostname
- su ubuntu -c echo "echo export PROMPT=\''%{$fg[green]%}%n@%{$fg[green]%}%m%{$reset_color%} ${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'\'" >> /home/ubuntu/.zshrc
# get environment variables defined above
- echo "source ~/.profile" >> /home/ubuntu/.zshrc
当 cloud-init 完成并且我 ssh 进入颜色在$PROMPT
不起作用时,我看到[green]
和[cyan]
:
[green]ubuntu@[green]ip-172-31-27-24 [cyan]~
如果我在 ssh 进入后运行与 ubuntu 用户相同的PROMPT
命令,颜色可以正常工作:
问题似乎是当 cloud-init 脚本运行echo
命令时颜色是如何计算的,而当 ubuntu 用户运行echo
命令时颜色是如何计算的。 有谁知道我如何更改PROMPT
以便颜色仅在 ubuntu 用户评估~/.zshrc
.zshrc 时评估一次?
我解决了这个感谢jgshawkey的答案在这里。 我使用 bash 变量来转义颜色代码和命令以推迟它们的评估:
- apt-get install -y zsh
- runuser -l ubuntu -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/coreycole/oh-my-zsh/master/tools/install.sh)"'
- chsh -s $(which zsh) ubuntu
- fgGreen='%{$fg[green]%}'
- fgCyan='%{$fg[cyan]%}'
- fgReset='%{$reset_color%}'
- retStatus='${ret_status}'
- gitInfo='$(git_prompt_info)'
- runuser -l ubuntu -c "echo export PROMPT=\''${fgGreen}%n@%m${fgReset} ${retStatus} ${fgCyan}%c${fgReset} ${gitInfo}'\'" >> /home/ubuntu/.zshrc
- echo "source ~/.profile" >> /home/ubuntu/.zshrc
它最终在我的~/.zshrc
看起来像这样:
由于我正在创建一个新的用户/服务器,所以我是这样做的:
user=you user here
pass=you password here
apt install -y zsh curl wget # considering you currently are root
#creates a new user with password and zsh as default shell
useradd "$user" -m -p $(openssl passwd -1 "$pass") -s $(which zsh)
usermod -aG sudo "$user" # append to sudo and user group (optional)
# mind the command above, with the parameter --unattended which means "no questions" and "no set default to zsh"
runuser -l $user -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended'
对我来说效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.