結論
-
sed -n {row}p {file_name}- 削除前に行(row)を指定して確認する場合
-
sed -i '{row}d' {file_name}-
'{row}d'で行を指定して削除し、-iでその結果をファイルに反映させる
-
sedコマンドのオプションについて:https://atmarkit.itmedia.co.jp/ait/articles/1610/17/news015.html
背景
リモートサーバーへのSSH時に、以下のようなメッセージが出て失敗。
原因として対象サーバーのSSHホスト鍵が変更された場合に、接続元のknown_hostsに記録されているホスト鍵と一致しなくなることによるもので、新しいSSHホスト鍵に更新することで接続が成功します。
今回は上記のsedコマンドで解消できたのでメモ。
# ssh root@hoge01
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:11
ECDSA host key for hoge01 has changed and you have requested strict checking.
Host key verification failed.
対応
Offending ECDSA key in /root/.ssh/known_hosts:11にあるようにknwon_hostsの11行めに問題があるので、そこを確認し削除する
(削除前にknown_hosts.oldとしてバックアップを残しています)
$ pwd
/root/.ssh
$ ls
id_rsa id_rsa.pub known_hosts
$ cp known_hosts known_hosts.old
$ sed -n 11p known_hosts
hoge01,192.168.0.0 ssh-rsa **:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**.
$ sed -i '11d' known_hosts
$ ssh hoge01
The authenticity of host 'hoge04 (192.168.0.0)' can't be established.
RSA key fingerprint is **:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'hoge01,192.168.0.0' (RSA) to the list of known hosts.
root@hoge01's password:
補足
今回のようなホスト鍵更新の際はsedでやらなくても以下のようにssh-keygenで一発でできるらしい
バックアップ自動でやってくれるのもありがたい
$ ssh-keygen -R hoge01
# Host hoge01 found: line 11 type RSA
/root/.ssh/known_hosts updated.
Original contents retained as /root/.ssh/known_hosts.old