ubuntuへのssh接続の方法とは?

今回は、ubuntuへのssh接続の方法について説明します。ここでは、PowerShellからWSL2(Windows Subsystem for Linux 2)上のubuntuにssh接続を行います。
sshクライアントおよびsshサーバの設定方法、接続方法について紹介します。
WindowsPC 1台で完結しますので、ubuntuへのssh接続の方法に興味のある方はぜひご覧ください。
sshクライアントおよびサーバ
ここでは、PowerShellからWSL2上のubuntuにssh接続を行います。WSL2を導入していない場合は事前に導入してください。クライアント側(PowerShell)のバージョンは以下です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
PS C:\Users\user> wsl --list --verbose NAME STATE VERSION * Ubuntu-20.04 Running 2 PS C:\> $PSVersionTable Name Value ---- ----- PSVersion 5.1.18362.1171 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.18362.1171 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 PS C:\> ssh -V OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5 PS C:\> |
サーバ側(ubuntu)のバージョンは以下です。
|
$ cat /etc/issue Ubuntu 20.04.1 LTS \n \l $ ssh -V OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f 31 Mar 2020 $ |
公開鍵と秘密鍵の作成
SSH接続するために、クライアント側でssh-keygenコマンドで公開鍵と秘密鍵を作成します。ここでは「-t rsa」オプションを付与してRSA暗号方式で作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
PS C:\Users\(ユーザー名)> ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (C:\Users\(ユーザー名)/.ssh/id_rsa): # そのままEnterを押下 Created directory 'C:\Users\(ユーザー名)/.ssh'. Enter passphrase (empty for no passphrase): # passphraseを入力 Enter same passphrase again: # 再度、passphraseを入力 Your identification has been saved in C:\Users\(ユーザー名)/.ssh/id_rsa. Your public key has been saved in C:\Users\(ユーザー名)/.ssh/id_rsa.pub. The key fingerprint is: SHA256:glwgc4NFGjxtfcGyRAYenHR8BzkH9I2p4Dl1reGIGEw (ユーザー名)@(PCの機種名) The key's randomart image is: +---[RSA 2048]----+ | .+BE*+o== | | +X=*= *.o= | | .o+.o=.+* o | | . *.= = o | | + * S o | | o | | | | | | | +----[SHA256]-----+ PS C:\Users\(ユーザー名)> |
ssh-keygenコマンドが成功すると、.sshフォルダが生成されます。.sshフォルダ配下にはid_rsaとid_rsa.pubが生成されます。id_rsaが秘密鍵、id_rsa.pubが公開鍵です。
id_rsa.pubの内容をクリップボードにコピーしておきます。
|
PS C:\Users\(ユーザー名)\.ssh> cat .\id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQClQ1ZSy0j****************************************************************************************************************************************************************************************************** (ユーザー名)@(PCの機種名) PS C:\Users\(ユーザー名)\.ssh> |
sshサーバの設定
sshサーバの設定方法を紹介します。まず、id_rsa.pubの内容を.ssh/authorized_keysファイルに貼り付けます。
次に、サーバ側にopenssh-serverをインストールします。
|
$ sudo apt-get install openssh-server [sudo] password for (ユーザー名): |
コマンドが成功したら、ifconfigコマンドでIPアドレスを確認します。以下のように失敗する場合はnet-toolsをインストールしてからifconfigコマンドを実行してください。
|
$ ifconfig Command 'ifconfig' not found, but can be installed with: sudo apt install net-tools $ sudo apt install net-tools $ ifconfig |
次に、ssh-keygenコマンドでhostキーを作成します。
|
$ sudo ssh-keygen -A ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519 $ |
sshのサービスをstartさせます。
|
$ sudo /etc/init.d/ssh start * Starting OpenBSD Secure Shell server sshd [ OK ] $ |
ssh接続
クライアント側からssh接続してみます。以下のコマンドを実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
PS C:\Users\(ユーザー名)> ssh ユーザー名@IPアドレス The authenticity of host 'IPアドレス (IPアドレス)' can't be established. ECDSA key fingerprint is SHA256:kFOwqNI1gzmnN0KwIrCNvPVuGJhc4faYDKR3ehF3ULg. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'IPアドレス' (ECDSA) to the list of known hosts. Enter passphrase for key 'C:\Users\ユーザー名/.ssh/id_rsa': Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Tue Dec 29 15:39:06 JST 2020 System load: 0.0 Processes: 10 Usage of /: 0.4% of 250.98GB Users logged in: 0 Memory usage: 3% IPv4 address for eth0: IPアドレス Swap usage: 0% 0 updates can be installed immediately. Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Tue Dec 29 15:39:06 JST 2020 System load: 0.0 Processes: 10 Usage of /: 0.4% of 250.98GB Users logged in: 0 Memory usage: 3% IPv4 address for eth0: IPアドレス Swap usage: 0% 0 updates can be installed immediately. 0 of these updates are security updates. The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. $ |
無事にssh接続できました。
接続先で、何かコマンドを実行してみましょう。接続を終了するにはexitコマンドを入力します。
|
$ ls $ uname -a Linux PC機種名 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux $ cat /etc/issue Ubuntu 20.04.1 LTS \n \l $ exit logout Connection to IPアドレス closed. PS C:\Users\(ユーザー名)> |
まとめ
いかがでしたでしょうか。PowerShellからWSL2上のubuntuにssh接続を行う方法について紹介しました。
ぜひご自身でコマンドを打ち込んで、理解を深めてください。
-
インフラ業界に特化
ネットワーク・サーバー・データベース等、ITインフラ業界に特化。Cisco Systemsプレミアパートナーをはじめ各種ベンダーのパートナー企業です。
業界を知り尽くしているからこそ大手の取引先企業、経験豊富なエンジニアに選ばれています。
-
正社員なのにフリーランスのような働き方
正社員の方でも希望を聞いたうえでプロジェクトをアサインさせていただいており、フリーランスのような働き方が可能。帰社日もありません。
プロジェクト終了後もすぐに次の案件をご紹介させていただきますのでご安心ください。
-
大手直取引の高額案件
案件のほとんどが大手SIerやエンドユーザーからの直取引のためエンジニアの皆様へに高く還元できています。
Ciscoをはじめ、Juniper、Azure、Linux、AWS等インフラに特化した常時300件以上の案件があります。
-
スキルアップ支援
不要なコストを削減し、その分エンジニアの方へのスキルアップ支援(ネットワーク機器貸出、合格時の受験費用支給など)や給与で還元しています。
受験費用例)CCNP,CCIE:6-20万円、JNCIS:3-4万円、AWS:1-3万円など
※業務に関連する一定の資格のみ。各種条件がありますので詳しくは担当者へにお尋ねください。
-
現給与を保証します!※
前職の給与保証しており、昨年度は100%の方が給与アップを実現。収入面の不安がある方でも安心して入社していただけます。
※適用にはインフラエンジニアの業務経験1年以上、等一定の条件がございます。
-
インセンティブ制度
ネットワーク機器の販売・レンタル事業等、売上に貢献いただいた方にはインセンティブをお支払いしています。
取引先企業とエンジニア側、双方にメリットがあり大変好評をいただいています。
-
社会保険・福利厚生
社員の方は、社会保険を完備。健康保険は業界内で最も評価の高い「関東ITソフトウェア健康保険組合」です。
さらに様々なサービスをお得に利用できるベネフィットステーションにも加入いただきます。
-
東証プライム上場企業グループ
ネプラスは東証プライム上場「株式会社オープンアップグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。