お名前.comのVPSでCentOS7をカスタムインストールで選択して使った時に行った最初のセットアップ手順です
お名前.comではCentOS7.1.1503バージョン以上のバージョンがドロップダウンで選択できなかった・・・なんてこったい(2017/5現在)
ISOイメージのアップロード領域は、VPS毎に10GByteが割り当てられているので、isoファイルをダウンロードして、それをアップロードしてそこからインストールしました
■ CentOSのインストール
・isoファイルのダウンロード
isoファイルをダウンロードして、お名前.comの手順に従い指定のディレクトリにアップロードします
・インストール手順
インストールの詳細については、こちらの2サイトで詳細に解説されているので、ここではダイジェストだけピックアップします。
- インストールの概要
自動で正しく設定してくれているものもありますが、未設定のものは設定していきます
- ソフトウェアの選択
- インストール先
今回はローカルの標準ディスクを選択
- ネットワークとホスト名
設定をONにすると、お名前.comで設定されているアドレスなどが自動で付与されました
- セキュリティポリシー
■ インストール後の設定
上記のインストールが完了してから、行った手順が以下の通りとなります。
ユーザ作成は、上記のインストール時に行うこともできますが、ここではコマンドライン操作で実行します。
・とりあえずアップデート
yum -y update
・新しいユーザーを作成する
もちろん、一番初めはrootでログインして作業をはじめます。
とはいえ、これから先のパッケージのインストール作業や諸々の作業をrootで行うのはセキュリティ的によくありません。
なんでもできる権限を持っているがゆえに、うっかり、が取り返しのつかないことになるので、
必要な時にだけ権限を実行スレな良いアカウントを作ります。
ユーザーを作成します
adduser bohebohe
パスワードを設定します
passwd bohebohe
・Rootの権限を付与します
sudoをコマンドの前につけて実行することを許可します。
CentOS7 では、「wheel」グループに属しているユーザーはsudoを実行することができるので、このグループに追加します
gpasswd -a bohebohe wheel
・作成したユーザーの公開鍵を設定します
作っていない場合は、ssh-keygenで作成しておきましょう。
サーバに上げておくのは、公開鍵のみです。
ローカル環境で作成した公開鍵の中身をコピーしましょう。
cat ~/.ssh/id_rsa.pub
さて、ここからは、サーバでの作業です。
まだrootでログインしている状態だと思います。ここで、ユーザーを切り替えます。
su - bohebohe
そうすると、/home/boheboheに移動しているはずです。ここで、鍵をおくディレクトリとファイルを準備しましょう
mkdir .ssh
chmod 700 .ssh
authorized_keysファイルを新規に作成して、ローカル環境のコピーした公開鍵の内容をペーストしましょう
vi .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
元のrootアカウントに戻りましょう
exit
・SELinux
私は無効にしました。
- 現状確認
getenforce
Enforcing # SELinux は有効な状態
- 設定変更
vi /etc/selinux/config
# 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 - No SELinux policy is loaded.
SELINUX=disabled # disabled に変更
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
- 再起動
reboot
・SSHデーモンの設定
rootアカウントでのssh接続を禁止しましょう
vi /etc/ssh/sshd_config
PermitRootLogin no
ポートを22番以外にしておきましょう
Port 10022
パスワードでのログインは禁止して、公開鍵認証だけにする
PasswordAuthentication no
再起動させて反映させます
systemctl reload sshd
これで、rootアカウントでsshできなくなります。
新しく作成したboheboheアカウントで、sudoできることを確認してから、exitしてくださいね。
・ネットワーク設定の確認
インストールの時にネットワーク設定下のと間違いないか確認
$ nmcli d show eth0
GENERAL.デバイス: eth0
GENERAL.タイプ: ethernet
GENERAL.ハードウェアアドレス: 02:16:xx:xx:xx:xx
GENERAL.MTU: 1500
GENERAL.状態: 100 (接続済み)
GENERAL.接続: eth0
GENERAL.CON パス: /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.キャリア: オン
IP4.アドレス[1]: xx.xx.xx.xx/24
IP4.ゲートウェイ: xx.xx.xx.xx
IP4.ルート[1]: dst = xx.xx.xx.xx/32, nh = xx.xx.xx.xx, mt = 100
IP4.DNS[1]: xx.xx.xx.xx
IP4.DNS[2]: xx.xx.xx.xx
IP4.ドメイン[1]: xxxxxxx.jp
IP6.アドレス[1]: xxxxx::xxxx:xxxx:xxxx:xxxx/64
IP6.ゲートウェイ:
・サービスの設定
現在起動しているサービスの確認
$ systemctl -t service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
auditd.service loaded active running Security Auditing Service
chronyd.service loaded active running NTP client/server
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
firewalld.service loaded active running firewalld - dynamic firewall daemon
getty@tty1.service loaded active running Getty on tty1
irqbalance.service loaded active running irqbalance daemon
kdump.service loaded active exited Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel
lvm2-lvmetad.service loaded active running LVM2 metadata daemon
lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling
lvm2-pvscan@252:2.service loaded active exited LVM2 PV scan on device 252:2
network.service loaded active exited LSB: Bring up/down networking
NetworkManager-wait-online.service loaded active exited Network Manager Wait Online
NetworkManager.service loaded active running Network Manager
polkit.service loaded active running Authorization Manager
postfix.service loaded active running Postfix Mail Transport Agent
rhel-dmesg.service loaded active exited Dump dmesg to /var/log/dmesg
rhel-import-state.service loaded active exited Import network configuration from initramfs
rhel-readonly.service loaded active exited Configure read-only root support
rsyslog.service loaded active running System Logging Service
sshd.service loaded active running OpenSSH server daemon
systemd-hwdb-update.service loaded active exited Rebuild Hardware Database
systemd-journal-catalog-update.service loaded active exited Rebuild Journal Catalog
systemd-journal-flush.service loaded active exited Flush Journal to Persistent Storage
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-random-seed.service loaded active exited Load/Save Random Seed
systemd-remount-fs.service loaded active exited Remount Root and Kernel File Systems
systemd-sysctl.service loaded active exited Apply Kernel Variables
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories
systemd-udev-trigger.service loaded active exited udev Coldplug all Devices
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-done.service loaded active exited Update is Completed
systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tuned.service loaded active running Dynamic System Tuning Daemon
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
42 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
lines 15-50/50 (END)
サービスの起動設定の一覧表示
$ systemctl list-unit-files -t service
UNIT FILE STATE
abrt-ccpp.service enabled
abrt-oops.service enabled
abrt-pstoreoops.service disabled
abrt-vmcore.service enabled
abrt-xorg.service enabled
abrtd.service enabled
auditd.service enabled
autovt@.service enabled
blk-availability.service disabled
brandbot.service static
chrony-dnssrv@.service static
chrony-wait.service disabled
chronyd.service enabled
console-getty.service disabled
console-shell.service disabled
container-getty@.service static
cpupower.service disabled
crond.service enabled
dbus-org.fedoraproject.FirewallD1.service enabled
dbus-org.freedesktop.hostname1.service static
dbus-org.freedesktop.import1.service static
dbus-org.freedesktop.locale1.service static
dbus-org.freedesktop.login1.service static
dbus-org.freedesktop.machine1.service static
dbus-org.freedesktop.network1.service bad
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service enabled
dbus-org.freedesktop.timedate1.service static
dbus.service static
debug-shell.service disabled
dm-event.service disabled
dnsmasq.service disabled
dracut-cmdline.service static
dracut-initqueue.service static
dracut-mount.service static
不要なサービスは停止しておきます
$ systemctl stop [サービス名]
$ systemctl disable [サービス名]
旧式の SysV サービスがいくつか残っており、こちらの自動起動設定は chkconfig で制御することになります。
$ chkconfig --list
注記: この出力は SysV サービスのみであり、ネイティブな systemd のサービスは含まれていません。
systemd services. SysV 設定のデータはネイティブな systemd の設定によって上書きされます。
systemd サービスを一覧表示するには 'systemctl list-unit-files' を使用してください。
特定のターゲットにおいて有効化されているサービスを確認するには、
'systemctl list-dependencies [target]' 。
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
・リポジトリの追加
標準リポジトリ提供のものを優先するようにプラグインをインストール
yum -y install yum-plugin-priorities
EPEL、CentOS SCLo Software collections、Remi's RPM repository を追加してプライオリティを変更
yum -y install epel-release
sed -i -e "s/\]$/\]\npriority=5/g" /etc/yum.repos.d/epel.repo
yum -y install centos-release-scl-rh centos-release-scl
sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/CentOS-SCLo-scl.repo
sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/remi-safe.repo
・ Firewalldの設定
アクセスできるポートの制限をしましょう。
CentOS6までのファイアウォールは「iptables」でしたが、CentOS7からは「firewalld」が初期状態では有効となっています。
Firewalld における各種サービスの定義はゾーンに対して設定します。
設定したゾーンとネットワークインターフェースを関連付けることで設定が有効となります。
「firewall-cmd」コマンドを使って設定をしていきます。
> 現在の状態を確認します
Activeなので稼働中ですね
$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 日 2017-05-14 11:53:48 JST; 10h ago
Docs: man:firewalld(1)
Main PID: 22966 (firewalld)
CGroup: /system.slice/firewalld.service
└─22966 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
5月 14 11:53:47 xxx.myvps.jp systemd[1]: Starting firewalld - dynamic firewall daemon...
5月 14 11:53:48 xxx.myvps.jp systemd[1]: Started firewalld - dynamic firewall daemon.
・defaultゾーン設定を表示
「default」として設定されているゾーンの設定内容を確認できます。
$ firewall-cmd --get-default-zone
public
「public」ゾーンが「default」設定になっていて「eth0」のNICに割り当てられています
$ sudo firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
> ゾーンに通信を許可するサービスの追加
$ firewall-cmd [--permanent] --zone=ゾーン名 --add-service=サービス名
httpを追加します
$ firewall-cmd --permanent --zone=public --add-service=http
success
$ firewall-cmd --reload
success
追加できるサービスを調べる
$ firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
各サービスの詳細は設定ファイルをみる。変更したい場合は/etc/firewalldにコピーして行う
$ cat /usr/lib/firewalld/services/http.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>WWW (HTTP)</short>
<description>HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description>
<port protocol="tcp" port="80"/>
</service>
> ゾーンに通信を許可するポートの追加
$ firewall-cmd [--permanent]--zone=ゾーン --add-port=ポート番号/プロトコル
「public」ゾーンにポート番号「10022」プロトコル「TCP」のルールを追加してみます。
$ firewall-cmd --permanent --zone=public --add-port=10022/tcp
success
$ firewall-cmd --reload
success
確認
$ firewall-cmd --zone=public --list-all
必要に応じて、ポートの追加、削除をしておきましょう