LoginSignup
0
1

More than 5 years have passed since last update.

CentOS7にsshで公開鍵認証を使って接続するまで

Last updated at Posted at 2018-06-15

CentOS側

ルートユーザーに変更
sshd_configを編集

$ su-
# vi /etc/ssh/sshd_config

ポート修正
ルートログインしない

sshd_config
#Port 22
Port 10022
#PermitRootLogin yes
PermitRootLogin no

SELinuxの設定変更コマンドをインストール
SELinuxで10022ポートを許可
ファイヤウォールのSSH設定ファイルをコピー

# yum -y install policycoreutils-python
# semanage port -a -t ssh_port_t -p tcp 10022
# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/
# vi /etc/firewalld/services/ssh.xml

22=>10022に書き換え

ssh.xml
- <port protocol="tcp" port="22"/>
+ <port protocol="tcp" port="10022"/>

ファイヤウォールの設定変更を反映
SSHサーバーを再起動

firewall-cmd --reload
systemctl restart sshd.service

自分のPC

SSHで接続

$ ssh -p 10022 [ユーザー名]@[ホスト名]

公開鍵認証設定

.sshディレクトリで鍵生成id_rsaのパーミッションを600に
(秘密鍵がid_rsa、公開鍵がid_rsa.pub)

$ cd ~/.ssh
$ ssh-keygen -t rsa
$ chmod 600 id_rsa
$ scp -p 10022 id_rsa.pub [ユーザー名]@[ホスト名]:.ssh/.

.ssh/configを下記のようにしておくと

ssh 〇〇

で接続できるようになる。

.ssh/config
Host 〇〇
 HostName [ホスト名]
 Port 10022
 User [ユーザー名]
 IdentityFile ~/.ssh/id_rsa
 ServerAliveInterval 60

CentOS側

.sshディレクトリに移動
id_rsa.pubの内容をauthorized_keysにコピー
authorized_keysのパーミッションを600に

cd .ssh
cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys

参考

sshで公開鍵認証を使ってアクセスする
https://qiita.com/mountcedar/items/43157ff1225c56500655

0
1
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
1