Linuxで簡単にポートを変更(リダイレクト)しよう

2019年10月11日サーバー

こんにちは、水珈琲(@mizucoffee)です。

今回は、Unix/Linuxで簡単にポートリダイレクト出来るコマンドを紹介します!

Sponsored Links

redirコマンドを使おう

例えば、サーバのプログラムは標準ユーザで実行したいけど、80番ポートで公開したい。とか思うことありますよね。Unix系の制約として、ウェルノンポートは標準ユーザではバインド出来ない事になっています。

ケーパビリティ機能を使って割り当てるのが正しいけど面倒くさい...

iptables等を使ってリダイレクトする事も出来るけど、これも結構面倒くさい...

そんな時にはredirコマンドがおすすめです!

インストールしよう

redirコマンドのGitHubリポジトリはこちらです。

apt、yum、brew等大体のリポジトリには入っているようです。ArchLinuxはAURに入っていました。

$ apt install redir
$ yum install redir
$ brew install redir
$ yay -S redir

こんな感じでインストールしましょう。

環境によって入るバージョンが違います。バージョンによってコマンドが違うので注意して下さい。

使い方(v3)

コマンドのヘルプはこんな感じです。

$ redir
Usage: redir [-hinspv] [-b IP]  [-f TYPE] [-I NAME] [-l LEVEL] [-t SEC]
                       [-x STR] [-m BPS] [-o FLAG] [-w MSEC] [-z BYTES]
                       [SRC]:PORT [DST]:PORT
Options:
  -b, --bind=IP            Force specific IP to bind() to when listening for
                           incoming connections.  Not applicable with -p
  -f, --ftp=TYPE           Redirect FTP connections.  Where type is
                           one of: 'port', 'pasv', or 'both'
  -h, --help               Show this help text
  -i, --inetd              Run from inetd, SRC:PORT comes from stdin
                           Usage: redir [OPTIONS] [DST]:PORT
  -I, --ident=NAME         Identity, tag syslog messages with NAME
                           Also used as service name for TCP wrappers
  -l, --loglevel=LEVEL     Set log level: none, err, notice*, info, debug
  -n, --foreground         Run in foreground, do not detach from terminal
  -p, --transproxy         Run in Linux's transparent proxy mode
  -s, --syslog             Log messages to syslog
  -t, --timeout=SEC        Set timeout to SEC seconds, default off (0)
  -v, --version            Show program version
  -x, --connect=STR        CONNECT string passed to proxy server
Traffic Shaping:
  -m, --max-bandwidth=BPS  Limit the bandwidth to BPS bits/second
  -o, --wait-in-out=FLAG   Wait for in(1), out(2), or in&out(3)
  -w, --random-wait=MSEC   Wait MSEC milliseconds before each packet
  -z, --bufsize=BYTES      Size of the traffic shaping buffer
SRC and DST are optional, redir will revert to use 0.0.0.0 (ANY)

使い方は以下のとおりです。

$ redir :<待ち受けるポート> 127.0.0.1:<転送先のポート>

では試しにpython3でサーバーを立ててポートリダイレクトをしてみましょう。

$ python -m http.server 8000
$ sudo redir :80 127.0.0.1:8000

このようにすると、80番ポートでアクセスできるようになります!

使い方(v2)

コマンドのヘルプはこんな感じです。

$ redir
usage:
	redir --lport=<n> --cport=<n> [options]
	redir --inetd --cport=<n>
	Options are:-
		--lport=<n>		port to listen on
		--laddr=IP		address of interface to listen on
		--cport=<n>		port to connect to
		--caddr=<host>		remote host to connect to
		--inetd		run from inetd
		--debug		output debugging info
		--timeout=<n>	set timeout to n seconds
		--syslog	log messages to syslog
		--name=<str>	tag syslog messages with 'str'
		--connect=<str>	CONNECT string passed to proxy server
		--bind_addr=IP	bind() outgoing IP to given addr
		--ftp=<type>		redirect ftp connections
			where type is either port, pasv, both
		--transproxy	run in linux's transparent proxy mode
		--bufsize=<octets>	size of the buffer
		--maxbandwidth=<bit-per-sec>	limit the bandwidth
		--random_wait=<millisec>	wait before each packet
		--wait_in_out=<flag>	1 wait for in, 2 out, 3 in&out
	Version 2.2.1.

使い方は以下の通りです。

$ redir --lport=<待ち受けるポート> --cport=<転送先のポート>

では試しにpython3でサーバーを立ててポートリダイレクトをしてみましょう。

$ python -m http.server 8000
$ sudo redir --lport=80 --cport=8000

このようにすると、80番ポートでアクセスできるようになります!

まとめ

今回はredirコマンドを紹介しました。

検証でポートを変えたい時など、意外と便利なので是非使ってみてください!

ちなみにUDPポートリダイレクト用のuredirというコマンドもあるので必要に応じて使い分けてみてください。