62セキュリティ対策(ssh)
sshはパスワードや通信経路を暗号化したセキュアな接続を実現するプロトコルです。
●SSHとSFTP
SSH(SecureShell)はもともと安全なリモート操作を実現するために開発されたプロトコルです。
公開鍵暗号を使った認証や通信経路の暗号化などの機能を提供します。
しかし現在ではファイル転送にも利用できます。
SSH通信路で利用可能なファイル転送プロトコルには「SCP」と「SFTP」の二つがあります。
現在一般手的に使われるほとんどのSSHサーバソフトは両者をサポートしています。
安全なファイル転送も実現可能です。
●sshで直接rootでログインすることはリスクがあります(rootのパスワードがネットワーク上を流れることがリスクの要因。もちろん暗号化されてはいますが、、、、、)。
【演習1】
まずは、TeraTermから新たにrootで直接ログインしてみてください。デフォルト設定がそのままであれば接続可能なはずです。これは、上述のようにリスクがありますのでsshでrootの認証を不可にします。
(1)sshdの設定ファイル(sshd_config)を書き換える
[root@aso ~]# vi /etc/ssh/sshd_config
sshd_config 内に以下の設定を行うこと(コメント#を外して以下の設定に変更する)。
(38行目あたりを見てください)
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes → no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
(2)設定ファイルを書き換えたらサービスを再起動する
[root@aso ~]# systemctl restart sshd
(3)新たにターミナル(TeraTerm)を起動する
rootでログインできないことを確認すること
※パスワードの認証エラーになります!!
●実際の運用時はセキュリティを上げるためにsshのポートはデフォルトから変更するべきです(tcpの22番ポートはWellKnownポートです)。
【演習2】
/etc/ssh/sshd_config の設定を変更してデフォルト接続ポートを変える。
(1)/etc/ssh/sshd_config の設定を変更します。
# vi /etc/ssh/sshd_config
#Port 22
とあるところのコメントを外します(#を削除する)。
Port 22 → Port 2233 へ変更します。
デフォルトの接続ポートであるTCPの22番の代わりとして2233番を使うという意味です。
(2)sshを再起動します。
# systemctl restart sshd
(3)新たにターミナル(TeraTerm)を起動してTCPポートを変更します。
(既に起動しているターミナルは落とさないこと)
これまでの実習が確実にできていれば繋がりません。なぜでしょうか、、、、、、、。
まずは、sshのポートが2233になっていることを確認してください。
[root@aso ~]# netstat -pant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1247/rpcbind
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1378/cupsd
tcp 0 0 0.0.0.0:2233 0.0.0.0:* LISTEN 1929/sshd ←←←ココ
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1577/master
tcp 0 0 0.0.0.0:51722 0.0.0.0:* LISTEN 1298/rpc.statd
tcp 0 52 192.168.1.90:2233 192.168.1.60:50463 ESTABLISHED 1935/sshd
tcp 0 0 192.168.1.90:22 192.168.1.60:50450 ESTABLISHED 1863/sshd
tcp 0 0 :::111 :::* LISTEN 1247/rpcbind
tcp 0 0 ::1:631 :::* LISTEN 1378/cupsd
tcp 0 0 :::2233 :::* LISTEN 1929/sshd
tcp 0 0 ::1:25 :::* LISTEN 1577/master
tcp 0 0 :::34472 :::* LISTEN 1298/rpc.statd
[root@inspiron ~]#
【課題】
あとは、どうすればよいでしょうか、、、、、、、
繋がったら、対処したコマンドを個人チャットで報告してください。
その後、sshの接続ポートは元の22番に戻しておいた方が良いでしょう。
後々、繋がらないトラブルがないようにしたいので!
【参考】
/etc/ssh/sshd_configの設定値のコメントはデフォルト値のようです
# $OpenBSD: sshd_config,v 1.73 2005/12/06 22:38:28 reyk 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:/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 change a
# default value.
#Port 22 1.利用するポート番号の指定(22)
#Protocol 2,1
Protocol 2 2.サポートするSSHのバージョンの指定(バージョン2)
#AddressFamily any
#ListenAddress 0.0.0.0 接続を許可するIPアドレス(すべて)
#ListenAddress :: 接続を許可するIPアドレス(すべて)
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key ホスト用鍵ファイルの指定(デフォルト値)
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key ホスト用鍵ファイルの指定(デフォルト値)
#HostKey /etc/ssh/ssh_host_dsa_key ホスト用鍵ファイルの指定(デフォルト値)
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h 鍵ファイルの生成時間の指定(1時間)
#ServerKeyBits 768 サーバー用鍵ファイルのビット長(768ビット)
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV ログメッセージの分類の指定(AUTH)
#LogLevel INFO ログメッセージの内容レベルの指定(INFO)
# Authentication:
#LoginGraceTime 2m 接続からログインまでの猶予時間(2分)
#PermitRootLogin yes 3."root"アカウントのログイン認証(許可)
#StrictModes yes ユーザーパーミッションのチェック(有効)
#MaxAuthTries 6 ログインまでの最大許容試行回数(6回)
#RSAAuthentication yes .RSA認証(許可)
#PubkeyAuthentication yes 公開鍵認証(許可)
#AuthorizedKeysFile .ssh/authorized_keys 鍵ファイルの場所(デフォルト値)
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
※.このコンテンツではrhosts認証は扱いません
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no パスワードなしでの認証(拒否)
PasswordAuthentication yes パスワード認証(許可) ← ここでパスワード認証を無効にできる(その場合は鍵認証となる)
# Change to no to disable s/key passwords
※.このコンテンツではチャレンジレスポンス認証は扱いません
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
※.このコンテンツではKerberos認証は扱いません
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
※.このコンテンツではGSSAPI認証は扱いません
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
# Set this to 'yes' to enable PAM authentication, account processing,
※.このコンテンツではPAM認証は扱いません
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# ChallengeResponseAuthentication=no
#UsePAM no
UsePAM yes
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL
#AllowTcpForwarding yes TCP転送の許可(有効)
#GatewayPorts no ポート中継の許可(無効)
#X11Forwarding no
X11Forwarding yes X-Windowへのデータ転送(有効)
#X11DisplayOffset 10 X-Windowのディスプレイ番号オフセット値(+10)
#X11UseLocalhost yes X-Windowをlocalhostでのみ使用許可(する)
#PrintMotd yes 対話ログイン時のメッセージ表示(有効)
#PrintLastLog yes 対話ログイン時の前回ログイン日時の表示(有効)
#TCPKeepAlive yes 接続状態の監視パケット送信(有効)
#UseLogin no 対話ログイン時の"login"コマンドの使用(無効)
#UsePrivilegeSeparation yes 起動プロセスをroot権限と分離する(有効)
#PermitUserEnvironment no ユーザー環境変数の変更許可(拒否)
#Compression delayed データ圧縮の許可(認証後に許可する)
#ClientAliveInterval 0 クライアント側の状態のチェック間隔(チェックしない)
#ClientAliveCountMax 3 クライアント側の状態チェック試行回数(3回)
#ShowPatchLevel no OpenSSHのパッチ内容のクライアントへの通知(しない)
#UseDNS yes クライアント側IPアドレスの逆引きチェック(する)
#PidFile /var/run/sshd.pid プロセスIDファイル(PidFile)の指定(デフォルト値)
#MaxStartups 10 最大同時起動プロセス数(10)
#PermitTunnel no トンネリング(VPN)の使用(しない)
# no default banner path
#Banner /some/path 警告メッセージのファイルの指定(なし)
# override default of no subsystems
※.このコンテンツではデータ転送サブシステムは扱いません
Subsystem sftp /usr/libexec/openssh/sftp-server
変更後のsshベースの通信には明示的なポートの指定が必要です。
ssh -p 2233 ~
scp -P 2233 ~