0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

visudoでnanoを使用する方法(環境変数だけではうまく行かない人向け)

Last updated at Posted at 2025-03-11

背景

私は普段、 viの操作を覚えるのが面倒なので テキストエディタにはシンプルに操作できるnanoを使用している。
しかしvisudoにおいてはデフォルトでviが使われてしまうため、これをnanoに変更したい。
google検索で出てくる方法だけでは解決しなかったので、記事として残すことにする。

課題

sudo EDITOR=nano visudo と明示的に指定すればnanoが起動するものの、毎回指定するのが手間。
調べると、環境変数の変更やupdate-alternativesの設定を変更するといった方法が出てくるが、これらでは解決ができなかった。

以下事前に試していたこと

  • ユーザー、rootそれぞれの.bashrcに以下を追記し、sourceコマンドで実行
    • export SUDO_EDITOR=nano
    • export EDITOR=nano
  • 以下のコマンドを実行し、editorコマンドでnanoが開くように変更
$ sudo update-alternatives --install /usr/bin/editor editor bin/nano 100
$ sudo update-alternatives --config editor
There is 1 program that provides 'editor'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/bin/nano

Enter to keep the current selection[+], or type selection number: 

解決方法

ChatGPT o3-miniに質問したら一発で正しい回答が得られた。
visudoの起動オプション(EDITOR=nanoなど)は、visudoの設定ファイルである/etc/sudoers内でデフォルト値を指定できる模様。

  1. 一時的にnanoでvisudoを起動する

    sudo EDITOR=nano visudo
    
  2. sudoersファイルにエディタの指定を追加する
    visudoで開いたファイル内に、以下の行を追加。

    Defaults editor=/usr/bin/nano
    

    ※nanoのパスは通常/usr/bin/nanoだと思われるが、念の為which nanoで確認しておくことを推奨。

  3. 設定の保存と終了
    変更を保存して終了すれば、今後は単に以下のコマンドを実行するだけでnanoが起動するはず。

    sudo visudo
    

うまく行かない場合、冒頭に述べた環境変数の設定やupdate-alternativesの設定も合わせて試すと良い。

参考文献

余談だが、google検索するとnanoからviに変える記事ばかりが引っかかった。
nanoは少数派なのかなぁ…

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?