You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Lando将zsh设置为默认Shell?

解决Lando Docker镜像中zsh无法设为默认Shell的问题

问题描述

在为Lando构建的Docker镜像中已安装zsh和oh-my-zsh,安装oh-my-zsh时也使用了CHSH=yes参数,但执行lando ssh进入容器后,默认Shell仍是/bin/bash。手动输入zsh可正常进入zshell且.zshrc配置生效,但每次登录都需要手动切换。

已尝试的方法:

  • 创建.profile文件:
export SHELL=`which zsh`
[ -z "$ZSH_VERSION" ] && exec "$SHELL" -l
  • 创建.bash_profile文件:
export SHELL=/bin/zsh
exec /bin/zsh -l

解决方案

1. 直接修改用户默认Shell(核心解决方法)

问题根源是CHSH=yes在非交互式环境下执行时,可能未成功修改/etc/passwd中用户的Shell配置。需在Dockerfile中以root身份显式修改www-data用户的默认Shell:

在Dockerfile末尾的USER root之后添加以下命令:

# 修改www-data用户的默认Shell为zsh
RUN chsh -s $(which zsh) www-data

2. 验证Shell配置(可选)

可在Dockerfile中添加验证命令,确认修改是否生效:

RUN cat /etc/passwd | grep www-data

执行后应输出类似内容:www-data:x:33:33:www-data:/var/www:/usr/bin/zsh,说明Shell已修改为zsh。

3. Lando配置兜底(可选)

若上述方法仍不生效,可在项目的.lando.yml中指定登录Shell:

services:
  appserver:
    overrides:
      environment:
        SHELL: /usr/bin/zsh
    ssh:
      - "/usr/bin/zsh"

修改后的完整Dockerfile示例

FROM devwithlando/php:8.3-fpm-4

# Upgrade system
RUN apt-get update && apt-get -y upgrade

# Install system tools
RUN apt-get install -y tree nano

# Install locales
RUN apt-get install -y locales locales-all

# Add PHP extension helper
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

# Install PHP extensions
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
  install-php-extensions sqlsrv pdo_sqlsrv

# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
RUN apt-get install -y nodejs

# Instal/update NPM
RUN npm install npm --global

# Install build dependencies for screen
RUN apt-get install -y build-essential libncurses5-dev automake texinfo wget \
    git libtool pkg-config m4 perl

# Install newer Autoconf version
RUN wget https://ftp.gnu.org/gnu/autoconf/autoconf-2.71.tar.gz && \
    tar -xzf autoconf-2.71.tar.gz && \
    cd autoconf-2.71 && \
    ./configure && \
    make && \
    make install && \
    cd .. && \
    rm -rf autoconf-2.71 autoconf-2.71.tar.gz

# Download, compile and install screen 5.0.0
RUN wget https://ftp.gnu.org/gnu/screen/screen-5.0.0.tar.gz && \
    tar -xzf screen-5.0.0.tar.gz && \
    cd screen-5.0.0 && \
    ./autogen.sh && \
    ./configure && \
    make && \
    make install && \
    cd .. && \
    rm -rf screen-5.0.0 screen-5.0.0.tar.gz

# Install zsh and ohmyzsh
RUN apt-get install -y zsh
USER www-data

RUN cd /var/www && \
    wget https://install.ohmyz.sh -O install.sh && \
    CHSH=yes RUNZSH=no KEEP_ZSHRC=no sh install.sh && \
    rm install.sh

# Create default .zshrc in home directory
RUN echo 'export ZSH="$HOME/.oh-my-zsh"\n\
ZSH_THEME="crunch"\n\
plugins=(git laravel)\n\
source $ZSH/oh-my-zsh.sh\n' > ~/.zshrc

USER root
# 修改www-data用户的默认Shell为zsh
RUN chsh -s $(which zsh) www-data

内容的提问来源于stack exchange,提问作者SlimDeluxe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 12:04:53