LoginSignup
0
0

More than 1 year has passed since last update.

CentOS7のセットアップ(さくらのVPS)

Last updated at Posted at 2018-07-11

さくらのVPSでサーバーを構築していきます。

以下の環境でセットアップしていきます。
〜さくらのVPS 1Gプラン〜
メモリ:1G
CPU:仮想2Core
ストレージ:HDD 100GB
OS:CentOS 7(カスタムOS)

はじめに

コントロールパネルとVNCでの操作は省略。

1.Terminalを起動する

Terminalアプリを起動。
次のコマンドを実行する。(abc.comはご自身のドメインに置き換えて)

$ ssh root@abc.com

すると初回のみ次のように表示される。

The authenticity of host 'abc.com (***.***.***.***)' can't be established.
ECDSA key fingerprint is SHA256:abcdef12345ghijk67890lmn.
Are you sure you want to continue connecting (yes/no)? 

”yes”を入力し、returnキーを押す。

Are you sure you want to continue connecting (yes/no)? yes

すると次のようなものが表示される。

Warning: Permanently added 'abc.com,***.***.***.***' (ECDSA) to the list of known hosts.
root@abc.com's password: 

パスワードを入力し、returnキーを押す。

Warning: Permanently added 'abc.com,***.***.***.***' (ECDSA) to the list of known hosts.
root@abc.com's password: 
[root@abc ~]# 

これでログインができた。

2.アップデートする

はじめに次のコマンドを実行し、アップデートする。

[root@abc ~]# yum -y update

3.直接rootでログインできないようにする

次のコマンドを実行し、直接rootでログインができないようにする。

[root@abc ~]# vim /etc/ssh/sshd_config

38行目先頭の#を削除し、"yes"を"no"にする。

/etc/ssh/sshd_config
#PermitRootLogin yes

/etc/ssh/sshd_config
PermitRootLogin no

保存したら次のコマンドを実行し、sshdを再起動する。

[root@abc ~]# systemctl restart sshd

ログアウトする。

[root@abc ~]# exit
Connection to abc.com closed.

次のコマンドを実行し、rootで直接ログインできなくなっていることを確認する。

$ ssh root@abc.com
root@abc.com's password: 
Permission denied, please try again.

4.SSHが切断されないようにする

デフォルトのままだと少しの間操作しないだけでSSHが切断される。

次のコマンドを実行する。

[root@abc ~]# vim /etc/ssh/sshd_config

112行目先頭の#を削除し、"0"を"60"にする。
113行目先頭の#を削除し、"3"を"30"にする。

/etc/ssh/sshd_config
#ClientAliveInterval 0
#ClientAliveCountMax 3

/etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 30

保存したら次のコマンドを実行し、sshdを再起動する。

[root@abc ~]# systemctl restart sshd

5.Port番号を変更する

デフォルトのままだと不正アクセスをされる可能性が上がる。

次のコマンドを実行する。

[root@abc ~]# vim /etc/ssh/sshd_config

17行目先頭の#を削除し、"22"を好きな数字にする。

/etc/ssh/sshd_config
#Port 22

/etc/ssh/sshd_config
Port 50000

保存したら次のコマンドを実行し、ポートを開放する。

[root@abc ~]# firewall-cmd --add-port=50000/tcp --zone=public --permanent

次のコマンドを実行し、Firewallをリロードする。

[root@abc ~]# firewall-cmd --reload

次のコマンドを実行し、sshdを再起動する。

[root@abc ~]# systemctl restart sshd

6.SELinuxを停止する

あとあと面倒になるのであらかじめ停止しておく。
次のコマンドを実行する。

[root@abc ~]# vim /etc/selinux/config

7行目の"enforcing"を"disabled"にする。

/etc/selinux/config
SELINUX=enforcing

/etc/selinux/config
SELINUX=disabled

保存したら次のコマンドを実行し、再起動する。

[root@abc ~]# reboot

再起動し終えたら再度ログインし、次のコマンドを実行する。

[root@abc ~]# getenforce

次の表示がされたらSELinuxは無効化されている。

[root@abc ~]# getenforce
Disabled

もしも"Enforcing"が表示されたら、3をもう一度実行する。

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