LoginSignup
0
0

More than 5 years have passed since last update.

さくらVPS(Ubuntu16.04)で動いているbitcoindに対して、JSON-RPCコマンドの受付を有効にする

Last updated at Posted at 2018-05-15

はじめに:セキュリティに関わる設定を変更するので自己責任でお願いします。

外部からJSON-RPCコマンドでgetblockchaininfoメソッドなどの情報を取得したいのに
"No route to host(ホストへの経路がありません)"となっておりました。
もちろんpingは通っております。
ただtelnetは同様のエラーになっていました。

結論からいうとfirewall(iptables)の設定と、
bitcoin.confのrpcallowipによって外部からはコマンドが叩けなかった。

iptablesでポートを解放する

まずBitcoin RPCPortのポートをiptablesのルールが記載されたファイルに追加します。
sudo vi /etc/iptables/iptables.rules
sshのINPUTをACCEPTしている箇所があると思うので、
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
この下に以下の1行を追加します。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8332 -j ACCEPT

現在のsshしか許可されていない状態をまず確認します。
sudo iptables -L

ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere 
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

では変更を反映するためにiptablesのserviceをrestartします。
sudo /etc/network/if-pre-up.d/iptables restart

それでは反映されたか確認してみましょう。
sudo iptables -L

ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:8332
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

最後のREJECTの前に追加されていることがポイントです。

bitcoin.confの設定を行う

~/.bitcoin/bitcoin.conf

rpcuser=myuser
rpcpassword=mypass
rpcallowip=0.0.0.0/0 #全てのipaddrからの接続を許可する場合の設定

を記載の上で、bitcoindを立ち上げます。

外部からcurlでJSON-RPCコマンドを試してみる

これで外部からcurlなどで情報が取得できるようになったと思います。

curl --data-binary '{"jsonrpc":"1.0","id":"curltext","method":"getblockchaininfo","params":[]}' -H 'content-type:text/plain;' http://myuser:mypass@myipaddress(domainname):8332

localhostでは取得できるのに〜という方に。
以上、備忘録的に記載させていただきましたが自己責任で宜しくおねがいします。

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