プログラミングノート

一からものを作ることが好きなエンジニアの開発ブログです。

さくらVPSの導入 基本設定編

さくらVPSの設定ブログはたくさんあるので今更ではありますが自分用に。
色々なブログから取捨選択しつつ、まず基本的な設定のみ実施しました。

  • ユーザー追加
  • 公開鍵の登録とSSH設定
  • sudoの設定
  • iptablesの設定
  • その他確認

ユーザー追加

作業ユーザーを追加します。

# adduser ntaku
# passwd ntaku
# usermod -G wheel ntaku
# id ntaku

公開鍵を登録

ローカルから公開鍵を転送します。

$ scp ~/.ssh/id_rsa.pub ntaku@xxx.xxx.xxx.xxx:/home/ntaku


authorized_keysに追加します。

# su ntaku
$ cd
$ mkdir .ssh
$ cat id_rsa.pub >> .ssh/authorized_keys 
$ chmod 700 .ssh
$ chmod 600 .ssh/authorized_keys

SSH設定

公開鍵認証のみ許可するようにします。

# vi /etc/ssh/sshd_config
# 22から変更
Port 10022

# rootログイン不可
PermitRootLogin no

# 公開鍵認証のみ許可
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys

PasswordAuthentication no
UsePAM no


sshd再起動

# /etc/init.d/sshd restart


作業コンソールを閉じる前にログインできることを確認しておきます。

ssh -p 10022 ntaku@xxx.xxx.xxx.xxx

sudo設定

wheelグループにsudoを許可します。

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


パスを追加しておきます。

$ vi .bash_profile
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin


rootパスワードを無効にします。

# passwd -l root

iptable

SSH, HTTPのみ許可するように修正します。

$ sudo 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
-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 -j REJECT --reject-with icmp-host-prohibited
COMMIT


再起動して、

$ sudo /etc/init.d/iptables restart


設定内容を確認します。

$ sudo 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 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

その他確認

不要デーモンの停止ですが、デフォルトで全て無効になっているようでした。
なっていない場合は下記で無効に。

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


/etc/inittab
不要コンソールが無効になっているか確認します。

# 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


/etc/sysconfig/selinux
SELINUXが無効になっているか確認します。

SELINUX=disabled


最後にrebootで終了。

参考

さくらに関する記事は本当にたくさんあるのでスムーズに設定できますね。今回は自分が後からもう一度作業しやすいようにまとめてありますので、詰まった方は参考サイトをどうぞ。