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?

Shadowsocks を Conoha VPS で構築する

Last updated at Posted at 2025-07-06

参照記事 (丸写し)

以下について,丸写し または 参考にしました。本当に感謝いたします。

はじめに

中国から VPN 接続して自由なインターネットを楽しむため,以下のセットアップを行う。
そのための備忘録を記述する。

  • VPS を容易
    • Conoha VPS を選定。(従量課金が行えるため,1時間単位)
  • Shadowsocks をインストール
  • Google BBR をインストール

VPS の選定

Conoha VPS を選定,従量課金で行えるため,稼働時間よってはおすすめ。他の VPS でももちろんOK。

選定 OS は,ubuntu (筆者が使い慣れているため)。メモリは,1GB 以上しか選べないが使い勝手を優先。構築時のファイヤーフォールは,ICMP, SSH, Web を許可しておく。

ログイン方法

Web コンソールからログインする

Ubuntu 24.04 LST
login: root
Password: <入力文字列は表示されない>

# uname -a
Linux vm-30ef0c62-51 6.8.0-36-generic #36-Ubuntu SMP PREEMPT_DYNAMIC Mon Jun 10 10:49:14 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.2 LTS
Release:        24.04
Codename:       noble

Ubuntu の初期セットアップ

一般ユーザーの追加 と セキュリティ面の強化

root ユーザーを有効にしたままは危険なので,一般ユーザーを追加し sudo 権限を与える。SSH の root ログインも無効化する。

  • apt で 最新にする
  • ホスト名を変更する
  • adduser で,対話式で一般ユーザーを追加
  • gpasswd で,sudo 権限を付与
  • sudo su - で,一般ユーザーにログインし直す
  • sudo passwd -l rootroot ログイン無効化
# apt update
# apt upgrade
# apt dist-upgrade
# hostnamectl set-hostname <new-hostname>
# adduser general
# gpasswd -a general sudo
$ sudo su - general
$ sudo apt update

ssh で入り直し

> ssh general@<ip-address>
$ sudo passwd -l root
$ sudo cp -v /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
$ sudo vim /etc/ssh/sshd_config
PermitRootLogin yes   ## <- をコメントアウト
$ sudo reboot         ## ssh の設定を有効化

自分に好みにする

操作性を向上させるため,自分好みのセットアップにする。

$ sudo update-alternatives --config editor
$ sudo select-editor

Shadowsocks を install する

現在(2025/07/05) apt install shadowsocks-libev非推奨(not recommended), 現在は,rust による実装が進んでいるため,snap から shadowsocks-rust をインストールする。

$ sudo snap install shadowsocks-rust

## サービス(デーモン)の確認
$ snap services shadowsocks-rust

## shadowsocks server を起動
$ sudo snap start --enable shadowsocks-rust.ssserver-daemon

## 状態確認
$ sudo systemctl status snap.shadowsocks-rust.ssserver-daemon.service

## config 作成 (設定は後述)
$ sudo touch /var/snap/shadowsocks-rust/common/etc/shadowsocks-rust/config.json

# shadowssocks server 再起動
$ sudo systemctl restart snap.shadowsocks-rust.ssserver-daemon.service

## ステータス確認
$ sudo systemctl status snap.shadowsocks-rust.ssserver-daemon.service

## ファイヤーフォール で 443 を開放
$ sudo ufw allow 443
$ sudo ufw status verbose

Shadowsocks を安定させるためのコツは,Arch Linux Wiki に記述されているように,

  • ポートは,443 にする
  • TCP Fast Open を有効にする
  • Google BBR を有効にする
  • shadowsocks の速度を上げるために python-gevent をインストールする
    • 今回は,rust 版なので,無関係と思われる。
  • カーネルパラメーターを最適化する

shadowsock-server の Config

snap 経由でインストールした場合,/var/snap/shadowsocks-rust/common/etc/shadowsocks-rust/config.json,それ以外は,/etc/shadowsocks/config.json または /etc/shadowsocks-libev/config.json と思われる。

