爆速でCentOS7環境を整える(引越し)


(半)個人で運用中のサーバーで爆速でcentos7環境を整えないといけなかったので備忘録
Centos6からCentos7にアップグレードしました。

バックアップ

だいたい /etcにあるもの

/etc/nginx
/etc/letsencrypt
/etc/rc.d/init.d/...
...などなど

インストール

前回のインストールだと余計なものが入り過ぎてたので、カスタムインストールを選択し、minimalインストールをぶちかます

このときにrootのパスワードと、追加のアカウントも作成できるので、作業用ユーザー追加した。
Administratorのチェック入れずに、Advanced..を押してみて、ユーザーグループに追加みたいな欄があったから、お、wheel入れとくか、とおもって追加したら、Administratorにチェックが入ってちょっと恥ずかしい気持ちに

ここまで1時間くらい(バックアップ取らなきゃいけないファイルを探してはコピーしててこれなので、必要なファイルを把握していればもっと早くできるはず)
インストール待ち、、、

SSH Firewall SELinux

インストール終わった。再起動が終わったら、まずローカルで2つのターミナルを開いて(事故防止)sshでログイン

SELinux

勉強不足なのでとりあえずOFF..

/etc/selinux/config
...
SELINUX=disabled
...
[root@~]# setenforce 0

Firewall

iptablesの時代と比べてfirewalldは使いやすくて万々歳ですな、、

[root@~]# cd /etc/firewalld/zones
[root@~]# cp public.xml master.xml
master.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Custom</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <!-- <service name="ssh"/> -->
  <port protocol="tcp" port="(任意のsshポート番号)"/>
  <port protocol="tcp" port="80"/>
  <port protocol="tcp" port="443"/>
  <service name="dhcpv6-client"/>
</zone>
/etc/firewalld/firewalld.conf
# firewalld config file

# default zone
# The default zone used if an empty zone string is used.
# Default: public
DefaultZone=master

今回は安全対策にsshのポート番号を変更します

SSH

sshdの変更を加える前にfirewalldを再起動してしまってやらかし
Connection: ESTABLISHEDでもリセットされてしまうのか、、
コントロールパネルからなんとか直す

/etc/ssh/sshd_config
#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port (さっき設定したやつ)
...
PubkeyAuthentication yes

公開鍵の設定をローカルで行う。下二行は今までの設定を更新するためのもの

ローカル
$ ssh-keygen -y -f ~/.ssh/(今まで使ってた秘密鍵)
...公開鍵が出力される...
$ ssh-keygen -R (サーバーのip)
$ ssh-keyscan -H (サーバーのip) >> ~/.ssh/known_hosts

サーバーに公開鍵を登録

/home/(作業用ユーザー)/.ssh/authorized_keys
 (出力された公開鍵を登録)

パーミッションを整えてsshd再起動

[root@~]# chmod 644 /home/(作業用ユーザー)/.ssh/authorized_keys
[root@~]# systemctl restart sshd

ここでローカルから作業用ユーザーでログインできる事を確認後、パスワード認証、rootでのsshログインを無効化する

/etc/ssh/sshd_config
...
PermitRootLogin No  # コメントアウトされてるのでコメントを外して yes→no
...
PasswordAuthentication no
...
ChallengeResponseAuthentication no  # 多分いじらなくてもこうなってる
...
GSSAPIAuthentication no  # よく知らない認証なのでno(殴
...
# PasswordAuthentication と ChallengeResponseAuthentication
# が両方noになっていればおそらく大丈夫ですが使わないし念の為
UsePAM no  
[root@~]# systemctl restart sshd

この後で別ウィンドウでログインできればオーケー!できなかったら設定を見直す。

必要なサービスのインストール

やばい、、2時間くらいたってる気がする

Python3がなかったので

[root@~]# yum install -y https://centos7.iuscommunity.org/ius-release.rpm
[root@~]# yum install -y python36-devel python36-pip
...
他のパッケージもインストール
...

Certbotの設定

前のサーバーではcertbot-autoを使ってSSLの証明書を管理してたのでその設定を移行しないといけません。バックアップをとった/etc/letsencryptからどれくらい修復できるかな。。

[root@~]# yum install -y certbot

Python2関連がずらずらと、、うっ

バックアップとったものを復帰させて、certbot renew --dry-runすると何やらエラーが、、
liveの下のファイルはarchiveの下のもののsymlinkじゃないといけないんだと
該当するファイルをmd5sumで探してなおす
(参考:Let's Encrypt(certbot)でrenewしたら"expected /etc/letsencrypt/live/~/cert.pem")

これでもまだnginxのプラグインがないよーとか言われたのでyum install -y certbot-nginx

いけたっぽい!

自動更新設定をonにする

[root@~]# systemctl enable --now certbot-renew.timer

これで悪夢のような「このサイトは信頼できません!」の表示から解放される

あとはだいたい個別の環境の話なので別記事で書こうかな

参考記事

SELinuxの無効化 - Qiita
[Linux] firewalldでfirewallの設定をする - Qiita
よく使う ssh-keygen の tips - Qiita
SSHのknown_hostsをスマートに更新する - Qiita
SSHが遅いときには”GSSAPIAuthentication no”の設定を入れることを考えてみてほしい - じゃあ、おうちで学べる
sshによるユーザ列挙攻撃"osueta" - ろば電子が詰まつてゐる
Python 3 を CentOS 7 に yum でインストールする手順 | WEB ARCH LABO
CentOS 7 サービス自動起動設定 | server-memo.net
let's Encryptの証明書更新 CentOS7編 - Qiita