0
1

【備忘録】Ubuntu 18.04.6 LTSにてシェル変更とAnaconda環境設定のトラブルシューティング

Last updated at Posted at 2024-06-11

概要

Ubuntuシステムにおけるデフォルトシェルの変更とAnaconda環境の設定に関するトラブルシューティングの過程を詳述します。このドキュメントは、同様の問題に直面した際に役立つことを目的としています。

環境

  • 開発端末: Windows 10 Pro 64bit デスクトップPC
  • リモートサーバー: Ubuntu 18.04.6 LTS
  • 使用シェル: tcsh → bash
  • ソフトウェア: Anaconda 3
  • 開発ツール: Visual Studio Code (VSCode)
  • 拡張機能: Remote - SSH

接続方法

  1. Windows PCからの接続
    • 使用ツール: Visual Studio CodeのRemote - SSH拡張機能を使用して、Windows PCからUbuntuサーバーにリモート接続。
    • 手順:
      1. VSCodeを開く。
      2. Remote - SSH拡張機能をインストール。
      3. VSCodeのコマンドパレットで「Remote-SSH: Connect to Host...」を選択し、リモートサーバーのIPアドレスを入力。
      4. 接続が確立されると、VSCode内でリモートサーバーのファイルを編集できるようになる。

問題

  • ~背景~
    ユーザーはシェルを/bin/tcshから/bin/bashに変更したかった…
    → しかし
  1. シェルの変更がうまくいかない
  2. Anacondaの複数のインストール場所によるPATHの重複

という問題が発生した。

シェルの変更でのトラブル

問題点

/etc/passwd ファイルで user:x:1000:1000:user:/home/user:/bin/bash に設定されているのに、ログイン後にシェルが bash に変更されない。

解決方法

  1. シェルの変更コマンドを実行:
    $ chsh -s /bin/bash
    
  2. システムを再起動:
    $ sudo reboot
    
  3. 再ログイン後、シェルの確認:
    $ echo $SHELL
    # /bin/bash と表示されるはずです。
    

Anacondaの設定

問題点

Anacondaが複数の場所にインストールされており、PATHが重複している。

解決方法

  1. 不要なAnacondaディレクトリを削除:
    $ rm -rf /home/user/anaconda3
    
  2. .bashrc ファイルの修正:
    $ nano ~/.bashrc
    

3.以下の内容で/home/user/.bashrcに保存する:
```bash
# ~/.bashrc: executed by bash(1) for non-login shells.

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    alias dir='dir --color=auto'
    alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history | tail -n1 | sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

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

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
      . /usr/share/bash-completion/bash_completion
    elif [ -f /etc/bash_completion ]; then
        . /etc/bash_completion
  fi
fi

# To automatically log out after 1800 seconds (30 minutes) of inactivity
# add the following line to your .bashrc file:
export TMOUT=1800

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/opt/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
```

4.Shell使えるかのテストコマンド

$ cat /etc/shells 
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
/usr/bin/tmux
/bin/tcsh
/usr/bin/tcsh

5.以下のコマンドを実行して環境変数を再読み込みします。

$ source ~/.bashrc

!ここでsudo rebootコマンドで再起動するべきだった!

$ sudo reboot

反省点と教訓

対応の反省点

  • シェルの変更には"sudo reboot"コマンドで再起動が必要であることを知らなかった。

手順の反省点

  • 再起動を試みる前に、シェルの変更が反映されていないことを確認する手順が欠けていました。

まとめ

今後、シェルの変更や設定変更を行う際には、変更後の動作確認と必要に応じたシステム再起動を行うことが重要と感じた。なのでトラブルシューティングの過程をドキュメント化し、再発防止に努めることが重要と考えた。

参考リンク

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1