LoginSignup
8
8

More than 5 years have passed since last update.

conohaのcentosで初期設定

Last updated at Posted at 2017-08-08

flaskで作成したアプリをデプロイするときの、私が行うサーバ(conoha + centos7.1)の初期設定方法について書いていきます。

VPC起動

サーバとしてconohaのVPSを使います。
conohaのコンソール画面からVPSを起動します。
今回はcentos7.1で作成しました。
VPSが作成されたら ip を確認しておきます。

ユーザーの作成

まず、rootでssh接続して作業用のユーザーを作成します。

server
$ ssh root@<ipアドレス> -p 22
# pass入力

$ adduser develop
$ passwd develop
# pass入力
$ gpasswd -a develop wheel # developをwheelに追加

# suでdevelopユーザーに変更できるか確認
# developユーザーでsudoできるか確認
$ su develop
# pass入力
$ sudo echo 'aaa'
aaa

ここからはdevelopユーザーで設定を行なっていきます。

sshの設定見直し

セキュリテイ的にssh接続のみ許可するように変更します。

server
$ sudo vi /etc/ssh/sshd_config
# 以下を設定
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

$ sudo systemctl restart sshd

# sshの設定
$ cd ~/
$ mkdir .ssh
$ chmod 700 .ssh
$ cd .ssh/
$ ssh-keygen -t rsa -b 2048
Enter file in which to save the key (/home/develop/.ssh/id_rsa): <生成されるキーのファイル名> #今回はconoha
Enter passphrase (empty for no passphrase): <pass入力> # なんでも
Enter same passphrase again: <もう一度pass入力>

# AuthorizedKeysFileで設定したファイル名に公開鍵の名前変更
$ mv conoha.pub authorized_keys
$ chmod 600 authorized_keys

# ローカルPCに秘密鍵をコピー
$ cat conoha
# 出てきた内容をコピーする

ローカルPCでの作業に移ります。

mac
$ cd ~/.ssh
$ vi conoha # 先ほどのキーの中身をコピー
$ chmod 600 conoha

# ssh_configを編集
$ vi config
Host conoha
  HostName <ipアドレス>
  User develop
  Port 22
  IdentityFile ~/.ssh/conoha

# sshの設定が完了したら一旦接続できるか確認をします。
$ ssh conoha
# pass入力
# 入れたらok

再びサーバーに戻ります。

server
$ sudo vi /etc/ssh/sshd_config
PermitRootLogin no # rootへのssh接続禁止
PasswordAuthentication no # パスワード入力によるログイン禁止

$ sudo systemctl restart sshd

ローカルのPCからsshでrootとパスワードログインできないことを確認します。

mac
$ ssh root@<ipアドレス> -p 22
$ ssh develop@<ipアドレス> -p 22

iptables設定

iptablesを設定していきます。
centos7のfirewall使ってもいいと思うのですが、不慣れなので参考にした資料にあるようにiptablesをインストールしてそちらを使います。

server
$ sudo yum install iptables-services

/etc/sysconfig/iptablesを編集します。
僕の場合は下記のように編集してみました。

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8022 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

ブラウザでアクセスできるように、80を解放しました。
また、sshの接続先のポートを22から変更しています。今回は8022にしました。
というのも、22へのウイルスの攻撃が多いからです。22ポートを開いて数分でどこからともなくアクセスが来ます。
基本的なセキュリティ対策を行なっていれば問題はないですが、私の場合は念のため8022など別のポートに変えています。
それに伴いshhで使うポートの設定を変更します。

server
$ sudo vi /etc/ssh/sshd_config
Port 8022

$ sudo systemctl restart iptables
$ sudo systemctl restart sshd

ローカルに戻ってssh/configPortを編集して8022にします。

再起動しても問題ないように、iptablesを自動起動の設定に追加します。
このときiptablesを使っているのでfirewallを切り、自動起動も切ります。

server
$ sudo systemctl stop firewalld
$ sudo systemctl mask firewalld
$ sudo systemctl enable iptables
$ systemctl list-unit-files --type=service
# iptabelsがenableになっているか確認

ただ、僕がわからないのでiptables使っていますが、友人曰くcentos7系ならfirewalldを使った方が今後はいいよと言われました。
別の機会に設定方法を勉強してみたいと思います。

基本的な設定はこれで以上です。
ここから各種ソフトのインストールや設定をdevelopユーザーで行なっていきます。

8
8
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
8
8