Shin's IT Diary

サーバ、ネットワーク、PCに関してのメモとかを書き残してます。

VPSのSSHコンフィグ変更

皆様お久しぶりです。お元気でしたか?
ブログ主桜坂です。
気がつけば季節ももう秋になり最近はどうも肌寒いですね。
夏が終わるとどうしてもしんみりしがちですが
これから先の冬が若干楽しみなので僕としては
ちょっとだけありがたいです。
冬ってなんかわくわくしますよね。

さてはて、そんなわけで今回はVPSのお話。
前々からレンタルしてみたかったんだけど
やれ時間がない、技術ない、とかで
先延ばしにしまくりだったわけでして…

でも最近ちょっとやりたいことも増えてきてので
「んじゃいい機会だし作ってみよう」
なんて安易な考えでVPS作成してみました。
いや、勉強にもいいし…ね?

今回はそのVPSの初期構築を書くことにしました。
何方かのお役に立てれば幸いです。

構成表

Conoha VPS

CPU SSD メモリ
3コア 50GB 2GB


初期設定

まずはとりあえず画面の指示にしたがってサーバ追加。 そんなに難しいことはないかと。



構成できたらターミナル上からアクセス

今の設定だとrootログインもできちゃうしデフォルトポートなので
このまま使うのはかなり危険ですね。まずはSSHからなんとかしましょう


SSHの設定

基本となるSSHの設定を行いましょう。
まずはポート変更とか云々。

変更前

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# The default requires explicit activation of protocol 1
#Protocol 2

~(省略)~

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

~(省略)~

#PermitEmptyPasswords yes
#PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

Port
待受ポートの設定。デフォルトは22番
ウェルノウンポート以上(1024番)にしておくのが通例らしいです。

AddressFamily
IPv4IPv6どちらかまたはその両方でアクセスさせるときに使用
デフォルトはどっちもOKになってますが使用環境に応じて変更を。

ListenAddreess 0.0.0.0 (::)
ローカルアドレスの待受アドレス。指定したアドレスでSSHを通します。

Protocol 2
SSHで使用するプロトコルを指定します。通常は2を使います。

LoginGraceTime
ユーザがログインに成功するまでの制限時間。デフォルトは2分
この時間内にログイン成功できなければサーバが接続を切ります

PermitRootLogin
ルートログインを許可するかどうかの設定。デフォルトはyes
危険なのでよほどのことがなければnoを設定。(ってかほぼ?)

StrictModes
ログインさせる前に、ユーザホームディレクトリのオーナシップと
パーミッションをチェックするかどうか。
デフォルト値は yes。

MaxAutheTries
認証の試行回数。指定した回数以上失敗すると接続を切ります デフォルトは6

MaxSessions
最大セッションを張れる数。デフォルトだと10

RSAAuthentication
RSA認証を許可するかの設定。デフォルトはyes SSH version1で動作…?

ざっと説明を付加しました。参考までにどうぞ。

上記のうち自分の環境に合わせて好みで変更を加えてください。

変更後(一例)

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 12341                      ←アンコメント
#AddressFamily any
ListenAddress 192.168.100.1     ←アンコメント
#ListenAddress ::

# The default requires explicit activation of protocol 1
Protocol 2                      ←アンコメント

~(省略)~

# Authentication:

LoginGraceTime 30s              ←アンコメント
PermitRootLogin no
StrictModes inet                ←アンコメント
MaxAuthTries 3                  ←アンコメント
MaxSessions 5                   ←アンコメント

#RSAAuthentication yes
#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

まだ他にもやることはありますが現段階ではここまで変更しましょう。

■sudo有効

sudoを使えるユーザを作成しておきましょう。

# useradd -G wheel hoge
# passwd hoge

作成したら追加します

# visudo 

~(省略)~

## Allows people in group wheel to run all commands
wheel  ALL=(ALL)       ALL  ←%を外す

以上でおしまい。簡単でしょ?←



SSH鍵生成

次にSSHの公開鍵と秘密鍵を生成します。
WindowsTeraTermを入れてるなら下記サイトさんを参考にするといいかもしれません

www.j-oosk.com

生成したらサーバーに設置します。
これもTeraTermなんかを使うこともできるのでよかったらどうぞ。

www.j-oosk.com

設置したらパーミッション変更とかSSHフォルダとか作りましょう
権限をミスるとアクセスできなくなるので要注意!!

$ mkdir ~/.ssh/
$ mv ip_rsa.pub ~/.ssh/authorized_keys
$ sudo chmod 700 ~/.ssh/
$ sudo chmod 600 ~/.ssh/authorized_keys

忘れてましたけど、ポートを変更したのなら
iptablesまたはfirewalldの設定を変更するのもお忘れなく。



■ファイヤーウォールの変更(firewalldの場合)

雛形をコピーします。

# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh-2.xml 

コピーしたら中身を変更します

# vim /etc/firewalld/services/ssh-2.xml

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="12341"/> ←ここ
</service>

保存してサービスを追加します

# firewall-cmd --reload

successが表示されたら成功
サービスを追加します

# firewall-cmd --add-service=ssh-2
# firewall-cmd --permanent --add-service=ssh-2

successが表示されたら成功
もう一度サービスを再起動します

# firewall-cmd --reload

successが表示されたら追加完了です。
SSHを再起動して公開鍵でログインしてみましょう。

# systemctl restart sshd


ログインに成功したら最後の仕上げを行います。

SSHの最終設定

最後にパスワード認証を無効化し、公開鍵でアクセスできるようにします。

変更前

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

~(省略)~

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords yes
#PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
#ChallengeResponseAuthentication yes

AuthorizedKeysFile
公開鍵の場所を指定します。デフォルトのままでもいいですし
変更してもOKです。今回は変更しません。

PasswordAuthentication
パスワード認証方式を設定します。デフォルトはyesです。
今回は公開鍵認証を使うので無効化します。

PermitEmptyPasswords
空パスワードによる認証を設定します。デフォルトはyesになってます。
今回はnoにします。

ChallengeResponseAuthentication
チャレンジレスポンス認証による認証を設定します。デフォルトはyesになってます。
今回はnoにします。(使いたい方は別サイトを参考に設定してください。)

変更後

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

~(省略)~

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no               ←アンコメント
PermitEmptyPasswords no                 ←アンコメント
PasswordAuthentication yes              ←アンコメント

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes    
ChallengeResponseAuthentication no      ←アンコメント
■firewalldの最終設定

今のままだとデフォルトのポートが残ったままなので
ポートを閉じるべく設定を変えます。

まずはリストの表示を確認。

$ sudo firewall-cmd --list-all

public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh ssh-2
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

残っているので削除します。

$ sudo firewall-cmd --remove-service=ssh
$ sudo firewall-cmd --permament --remove-service=ssh

successが表示されたら削除完了です。

再起動して確認してみましょう。

$ sudo firewall-cmd --reload
success

$ sudo firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh-2
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

こうなれば成功です。
これでSSHの初期設定は完了しました。




次回はVPSで何か作ったときの設定でも残せるといいな…