ssh
sshの設定
ssh-keygenによる公開鍵/私有鍵のキー・ペアの生成
$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/xxxx/.ssh/id_rsa): Enter passphrase (empty for no passphrase): <= パスフレーズを入力 Enter same passphrase again: <= 再度パスフレーズを入力 Your identification has been saved in /home/xxxx/.ssh/id_rsa. Your public key has been saved in /home/xxxx/.ssh/id_rsa.pub. The key fingerprint is: 0c:7c:79:fd:dc:08:d6:50:de:86:5f:84:e3:98:c9:3f xxxx@yyyy $ ls .ssh id_rsa id_rsa.pub $ chmod 400 .ssh/id_rsa*
debian/linuxインストール直後のPCは、相手側からsshするとパスワード入力を要求し、パスワード入力でアクセス可能。
自分側PCで、 $ chmod +w .ssh/authorized_keys
ssh-copy-id で自分側PCにsshでアクセスしたい相手側へid_rsa.pubをコピーし、相手側ホームディレクトリの.ssh/authorized_keysに追加され、相手側で以下の作業を行えば、公開鍵認証によるsshが可能となる。
$ ssh-copy-id -i ~/.ssh/id_rsa.pub 自分側PCのIPアドレス で相手側PCの公開鍵は ~/.ssh/id_rsa.pub で、自分側PCの .ssh/authorized_keys に登録される
自分側PCで、
$ chmod +w .ssh/authorized_keys 一回目のsshでは、以下の様な表示がされる $ ssh IPアドレス The authenticity of host 'IPアドレス (IPアドレス)' can't be established. RSA key fingerprint is 95:f2:f2:c8:17:2d:07:69:c6:ed:7e:9f:d3:f5:b1:10. Are you sure you want to continue connecting (yes/no)? ここで、yesを入力すればOK 次に、自分側PCから相手側PCにアクセス出来る様にするには、 相手側PCで $ ssh-copy-id -i ~/.ssh/id_rsa.pub 相手側PCのIPアドレス すれば、相手側PCのid_rsa.pubをコピーできる 自分側PCで $ chmod -w .ssh/authorized_keys
さて、相手側PCへsshしてみると
$ ssh 相手側PCのIPアドレス Enter passphrase for key '/home/xxxx/.ssh/id_rsa': の様に表示されるので、相手側PC用のパスフレーズを入力すればOK。
でも、毎回入力するのは面倒なので自分側PCの電源投入直後の一回だけパスフレーズを入力すればいい様にする。自分側PCで、
$ sudo apt-get install keychain 「 Configuring keychain」のFor more information please read the man pageで、<Ok>を選択しenterキー そして、 $ vi .bashrc で、以下を最後の行以降に追加 /usr/bin/keychain ${HOME}/.ssh/id_rsa . ~/.keychain/${HOSTNAME}-sh 一度ログアウトし、ログインし直すと一回目のログインの時だけ KeyChain 2.5.1; http://www.gentoo.org/proj/en/keychain/ Copyright 2002-2004 Gentoo Foundation; Distributed under the GPL * Initializing /home/xxx/.keychain/xxx-sh file... * Initializing /home/xxx/.keychain/xxx-csh file... * Starting ssh-agent * Adding 1 ssh key(s)... Enter passphrase for /home/xxxx/.ssh/id_rsa: と表示されるので、自分側PCのパスフレーズを入力すればOK。 2回目以降のログインでは、パスフレーズの入力が不要。
自分側PCに相手側PCからrootでのsshやパスワード認証でのsshを拒否する様にするため、
$ sudo vi /etc/ssh/sshd_config で、以下の変更を行う ##PermitRootLogin yes PermitRootLogin no #ChallengeResponseAuthentication yes ChallengeResponseAuthentication no #UsePAM yes UsePAM no $ sudo /etc/init.d/ssh restart で、sshdを再起動
ssh接続したターミナルのタイトルに、接続先のユーザー名@ホスト名が表示されない時
接続された側ユーザーの .bash_profile に以下を追加 export PROMPT_COMMAND="echo -ne '\033]0;${USER}@${HOSTNAME}\007';$PROMPT_COMMAND"
ssh.txt · 最終更新: 2025/02/17 04:53 by admin