VPS サーバ

さくら VPS + CentOS 基本的な設定

投稿日:

さくら VPS でサーバを借りて 勉強も併せていろいろやりました.

その際に行った基本的な設定をまとめました。サーバはデフォルトの Cent OS 5 (x86_64) です。

ユーザ設定

ユーザ名 root では危ないので新しいユーザを作成します。

ユーザー名は好きなのにしてください.ここではasamaというユーザーを作ります.

# useradd asama
# passwd asama
Changing password for user asama.
New UNIX password:パスワード入力
Retype new UNIX password:パスワード再入力
passwd: all authentication tokens updated successfully.

続いて,いま作ったユーザが root になれるように wheel グループに追加します。

 
# usermod -G wheel asama 
 


ポート番号を変更する

Port 22 のままだと攻撃対象になりやすいため Port を変更する.

# vi /etc/ssh/sshd_config
#Port 22
Port 10022

その後以下のようにして SSH を再起動し反映します。

# /etc/init.d/sshd restart
2	Stopping sshd:                                  [  OK  ]
3	Starting sshd:


SSHの設定

今回、クライアント側の OS は Windows で作業を行いました.Tera Termで作業を行いました.

  • id_rsa.pub : 公開鍵 – サーバ側に置く
  • id_rsa.ppk : 秘密鍵 – WinSCP 用
  • id_rsa : 秘密鍵 – Tera Term 用

・生成されたファイルをサーバ側に移動する

id_rsa.pub をさくら VPS 側に移動させます。

ファイルを移動させたら、.ssh ディレクトリを作成し、id_rsa.pub を作成したディレクトリに移動させファイル名を authorized_keys に変更します。

$cd ~
$mkdir .ssh
$mv id_rsa.pub .ssh
$mv id_rsa.pub authorized_keys

次にサーバ側でパーミッションの調整します。

# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/authorized_keys

・sshd_config の設定変更

ポートの変更で使ったsshd_configを編集します.

 
# vi /etc/ssh/sshd_config 
 

設定変更は以下の4つです.

  • PasswordAuthenticationno にし root によるログインを禁止にします。
  • PasswordAuthenticationno にし root を含め、全てのユーザでパスワードによるログインを禁止します。
  • PermitEmptyPasswordsno に設定し 空のパスワードでのログインを禁止にします。
  • ChallengeResponseAuthenticationno に設定し チャレンジレスポンスログインを許可しないようにします。
PermitRootLogin no

PasswordAuthentication no

PubkeyAuthentication yes

ChallengeResponseAuthentication no

設定が完了したら SSH を再起動し設定を反映します。

 
# /etc/init.d/sshd restart 
 

このとき、SSH 鍵の設定がきちんと行われているかをチェックする場合、ログアウトはせずに別のウィンドウ(Tera Term であればもうひとつ開いて)でログインできるかを試してください。

万が一設定に問題があった場合、一度ログアウトしてしまうと二度とログインできなくなります

④ファイアウォールの構築

 iptables というファイルを作成し、そこに設定をいろいろと記述していきます。

その前に iptables がインストールされているかチェックします。以下のコマンドで確認できます。

# yum list installed | grep iptables
iptables.x86_64            1.3.5-5.3.el5_4.1      installed
iptables-ipv6.x86_64       1.3.5-5.3.el5_4.1      installed

サーバの起動時に iptables も起動するように設定します。

 
# /sbin/chkconfig iptables on
 

自動起動の設定ができているか確認します。

# /sbin/chkconfig --list iptables
iptables  0:off  1:off  2:on   3:on   4:on   5:on   6:off

iptables のインストールの確認と自動起動の設定を終えましたら 次は実際に iptables ファイルを作成します。このファイルはいわばルールのようなものです。

SSH は上で設定した 10022 番、HTTP は 80 番、FTP は 20・21 番、MySQL はデフォルトの 3306 番に設定しました。22 ~ 26 行目がそれにあたります。

 
# vi /etc/sysconfig/iptables

