LoginSignup
2
3

More than 5 years have passed since last update.

サーバー構築・初期設定(メモ)

Last updated at Posted at 2015-05-24

 サクラVPS CentOS release 6.5 2015/05/21
 参考
 http://qiita.com/junki0605/items/6ce3df1933ef6d31ddfa

rootでサーバーに接続

サーバーに接続

$ ssh root@***.**.**.***

rootパスワードの変更

$ passwd

yum updateと日本語化

$ yum update
$ vim /etc/sysconfig/i18n

変更前

LANG="C"

変更後

LANG="ja_JP.UTF-8"

作業用ユーザーの作成と設定

$ useradd chou
$ passwd  chou

新規ユーザ(chou)をルート権限を得られるグループに追加

$ usermod -G wheel chou
$ visudo

2行目の#を外して保存。これでwheelグループに属するユーザにsudoコマンドの実行を許可する。

## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

作業ユーザーでサーバーに接続

$ ssh chou@xxx.xx.xx.xxx

鍵認証の設定

公開鍵を入れるディレクトリをサーバーのホームディレクトリに作成しておく。

$ mkdir ~/.ssh
$ chmod 700 ~/.ssh

クライアント側(mac)でssh鍵ペアを作成

$ ssh-keygen

すると、〜/.ssh フォルダにid_rsa(秘密鍵)とid_rsa.pub(公開鍵)が作成される。

$chmod 600 ~/.ssh/id_rsa.pub

次に公開鍵をサーバーに転送

$ scp ~/.ssh/id_rsa.pub chou@xxx.xx.xx.xxx:~/.ssh/authorized_keys

で公開鍵をサーバーに転送し、authorized_keysという名前で保存する。
これで、鍵認証でssh接続できるようになりました。

ssh接続のセキュリティ設定

設定ファイルを開き、設定を変更する。

$ sudo vim /etc/ssh/sshd_config

ポートの変更

デフォルトの22 を 10022 に変更する(設定可能範囲:1024〜65535)。

変更前

#Port 22

変更後

Port 10022

rootによるログインの禁止

変更前

#PermitRootLogin yes

変更後

PermitRootLogin no

passwordでのログイン禁止

変更前

#PasswordAuthentication yes

変更後

PasswordAuthentication no

sshを再起動

$ sudo service sshd restart

FireWallの設定

iptablesファイルを作成する

$ sudo vim /etc/sysconfig/iptables

デフォルトで10022番だけにオープンするように変更する。

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# SSH, HTTP, FTP1, FTP2, MySQL
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 61203 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 20 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 10022 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT

iptablesを再起動

$ sudo /etc/rc.d/init.d/iptables restart

設定の確認

$ sudo iptables -L
2
3
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
2
3