{
 "server": "0.0.0.0",
 "server_port": 443,
 "local_port": 1080,
 "method": "chacha20-ietf-poly1305",
 "password": "server_password",
 "timeout": 300,
 "fast_open": true,
 "workers": 2
}

BBR を有効にする

有効化の手段は以下の記事をそのまま実行して下さい。

$ mkdir -p repos/google-bbr
$ cd repos/google-bbr
$ curl -OL https://github.com/teddysun/across/raw/master/bbr.sh
$ chmod +x bbr.sh
$ sudo bash bbr.sh
$ sudo reboot
$ sysctl net.ipv4.tcp_available_congestion_control
net.ipv4.tcp_available_congestion_control = reno cubic bbr
$ sysctl net.ipv4.tcp_congestion_control
net.ipv4.tcp_congestion_control = bbr
$ sysctl net.core.default_qdisc
net.core.default_qdisc = fq

MyDNS で DDNS を登録,更新する

DDNS に登録しておくと IP アドレスの変更に対応できるので楽になります。代表的な MyDNS を今回は使いました。定期更新するため,そのスクリプトを cron に登録します。

$ sudo mkdir -pv /root/bin
$ sudo touch /root/bin/update-mydns.sh
$ sudo chmod +x /root/bin/update-mydns.sh
$ sudo vim /root/bin/update-mydns.sh

スクリプトの内容

#!/bin/bash

## MyDNS のユーザー名とパスワード
user_id=<User ID>
password=<Password>

## DDNS, IP の更新
curl -u ${user_id}:${password} https://www.mydns.jp/login.html
curl -u ${user_id}:${password} https://ipv4.mydns.jp/login.html
curl -u ${user_id}:${password} https://ipv6.mydns.jp/login.html

cron にて,更新する

crontab で 定期的にスクリプトを実行するように設定する。

$ sudo crontavb -e
@reboot /root/bin/update-mydns.sh        # 起動時に実行
*/15 * * * * /root/bin/update-mydns.sh   # 15分ごとに実行

カーネルパラメーターを変更する

詳しくないため,Shadowsocks - Advanced configurations を丸写しする。

$ sudo vim /etc/security/limits.conf
* soft nofile 51200
* hard nofile 51200

# for server running in root:
root soft nofile 51200
root hard nofile 51200

$ sudo /etc/sysctl.d/99-sysctl.conf
## 以下を追加
fs.file-max = 51200

net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.core.netdev_max_backlog = 250000
net.core.somaxconn = 4096

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
# net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_mem = 25600 51200 102400
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
net.ipv4.tcp_mtu_probing = 1

設定が反映されるか確認,エラーが出た場合,その行はコメントアウト。

sudo sysctl -p

DNS 設定

高速な DNS に設定する。

$ sudo vi /etc/netplan/50-cloud-init.yaml
$ sudo netplan apply
/etc/resolv.conf
network:
  version: 2
  ethernets:
    eth0:
      match:
        macaddress: ""
      addresses:
      - "192.168.1.1/24"
      - "<IPv6 Address>"
      nameservers:
        addresses:
         - 1.1.1.1 (追加)
         - 1.0.0.1 (追加)

NTP 設定

推奨される NTP に設定する。
筆者の環境では,systemd-timesyncd がインストールされていなかったのでそこからセットアップする。

$ sudo apt install systemd-timesyncd
$ sudo cp -v /etc/systemd/timesyncd.conf /etc/systemd/timesyncd.conf.orig
$ sudo vim /etc/systemd/timesyncd.conf
$ systemctl restart systemd-timesyncd
$ systemctl status systemd-timesyncd
/etc/systemd/timesyncd.conf
[Time]
### Nict, 事前申請なしで使用する
## URL: https://qiita.com/keigo_kabutomori/items/945b543cbe2792dd8d80
##
NTP=ntp.nict.jp
PollIntervalSec=512
FallbackNTP=ntp1.jst.mfeed.ad.jp ntp2.jst.mfeed.ad.jp ntp3.jst.mfeed.ad.jp
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?