*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 10022 -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 3306  -j ACCEPT

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

COMMIT

上のフィルタリング設定に関しては以下の記事が参考になります。

 参考:iptablesの設定 | サーバの実験室 RedHat/Fedora

設定を反映させるため iptables を再起動します。

# /etc/rc.d/init.d/iptables restart
Flushing firewall rules:                           [  OK  ]
Setting chains to policy ACCEPT: filter            [  OK  ]
Unloading iptables modules:                        [  OK  ]
Applying iptables firewall rules:                  [  OK  ]

以下のコマンドで設定の確認ができます。

# /sbin/iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain RH-Firewall-1-INPUT (2 references)
target     prot opt source       destination
ACCEPT     all  --  anywhere     anywhere
ACCEPT     icmp --  anywhere     anywhere       icmp any
ACCEPT     esp  --  anywhere     anywhere
ACCEPT     ah   --  anywhere     anywhere
ACCEPT     udp  --  anywhere     224.0.0.251    udp dpt:mdns
ACCEPT     udp  --  anywhere     anywhere       udp dpt:ipp
ACCEPT     tcp  --  anywhere     anywhere       tcp dpt:ipp
ACCEPT     all  --  anywhere     anywhere       state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere     anywhere       state NEW tcp dpt:10022
ACCEPT     tcp  --  anywhere     anywhere       state NEW tcp dpt:http
ACCEPT     tcp  --  anywhere     anywhere       state NEW tcp dpt:ftp-data
ACCEPT     tcp  --  anywhere     anywhere       state NEW tcp dpt:ftp
ACCEPT     tcp  --  anywhere     anywhere       state NEW tcp dpt:mysql
REJECT     all  --  anywhere     anywhere       reject-with icmp-host-prohibited


不必要なデーモンをストップ
パフォーマンスを少しでも上げるためサーバに必要ないサービスを OFF にします。

# /sbin/chkconfig auditd off
# /sbin/chkconfig autofs off
# /sbin/chkconfig avahi-daemon off
# /sbin/chkconfig bluetooth off
# /sbin/chkconfig cups off
# /sbin/chkconfig firstboot off
# /sbin/chkconfig gpm off
# /sbin/chkconfig haldaemon off
# /sbin/chkconfig hidd off
# /sbin/chkconfig isdn off
# /sbin/chkconfig kudzu off
# /sbin/chkconfig lvm2-monitor off
# /sbin/chkconfig mcstrans off
# /sbin/chkconfig mdmonitor off
# /sbin/chkconfig messagebus off
# /sbin/chkconfig netfs off
# /sbin/chkconfig nfslock off
# /sbin/chkconfig pcscd off
# /sbin/chkconfig portmap off
# /sbin/chkconfig rawdevices off
# /sbin/chkconfig restorecond off
# /sbin/chkconfig rpcgssd off
# /sbin/chkconfig rpcidmapd off
# /sbin/chkconfig smartd off
# /sbin/chkconfig xfs off
# /sbin/chkconfig yum-updatesd off


要らないコンソールを無効にする

  /etc/inittab を編集します。以下のように 6 ~ 10 行目をコメントアウトします。デフォルトで既に無効になっていました。

# vi /etc/inittab
:
:
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
# 2:2345:respawn:/sbin/mingetty tty2
# 3:2345:respawn:/sbin/mingetty tty3
# 4:2345:respawn:/sbin/mingetty tty4
# 5:2345:respawn:/sbin/mingetty tty5
# 6:2345:respawn:/sbin/mingetty tty6


selinux を無効

/etc/sysconfig/selinuxSELINUX=enforce を以下のように書き換え無効にします。こちらもデフォルトでは既に無効になっていました。

# vi /etc/sysconfig/selinux
:
:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled

設定変更を反映させる

サーバを再起動し設定を反映させます。

 
# reboot
 



-VPS, サーバ
-,

Copyright© あさま , 2024 All Rights Reserved.