2016年5月6日金曜日

CentOS 6.x インストール後の初期設定

個人的にインストール直後によくやる設定をまとめてみた。
用途はサーバ一般。vimのインストールは好みで。

システムアップデート


# yum update

vim インストール


# yum install vim

管理者ユーザ追加


# useradd [ユーザ名]

追加したユーザにパスワードを設定する

# passwd [ユーザ名]

追加したユーザに sudo 権限を付与する

# visudo
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
[ユーザ名]    ALL=(ALL)       ALL

root ユーザによる ssh ログインの禁止


# vim /etc/ssh/sshd_config

PermitRootLogin の値を "no" にする
#PermitRootLogin yes
PermitRootLogin no

SELinux無効化


vim /etc/selinux/config

"SELINUX=enforcing" を削除して、下記設定を追記
SELINUX=disabled

再起動


# reboot

SElinux と sshd の設定変更が反映される

2016年5月3日火曜日

iptables にルールを追加する

サーバの設定をしていると、iptablesの設定変更はよく行うが、結構簡単に忘れてしまうので、メモ。
  1. 設定のバックアップ
    # cp -a /etc/sysconfig/iptables{,.`date +%Y%m%d`}
    # ll /etc/sysconfig/iptables*
    -rw-------. 1 root root  476  4月 30 07:17 2016 /etc/sysconfig/iptables
    -rw-------. 1 root root 1974  7月 24 11:10 2015 /etc/sysconfig/iptables-config
    -rw-------. 1 root root  476  4月 30 07:17 2016 /etc/sysconfig/iptables.20160503
    -rw-------. 1 root root  476  4月 30 07:17 2016 /etc/sysconfig/iptables.old
    
  2. iptablesの起動
    # service iptables start
    iptables: ファイアウォールルールを適用中:                  [  OK  ]
    
  3. 現在のルールを確認する
    # iptables -L --line-numbers
    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    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
    (以下略)
    
  4. 新ルールを追加
    下記コマンドで新ルールを追加する。
    # iptables -I [チェインの種類] [ルール番号] -p [プロトコル] -m [プロトコル] --dport [ポート番号] -j ACCEPT
    
    今回は、ルール番号5に80番ポートを開放するルールを追加する。
    # iptables -I INPUT 5 -p tcp -m tcp --dport 80 -j ACCEPT
    
  5. 新ルールを確認
    # iptables -L --line-numbers
    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:http 
    6    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
    (以下略)
    
  6. 設定を保存
    このままでは、iptablesを再起動すると設定が消えてしまう。下記コマンドを実行し、設定を保存する。
    # service iptables save
    iptables: ファイアウォールのルールを /etc/sysconfig/iptable[  OK  ]中:
    
  • ルールを削除する時は
    # iptables -D [チェインの種類] [ルール番号]