はじめに
Heroku CLI のセットアップ中に作成された ~/.netrc というファイルについて。
これは一体どういったものなのか、いつ使われるものなのかを調べてみました。
検証環境
1
2
3
4
5
6
7
8
| $ uname -moi
arm64 unknown Darwin
$ bash -version | head -n 1
GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
$ docker --version
Docker version 20.10.21, build baeda1f
|
Man ページを参照
以下のページを見つけました。
netrc, .netrc - ftp のためのユーザー設定ファイル
…
このファイルには、ファイル転送プロトコル ( File Transfer Protocol ) クライアント ftp ( 1 ) のための、設定情報と自動ログイン情報が書かれている。
FTP サーバに接続する際の認証情報を保存しておくファイルということです。
curl 関連の情報を調べているときにも .netrc の名前を見た記憶があるので、登場背景の FTP 以外でも使われるようになっているということでしょうか。
~/.netrc の有無による動作の違いを確認
検証準備 ( FTP サーバコンテナを起動 )
FTP サーバを構築し、接続時に ~/.netrc が利用されるかを確認してみます。
FTP サーバはローカル PC 上の Docker 環境に構築することとしました。
以下の Docker イメージを利用させていただきました。
接続ユーザを用意するためにはコンテナ起動時に以下の環境変数を設定すればよいと書かれています。
FTP_USER_NAME : 接続ユーザFTP_USER_PASS : 接続パスワードFTP_USER_HOME : 接続ユーザのルートディレクトリ
ここではそれぞれ、 genzouw / passpass / /home/ftpusers/genzouw を設定しました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # デーモン(バックグラウンド)起動
$ docker run \
-d \
--name ftpd_server \
-p 21:21 \
-p 30000-30009:30000-30009 \
-e "PUBLICHOST=localhost" \
-e "FTP_USER_NAME=genzouw" \
-e "FTP_USER_PASS=passpass" \
-e "FTP_USER_HOME=/home/ftpusers/genzouw" \
stilliard/pure-ftpd
# 実行されているかログを監視
$ docker logs -f ftpd_server
Creating user...
Password:
Enter it again:
root user give /home/ftpusers/genzouw directory ftpuser owner
Setting default port range to: 30000:30009
Setting default max clients to: 5
Setting default max connections per ip to: 5
Starting Pure-FTPd:
pure-ftpd -l puredb:/etc/pure-ftpd/pureftpd.pdb -E -j -R -P localhost -p 30000:30009 -c 5 -C 5
|
無事起動できました。
~/.netrc がないとき
起動した FTP コンテナに接続してみます。
ftp localhost コマンドを実行します。
~/.netrc ファイルが存在しないときは接続のためにユーザ名、パスワードの入力を求められます。
ここではコンテナ起動時に設定した genzouw / passpss を指定します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| $ ftp localhost
Connected to localhost.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 5 allowed.
220-Local time is now 23:39. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
Name (localhost:root): genzouw
331 User genzouw OK. Password required
Password:
230 OK. Current directory is /
Remote system type is UNIX.
Using binary mode to transfer files.
|
問題なく接続できました。
~/.netrc があるとき
今度は ~/.netrcファイルを用意した状態で接続してみます。
ここでは細かい記法に触れませんが、設定項目は以下の3つです。
machine = 接続サーバーlogin = 接続ユーザpassword = 接続パスワード
1
2
3
4
5
6
7
8
9
| # 設定を ~/.netrc に追記
$ cat <<EOF >>~/.netrc
machine localhost
login genzouw
password passpass
EOF
# *** 設定後は必ずパーミッションを変更しておくこと! ***
$ chmod 600 ~/.netrc
|
接続してみます。
1
2
3
4
5
6
7
8
9
10
11
12
| $ ftp localhost
Connected to localhost.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 5 allowed.
220-Local time is now 23:38. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
331 User genzouw OK. Password required
230 OK. Current directory is /
Remote system type is UNIX.
Using binary mode to transfer files.
|
今度はユーザ、パスワードの入力は求められませんでした。
何度も接続するFTPサーバの場合には、入力の手間を減らすことができます。
注意
便利な機能ではありますが、 .netrc に平分でパスワードを記載しておくのはセキュリティの観点から言って不安が残ります。
GPGを使ってパスワードを暗号化するという方法をとることがあるそうですが、Herokuでは node-netrc-parser というツールを使ってパスワードの項目だけ暗号化しているようです。