LoginSignup
0
0

More than 5 years have passed since last update.

手元のwindowsからリモートのcentos7にssh接続する

Last updated at Posted at 2016-05-27

前提

centos7 は最小構成のデフォルトインストール。
デフォルトで ssh は通信許可されている。
ネットワークは有効にしていること。
デフォルトで root で ssh ログインできるようになっているので最後に拒否すること。
windows 側の ssh クライアントは teraterm を使う。

windows 側の準備

teraterm の設定 >SSH 鍵生成で鍵を生成し、適当なフォルダに保存する。
パスフレーズの入力は不要。
生成された鍵のペアの名前を id_rsaid_rsa.pub とする。

centos7 側の準備

root でパスワードログインする。デフォルトでログインできる。

[root@centos7 ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@centos7 ~]# useradd sshuser
[root@centos7 ~]# su - sshuser
[sshuser@centos7 ~]$ id
uid=1000(sshuser) gid=1000(sshuser) groups=1000(sshuser)
[sshuser@centos7 ~]$ 
[sshuser@centos7 ~]$ mkdir -m 700 .ssh # 権限は 700 であることが必須
[sshuser@centos7 ~]$ cat >> .ssh/authorized_keys
# id_rsa.pub の内容をコピペする
[sshuser@centos7 ~]$ cat .ssh/authorized_keys
# id_rsa.pub と同じ内容が出力されることを確認する
[sshuser@centos7 ~]$ chmod 600 .ssh/authorized_keys # 権限は 600 であることが必須。
[sshuser@centos7 ~]$ exit
[root@centos7 ~]# id
uid=0(root) gid=0(root) groups=0(root)

ログイン確認

ここで別の teraterm を立ち上げて証明書認証を使った接続ができるか確認する。
パスワードログインしている teraterm は落とさないこと(設定ミスの場合は再接続できなくなる)。

teraterm のファイル > 新しい接続を選択し、サービスに SSH を選択する。
SSH 認証ウィンドウで、ユーザ名に sshuser を入力し、RSA/DSA 鍵を使うを選択する。
秘密鍵として先ほど作成した id_rsa を指定する。
パスフレーズは空欄のままで良いので OK をクリックする。

正常に接続できれば、続けて以下の設定を追加する。

事後作業

[root@centos7 ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@centos7 ~]# passwd sshuser
# ユーザのパスワードを設定する。
[root@centos7 ~]# usermod -aG wheel sshuser # ROOT に代わって管理者とする
[root@centos7 ~]# id sshuser
uid=1000(sshuser) gid=1000(sshuser) groups=1000(sshuser),10(wheel)
[root@centos7 ~]# vi /etc/ssh/sshd_config
[root@centos7 ~]# cat /etc/ssh/sshd_config | grep -vE '#|^$'
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
PermitRootLogin no # root ログイン不可
AuthorizedKeysFile      .ssh/authorized_keys
PasswordAuthentication no # パスワードログイン不可
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM no # PAM 認証(パスワードログイン、チャレンジレスポンス)不可
X11Forwarding yes
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 LANGUAGE
AcceptEnv XMODIFIERS
Subsystem       sftp    /usr/libexec/openssh/sftp-server
[root@centos7 ~]# 
[root@centos7 ~]# systemctl restart sshd
[root@centos7 ~]# 

UsePAM が no なので PasswordAuthentication は設定不要な気がする。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0