2012年11月27日火曜日

サーバ設定メモ(その1)

お名前.comのVPS(KVM)を借りてみたので、設定などをメモ。

OSのアップデート


現在のバージョンを確認してアップデート。
# cat /etc/redhat-release
CentOS release 6.2 (Final)

# yum update

# cat /etc/redhat-release
CentOS release 6.3 (Final)

rootでのログインを禁止する

メンテナンス用ユーザーの追加
# useradd hoge
# passwd hoge
Changing password for user hoge.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
メンテナンス用のユーザーをwheelグループに追加
# usermod -G wheel hoge
rootになれるユーザーをwheelグループに限定
# vi /etc/pam.d/su
# auth            required        pam_wheel.so use_uid
↓変更
auth            required        pam_wheel.so use_uid

# vi /etc/login.defs
下記を追加
SU_WHEEL_ONLY yes
sudoを実行出来るユーザーをwheelグループのみに限定
# visudo
# %wheel  ALL=(ALL)       ALL
↓変更
%wheel  ALL=(ALL)       ALL
rootでのsshログインを禁止する
# vim /etc/ssh/sshd_config
#PermitRootLogin yes
↓変更
PermitRootLogin no
sshdの設定を反映
# /etc/init.d/sshd reload
Reloading sshd:                                            [  OK  ]

設定の反映を確認

別のターミナルを起動して下記を確認
  • rootでのログインが拒否されるか確認する
  • メンテナンス用ユーザーでログイン出来るか確認する
  • メンテナンス用ユーザーでrootになれるか確認する
  • メンテナンス用ユーザーでsudoが実行出来るか確認する

iptablesの設定

HTTP/HTTPS/FTP/POP3/IMAP/SMTP(サブミッションポート)/SSH(10022に変更する)を解放する
# /sbin/iptables -I INPUT 5 -p tcp --dport http -j ACCEPT
# /sbin/iptables -I INPUT 5 -p tcp --dport https -j ACCEPT
# /sbin/iptables -I INPUT 5 -p tcp --dport pop3 -j ACCEPT
# /sbin/iptables -I INPUT 5 -p tcp --dport imap -j ACCEPT
# /sbin/iptables -I INPUT 5 -p tcp --dport 20 -j ACCEPT
# /sbin/iptables -I INPUT 5 -p tcp --dport 21 -j ACCEPT
# /sbin/iptables -I INPUT 5 -p tcp --dport 587 -j ACCEPT
# /sbin/iptables -I INPUT 5 -p tcp --dport 10022 -j ACCEPT

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

SSHのポートを変更する

# vi /etc/ssh/sshd_config

#Port 22
↓変更
Port 10022

# /etc/init.d/sshd reload
別のターミナルを起動して10022で接続出来るか確認し、接続出来たらiptablesからsshの標準ポート22を削除する
# iptables -L --line-number

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     all  --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
5    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:10022
6    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:submission
7    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp
8    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp-data
9    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:imap
10   ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:pop3
11   ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https
12   ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http
13   REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

# iptables -D INPUT 4

# /etc/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
# service iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
別のターミナルを起動してPort22で接続出来ないか確認する