1. Vim インストール
エディタとして使い慣れたVimを使用しますので下記の通りインストール
1 |
# pkg install -y vim |
Vimを標準のエディタとして利用可能するため".cshrc"を編集し、反映させる
1 2 3 4 5 6 |
# vim ~/.cshrc 以下の内容を追加する alias vi vim 反映させる # source ~/.cshrc |
Vimの使用環境を整えるため".vimrc"を新規作成し、下記内容を記入
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# vi ~/.vimrc 以下の内容を追加する set nocompatible set encoding=utf-8 set nobackup set tabstop=4 set clipboard+=autoselect set number syntax on highlight Comment ctermfg=LightCyan set wrap set backspace=indent,eol,start |
2. SSH接続のセキュリティ対策
2.1 SSHポートの変更、ルートユーザーのログイン不可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# vi /etc/ssh/sshd_config 1 # $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $ 2 3 # This is the sshd server system-wide configuration file. See 4 # sshd_config(5) for more information. 5 6 # This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin 7 8 # The strategy used for options in the default sshd_config shipped with 9 # OpenSSH is to specify options with their default value where 10 # possible, but leave them commented. Uncommented options override the 11 # default value. 12 13 # Note that some of FreeBSD's defaults differ from OpenBSD's, and 14 # FreeBSD has a few additional options. 15 16 Port 2244 ←Portを2244に変更 17 #AddressFamily any 18 #ListenAddress 0.0.0.0 19 #ListenAddress :: 20 21 #HostKey /etc/ssh/ssh_host_rsa_key 22 #HostKey /etc/ssh/ssh_host_ecdsa_key 23 #HostKey /etc/ssh/ssh_host_ed25519_key 24 25 # Ciphers and keying 26 #RekeyLimit default none 27 28 # Logging 29 #SyslogFacility AUTH 30 #LogLevel INFO 31 32 # Authentication: 33 34 #LoginGraceTime 2m 35 PermitRootLogin no ←ルートユーザーでのログイン不可 36 #StrictModes yes 37 #MaxAuthTries 6 38 #MaxSessions 10 |
設定の反映
1 |
# service sshd restart |
2.2 公開鍵認証方式にする
① 公開鍵、秘密鍵ペアの作成---一般ユーザーにログインして作成する
作成先・ファイル名を指定しなければ、 /home/huong/.ssh/ に id_rsa, id_rsa.pub が作成される。 途中、鍵用のパスワードも入力する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# su - huong $ ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/huong/.ssh/id_rsa): Enter Created directory '/home/huong/.ssh'. Enter passphrase (empty for no passphrase): Password Enter same passphrase again: Again Paddword Your identification has been saved in /home/huong/.ssh/id_rsa Your public key has been saved in /home/huong/.ssh/id_rsa.pub The key fingerprint is: SHA256:IR0xTBIg2/uLxuqPox2mxYIcnloHb2xKlWwWKWzuT/U huong@Lepard The key's randomart image is: +---[RSA 4096]----+ | . ..o+=. | | . + . o.o | | = + . o | | o o + . . | | .o B . S | |+ooB o . | |o+O.B . E | |.O.Xo. . | |+o*=+ . | +----[SHA256]-----+ |
1 2 3 |
$ chmod 700 ~/.ssh $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys |
➁WinSCP等を利用し/home/huong/.ssh/id_rsa をWindowsの適当なところに保存する
➂SSH設定ファイルを編集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# vi /etc/ssh/sshd_config 35 PermitRootLogin no 36 #StrictModes yes 37 #MaxAuthTries 6 38 #MaxSessions 10 39 40 PubkeyAuthentication yes ←変更 41 42 # The default is to check both .ssh/authorized_keys and .ssh/authorized_keys 2 43 # but this is overridden so installations will only check .ssh/authorized_ke ys 44 AuthorizedKeysFile .ssh/authorized_keys ←コメント解除変更 45 46 #AuthorizedPrincipalsFile none 47 48 #AuthorizedKeysCommand none 49 #AuthorizedKeysCommandUser nobody 50 51 # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts 52 #HostbasedAuthentication no 53 # Change to yes if you don't trust ~/.ssh/known_hosts for 54 # HostbasedAuthentication 55 #IgnoreUserKnownHosts no 56 # Don't read the user's ~/.rhosts and ~/.shosts files 57 #IgnoreRhosts yes 58 59 # Change to yes to enable built-in password authentication. 60 # Note that passwords may also be accepted via KbdInteractiveAuthentication. 61 PasswordAuthentication no ←変更 62 #PermitEmptyPasswords no 63 64 # Change to no to disable PAM authentication 65 KbdInteractiveAuthentication no ←変更 66 67 # Kerberos options |
1 |
# service sshd restart |
④TeraTermでの接続
User name : ログインユーザー名
Password :公開鍵、秘密鍵ペアの作成で指定したパスワード
「Use RSA/DSA….」にチェックを入れ、「Private key file」の欄には先ほどwindowsに保存した「id_rsa」を指定する
3. ファイアウォール設定
FreeBSDでは以下のファイアーウォールがあります
・pf
・ipfw
・ipf
今回は、ipfwを使用してファイアーウォールの設定をします。
3.1 ipfwが動くか確認
1 2 3 |
# ipfw list ... Commnad not found.以外が出力されればOKです |
3.2 ipfwを使用するための設定を /etc/rc.conf に追記
1 2 3 4 5 |
# sysrc firewall_enable="YES" ←サーバー起動時に有効化 firewall_enable: YES -> YES # sysrc firewall_logdeny="YES" ←拒否時に /var/log/security にログ出力 firewall_logdeny: NO -> YES # sysrc firewall_script="/usr/local/etc/ipfw.rules" ←追加設定スクリプトを指定 |
3.3 追加設定スクリプト(/usr/local/etc/ipfw.rules)を作成
1 |
# vi /usr/local/etc/ipfw.rules |
追加設定スクリプトの内容-----とりあえず次のサービスポートを開放する
ftp (20,21), 初期ssh (22), smtp (25), dns (53) , http (80), ntp(123) , https(443) pop3(110), imap(143), mail-over(587) , 変更後SSH(2244)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#! /bin/sh # IPF="ipfw -q add" ipfw -q -f flush #loopback $IPF 10 allow all from any to any via lo0 $IPF 20 deny all from any to 127.0.0.0/8 $IPF 30 deny all from 127.0.0.0/8 to any $IPF 40 deny tcp from any to any frag # statefull $IPF 50 check-state $IPF 60 allow tcp from any to any established $IPF 70 allow all from any to any out keep-state $IPF 80 allow icmp from any to any $IPF 90 allow tcp from any to any 20 in $IPF 100 allow tcp from any to any 20 out $IPF 110 allow tcp from any to any 21 in $IPF 120 allow tcp from any to any 21 out $IPF 130 allow tcp from any to any 22 in $IPF 140 allow tcp from any to any 22 out $IPF 150 allow tcp from any to any 25 in $IPF 160 allow tcp from any to any 25 out $IPF 170 allow udp from any to any 53 in $IPF 175 allow tcp from any to any 53 in $IPF 180 allow udp from any to any 53 out $IPF 185 allow tcp from any to any 53 out $IPF 190 allow tcp from any to any 80 in $IPF 200 allow tcp from any to any 80 out $IPF 210 allow tcp from any to any 110 in $IPF 220 allow tcp from any to any 110 out $IPF 230 allow tcp from any to any 123 in $IPF 240 allow tcp from any to any 123 out $IPF 250 allow tcp from any to any 143 in $IPF 260 allow tcp from any to any 143 out $IPF 270 allow tcp from any to any 443 in $IPF 280 allow tcp from any to any 443 out $IPF 290 allow tcp from any to any 587 in $IPF 300 allow tcp from any to any 587 out $IPF 310 allow tcp from any to any 2244 in $IPF 320 allow tcp from any to any 2244 out # deny and log everything $IPF 500 deny log all from any to any |
システムの再起動
1 |
# shutdown -r now |
ipfwのリストを表示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# ipfw list 00010 allow ip from any to any via lo0 00020 deny ip from any to 127.0.0.0/8 00030 deny ip from 127.0.0.0/8 to any 00040 deny tcp from any to any frag offset 00050 check-state :default 00060 allow tcp from any to any established 00070 allow ip from any to any out keep-state :default 00080 allow icmp from any to any 00090 allow tcp from any to any 20 in 00100 allow tcp from any to any 20 out 00110 allow tcp from any to any 21 in 00120 allow tcp from any to any 21 out 00130 allow tcp from any to any 22 in 00140 allow tcp from any to any 22 out 00150 allow tcp from any to any 25 in 00160 allow tcp from any to any 25 out 00170 allow udp from any to any 53 in 00175 allow tcp from any to any 53 in 00180 allow udp from any to any 53 out 00185 allow tcp from any to any 53 out 00190 allow tcp from any to any 80 in 00200 allow tcp from any to any 80 out 00210 allow tcp from any to any 110 in 00220 allow tcp from any to any 110 out 00230 allow tcp from any to any 123 in 00240 allow tcp from any to any 123 out 00250 allow tcp from any to any 143 in 00260 allow tcp from any to any 143 out 00270 allow tcp from any to any 443 in 00280 allow tcp from any to any 443 out 00290 allow tcp from any to any 587 in 00300 allow tcp from any to any 587 out 00310 allow tcp from any to any 2244 in 00320 allow tcp from any to any 2244 out 00500 deny log ip from any to any 65535 deny ip from any to any |
4. NTPサーバー設定
4.1 /etc/ntp.conf中の参照先NTPサーバーを書き換えます。日本にある適当なサーバーを追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# vi /etc/ntp.conf 23 # The following pool statement will give you a random set of NTP servers 24 # geographically close to you. A single pool statement adds multiple 25 # servers from the pool, according to the tos minclock/maxclock targets. 26 # See http://www.pool.ntp.org/ for details. Note, pool.ntp.org encourages 27 # users with a static IP and good upstream NTP servers to add a server 28 # to the pool. See http://www.pool.ntp.org/join.html if you are interested. 29 # 30 # The option `iburst' is used for faster initial synchronization. 31 # 32 pool 0.freebsd.pool.ntp.org iburst 33 server ntp1.jst.mfeed.ad.jp ←追加 34 server ntp2.jst.mfeed.ad.jp ←追加 35 server ntp3.jst.mfeed.ad.jp ←追加 36 37 # |
4.2 起動時にntpが自動的に起動されるよう、/etc/rc.confに追記
1 2 3 4 5 |
# vi /etc/rc.conf 以下を追記 ntpd_enable="YES" ntpd_sync_on_start="YES" |
4.3 時刻を修正
時刻がずれすぎると NTP デーモンが動作を停止させるのでntpdate コマンドで時刻を修正してから NTP デーモンを起動させる
1 2 3 4 5 |
# ntpdate ntp.nict.jp 5 Aug 16:36:21 ntpdate[868]: step time server 61.205.120.130 offset -32398.937307 sec # /etc/rc.d/ntpd start Starting ntpd. |
4.4 時刻同期確認
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 0.freebsd.pool. .POOL. 16 p - 64 0 0.000 +0.000 0.000 ntp1.jst.mfeed. 133.243.236.17 2 u 34 64 1 16.627 +2.380 0.000 ntp2.jst.mfeed. 133.243.236.18 2 u 36 64 1 13.732 +0.654 0.000 ntp3.jst.mfeed. 133.243.236.19 2 u 37 64 1 17.334 +1.820 0.000 *ntp-a2.nict.go. .NICT. 1 u 26 64 1 10.889 -0.086 1.384 +133.152.126.129 133.243.238.163 2 u 23 64 1 15.265 +0.927 0.909 +v160-16-113-133 118.27.107.147 3 u 24 64 1 14.768 +2.054 1.202 122x215x240x52. 35.73.197.144 2 u 22 64 1 15.059 -0.900 0.770 s97.GchibaFL4.v 133.243.238.164 2 u 19 64 1 42.509 +9.819 10.158 |
+ 接続テストに合格し、いつでも参照可能なサーバー
– クラスタリング検査で、参照リストから外れたサーバー
* 参照同期中であると宣言されたサーバー