LoginSignup
160
165

More than 5 years have passed since last update.

CentOSの初期設定で最低限やること(@さくらのVPS)

Last updated at Posted at 2014-05-19

はじめに

さくらのVPSを借りた際に、いつもやっている最低限の設定手順を公開していきます。

初期設定

ユーザー設定

パスワードの設定をします。

# passwd

新規ユーザーの追加を行い、パスワードの設定、wheelグループへの追加を行います。

# useradd ユーザー名
# passwd ユーザー名
# usermod -G wheel ユーザー名

続いてrootに関する設定を行います。
以下ではrootになれるユーザーおよびsudoコマンドの実行ユーザーの設定を行っています。

# vim /etc/pam.d/su

# 以下の行のコメントを外す
auth required pam_wheel.so use_uid

# visudo

# 以下の行のコメントを外す
%wheel ALL=(ALL) ALL

SSHの設定

以下でSSHの設定を行っていきます。

rootログイン、空パス、パスワードログインを禁止します。

# vim /etc/ssh/sshd_config

PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no

続いてSSH鍵の設定を行います。
自分のホームディレクトリに.sshフォルダを作成し、使用する公開鍵を.sshディレクトリに転送します(今回はファイル名をid_rsa.pubとします)。その後SSHログインができるよう設定を行っていきます。

$ mkdir .ssh && cd .ssh
$ cat id_rsa.pub >> authorized_keys 
$ chmod 700 /home/ユーザー名/.ssh
$ chmod 400 authorized_keys

この時点でsshを再起動します。
そして、作成したユーザーでログインができるか確認をしてください。

# /etc/init.d/sshd restart

iptablesの設定

はじめに、sshで使用するポートの指定を行い、sshを再起動します。

# vim /etc/ssh/sshd_config

# 以下のようにポートを指定します
Port  22

# /etc/init.d/sshd restart

続いて、iptablesを設定します。

# vim /etc/sysconfig/iptable
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 -m state --state ESTABLISHED,RELATED -j ACCEPT

# SSH, HTTP
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

# 海外IPに対する設定
-A RH-Firewall-1-INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
-A RH-Firewall-1-INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 8 --rttl --name SSH -j DROP

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

COMMIT

上記ファイルを作成後、iptablesの再起動を行います。

# /etc/init.d/iptables restart

以上で設定は完了です。
最低限やるべきことは、ここまでかと思います。

あったら便利!

個人的にはzshとtmuxはぜひ使ってほしいので、以下に導入法を書いていきます。

zsh + oh-my-zsh

はじめにzshをインストールし、設定を行います。

# yum -y install zsh
# usermod -s /bin/zsh ユーザー名

続いてoh-my-zshを入れていきます。
これで面倒なzshの設定とおさらばです。

$ wget --no-check-certificate http://install.ohmyz.sh -O - | sh

tmux

コーディングする上で書かせないtmuxもインストールします。

# yum -y install tmux

おわりに

以上が最低限やっているCentOSの設定となります。
これもした方が良い!など意見ありましたら、コメントを頂けると幸いです。

参考

160
165
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
160
165