【Linux】sshの接続ポートをデフォルト(22番)のままにしてインターネットにさらすとどうなるか
sshの接続ポートはデフォルトTCP22番。
これは常識ですね。あまりに常識すぎて、デフォルトでインターネットに接続すると攻撃対象になりやすく危険とよく言われますが、本当でしょうか?
実は昨年10月にsshの接続ポートをデフォルトのままにしてサーバをインターネットに接続したところ、 実際に攻撃(というほど大げさではないですが)を受けたことがありました。
主な経緯は次の通りです。
2010年10月21日(木) Linuxサーバのセットアップ開始 2010年10月22日(金) セットアップが終わらないので家で続きをやろうと、 サーバをインターネットからssh接続可能にして帰宅(ssh接続ポートは22番) 2010年10月23日(土) (夜中にブラジルから攻撃) 朝、ログを見て攻撃に気付く → ssh接続ポート変更(とりあえず) → 以後、攻撃は無し
btmpファイルを見ると攻撃の様子がわかります。
(見やすいように表示を調整してあります)
【bamtファイル】 root :0 Thu Oct 21 18:31 - 18:31 (00:00) root :0 Fri Oct 22 11:07 - 11:07 (00:00) root ssh:notty Fri Oct 22 15:45 - 15:45 (00:00) 192.168.1.30 root ssh:notty Fri Oct 22 15:45 - 15:45 (00:00) 192.168.1.30 test01 ssh:notty Fri Oct 22 16:12 - 16:12 (00:00) *************.co.jp root ssh:notty Fri Oct 22 16:15 - 16:15 (00:00) *************.co.jp root ssh:notty Fri Oct 22 16:15 - 16:15 (00:00) *************.co.jp test01 ssh:notty Fri Oct 22 17:29 - 17:29 (00:00) *************.co.jp test01 ssh:notty Fri Oct 22 20:13 - 20:13 (00:00) *************.co.jp ====[ ここから ] ==================================================================== root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br aussiecr ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br aussiecr ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br gorzow ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br gorzow ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br dev ssh:notty Sat Oct 23 02:50 - 02:50 (00:00) server.#########.com.br dev ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br www ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br www ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br sec ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br sec ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br kylix ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br kylix ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br cisco ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br cisco ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br rita ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br rita ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br giovanna ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br giovanna ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br sec ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br sec ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:51 - 02:51 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br artem ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br artem ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br postgres ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br postgres ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br postgres ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br postgres ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br dev ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br dev ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br induacu ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br induacu ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br tollini ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br tollini ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br www ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br www ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br PlcmSpIp ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br PlcmSpIp ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:52 - 02:52 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br work ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br work ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br sysadmin ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br sysadmin ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br jason ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br jason ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br iony ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br iony ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br sauticom ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br sauticom ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br eak ssh:notty Sat Oct 23 02:53 - 02:53 (00:00) server.#########.com.br eak ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br sysadmin ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br sysadmin ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br provis ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br provis ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br halley ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br halley ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br prasoot ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br prasoot ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br thairepo ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br thairepo ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br georgeli ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br georgeli ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br edwardli ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br edwardli ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br dinochan ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br dinochan ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br sigcomm ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br sigcomm ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br test ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br test ssh:notty Sat Oct 23 02:54 - 02:54 (00:00) server.#########.com.br test ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br test ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br sigcomm ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br sigcomm ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br tklc ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br tklc ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br kylix ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br kylix ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br tedbaker ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br tedbaker ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br root ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br cyrus ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br cyrus ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br bin ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br sercon ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br sercon ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br sec ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br sec ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br nfsnobod ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br nfsnobod ssh:notty Sat Oct 23 02:55 - 02:55 (00:00) server.#########.com.br ====[ ここまで ] ==================================================================== test01 ssh:notty Mon Oct 25 08:43 - 08:43 (00:00) *************.*****.ne.jp test01 ssh:notty Mon Oct 25 08:43 - 08:43 (00:00) *************.*****.ne.jp test01 ssh:notty Mon Oct 25 08:43 - 08:43 (00:00) *************.*****.ne.jp
5分間に284回もトライするなんて、ブラジル人やり過ぎだよ・・・
幸いrootログインは禁止してあり、パスワードもランダムな文字列にしていたので実害はありませんでしたが、本当に攻撃を受けるんだなと身をもって感じました。
皆さんも気をつけてください。
【Linux】突然回線切断しても続きから作業を再開できるようにする方法
電波が不安定な状態で無線回線を使っていると回線が頻繁に切断され、その度に作業がやりなおし・・・なんてことがあります。
このような場合に対処するには、screenコマンドが便利です。
screenコマンドを使うと万一回線が切断しても、直前の状態から作業を再開できます。
screenコマンド
screenコマンドは仮想端末を管理するコマンドです。
screenコマンド上で複数の仮想端末を立ち上げることができ、1つの画面で複数の端末を切り替えながら使ったりすることができます。
screenコマンドは実に様々な機能を持っていますが、今回は回線切断に対応できるように起動・切断・再接続の方法について説明します。
起動
起動は簡単です。コマンドプロンプトで「screen」と入力します。
入力すると仮想端末が立ち上がります。
ウィンドウタイトルが「screen 0: bash」となっていて、screenコマンド上で管理されている仮想端末であることがわかります。
切断(デタッチ)
切断→再接続と操作した際に、プロセスが継続実行されていることが確認できるように「tail -f」を実行しておきます。
するとscreenコマンド上の仮想端末から切断され、screenコマンドが実行された元の端末に戻ります。
切断された仮想端末はバックグランドで動作し続け、ログアウトしても停止することはありません。
再接続(アタッチ)
再接続するために、まずlsオプションをつけてscreenコマンドを実行し、screenコマンドで管理されている仮想端末を確認します。
例では仮想端末「12417.pts-0.localhost」が動いているので、この仮想端末に再接続します。
再接続にするには、rオプションに仮想端末のプロセス番号「12417」をつけてscreenコマンドを実行します。
回線切断への操作手順
説明する必要はないかもしれませんが、回線切断時の操作手順をまとめておきます。
Step1. screenコマンドを実行し、仮想端末上で作業する。
*** 回線切断発生、サーバから強制的にログアウト ***
Step2. サーバに再ログイン
Step3. 「screen -ln」を実行し、仮想端末の一覧を確認。
Step4. 再接続したい仮想端末を見つけたら、「screen -r プロセス番号」を実行。
*** 作業再開 ***
screenコマンドは今回説明した以外にも、
- 1画面を分割し複数の仮想端末を表示して使う
- 同じ仮想端末に複数の端末から接続して画面共有のように使う
など様々な使い方ができます。興味のある方は調べてみると面白いと思います。
【Linux】ログイン中のユーザにメッセージを送る方法
緊急のサーバメンテナンスなどでシステムを停止する場合、ログイン中のユーザにその旨を至急通知したい場合があります。
Linuxではログイン中のユーザの相手のターミナルにメッセージを表示させる、wallコマンド・writeコマンドが用意されています。
これらを利用することで、ログイン中のユーザにメッセージを送ることできます。
wallコマンド : 全ユーザにメッセージを送る
≪書式≫ $ wall [Enter] ・・・ ログイン中の全ユーザのターミナルにメッセージを表示する。 (メッセージを記述) [Ctrl + D] → メッセージを送信 ≪記述例≫ 〜 送信側 〜 [root@localhost ~]# wall [Enter] This is test Message. [Ctrl + D] 〜 受信側 〜 [user1@localhost ~]$ Broadcast message from root (pts/0) (Mon Jan 10 16:44:24 2011): This is test message. [devpartner1@localhost ~]$
wallコマンドでは日本語も送信することができます。
writeコマンド : 個別のユーザにメッセージを送る
≪書式≫ $ write ユーザ名 [Enter] ・・・ 指定ユーザのターミナルにメッセージを表示する。 (メッセージを記述) [Ctrl + D] → メッセージを送信 ≪記述例≫ 〜 送信側 〜 [root@localhost ~]# write user1 [Enter] ・・・ ユーザ「user1」のターミナルにメッセージを送る。 This is test Message. [Ctrl + D] 〜 受信側 〜 [user1@localhost ~]$ メッセージ from root@localhost.localdomain on pts/0 at 16:37 ... This is test message. EOF [devpartner1@localhost ~]$
writeコマンドで日本語送信を試しましたが、wallコマンドと異なり上手く送ることができないようです。
【Linux】WindowsマシンとLinuxマシンでファイルを転送する方法
時々やり方を聞かれるのでメモ。
WindowsマシンとLinuxマシンの間でのファイル転送は、WinSCPというアプリケーションを使うと、とても簡単に実現できます。 WinSCPはSFTPまたはSCPでファイルを暗号化して転送するアプリケーションです。
今回は、WinSCPのインストールからファイル転送するところまでの流れを説明します。
Step1. ダウンロード
下記のWinSCPダウンロードページからインストーラ(Installation package)をダウンロードします。
WinSCP :: Official Site :: Download
Step3. セッション(接続先)の登録
インストールが完了したらセッション(接続先)を登録するため、WinSCPを起動します。
WinSCPが起動すると次の画面が現れるので、ホスト名・ユーザ名(任意:設定するとログインIDとして使用)・パスワード(任意:設定すると自動ログイン)を入力し、保存をクリックします。
セッションの保存名を入力するように求められるので、適当な名前をつけて保存します。
Step4. ログイン
Step3が終わると、セッション名の一覧が表示された画面になります(この画面は、次回以降WinSCPを起動したときの初期画面でもあります)。
ここから、接続先を選択して「ログイン」をクリックします。ログインID、パスワードを求められた場合には入力します。
なお、初めての接続する場合、sshに関する警告が出ます。接続先が間違いなければ「はい」を選択します。
【Linux】ログアウトしてもコマンドを実行させ続ける方法
バックアップなど時間がかかる処理を実施しなくてはいけないけど、終わるまで待っている時間がない、ってかもう帰りたい!ってことないですか?
そんな時、nohupを使うとログアウトしてもコマンドを実行させ続けることができ便利です。
≪書式≫ $ nohop コマンド [引数] & ・・・ nohupで実行したコマンドは、ログアウト後も実行され続ける。 処理中に出力されたメッセージは"nohup.out"に保存される。
たとえば、
$ nohop find / -type f -print > result.txt & [1] 14370 $ exit
とすると、ログアウト後もfindコマンドが実行され続けます。
【Linux】複数階層で構成されるディレクトリを一気に作成する方法
小ネタです。
複数階層で構成されるディレクトリを作成する場合、mkdirコマンドを「-p」オプション付きで実行することで一気に作成できます。
例えば、カレントディレクトリに何もない状態からディレクトリ「dirA/dirB」を作る場合、
「-p」オプションを付けずにmkdirコマンドを実行すると
$ mkdir dirA/dirB mkdir: ディレクトリ `dirA/dirB' を作成できません: そのようなファイルやディレクトリはありません
とエラーになりますが、「-p」オプションを付けてmkdirコマンドを実行すると
$ mkdir -p dirA/dirB
と上手くいきます。試しにtreeコマンドを実行すると
$ tree ; `-- dirA `-- dirB
ときちんと作成されていることがわかります。
【Linux】ユーザのログイン履歴を確認する方法
sshなどでリモートログインを可能にしている場合、ユーザのログイン履歴を調べたいことがあります。
こうした場合、lastコマンド、lastlogコマンド、lastbコマンドでログイン履歴を調べることができます。
lastコマンド、lastlogコマンドは成功したログインの履歴を表示します。
lastbコマンドは失敗したログインの履歴を表示します。
lastコマンド
≪書式≫ $ last [ユーザ名] ・・・ ログイン履歴を表示する。 ユーザ名:指定するとそのユーザのログイン履歴だけを表示。 省略した場合は全ユーザのログイン履歴を表示。
lastlogコマンド
≪書式≫ $ lastlog [ユーザ名] ・・・ システムに登録された各ユーザの最終ログイン履歴を表示する。 ユーザ名:指定するとそのユーザの最終ログイン履歴だけを表示。 省略した場合は全ユーザの最終ログイン履歴を表示。
lastbコマンド
≪書式≫ $ lastb [ユーザ名] ・・・ ユーザのログイン失敗履歴を表示する。 ユーザ名:指定するとそのユーザのログイン失敗履歴だけを表示。 省略した場合は全てのログイン失敗履歴を表示。