
Ubuntu に OpenSSH サーバーをインストールしたいのですが、どうすればいいですか?
次のことを行う必要があります:
OpenSSHサーバーをセットアップする
ユーザーのSSH公開鍵と秘密鍵のペアを設定する
パスワードログインを無効にする
ルートユーザーを有効にする
ルートユーザー用のSSH公開鍵と秘密鍵のペアを設定する
パスワードを設定する
答え1
ターミナルに移動して次のように入力します:
sudo su
aptitude install openssh-server openssh-client
インストールをテストする
ps -A | grep sshd
出力が次のような場合:
<some number> ? 00:00:00 sshd
これで、ssh デーモンが実行されます。
ターミナルに再度入力します。
ss -lnp | grep sshd
出力が次のような場合:
0 128 :::22 :::* users:(("sshd",16893,4))
0 128 *:22 *:* users:(("sshd",16893,3))
つまり、sshデーモンが着信接続を待機していることを意味します
次に、設定ファイルを編集します。まず、元のファイルのバックアップを作成します。
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults
設定ファイルを開いて編集します
sudo gedit /etc/ssh/sshd_config
弱いパスワードは簡単に推測されてしまいます。ベストプラクティスは、パスワードの代わりに SSH キーを使用することです。
したがって、パスワード認証は完全に無効になります。
ラインに行く
#PasswordAuthentication yes
それを次のように置き換えます
PasswordAuthentication no
転送を有効にすると、すでにパスワードを推測している攻撃者にさらに多くの選択肢を与えることになります。
だから無効にしました。少しは安全になります
ラインに行く
AllowTcpForwarding yes
X11Forwarding yes
そしてそれを
AllowTcpForwarding no
X11Forwarding no
特定のユーザーのログインを明示的に許可したり、特定のユーザーのログインを拒否したりできます。
そのためには、設定ファイルの最後に次の行を追加する必要があります。
AllowUsers Fred Wilma
DenyUsers Dino Pebbles
ラップトップの最適なパフォーマンスのために、2 つの保留中の接続を許可します。3 番目と 10 番目の接続の間、システムは 30% から 10 番目の同時接続で 100% までランダムに接続を切断し始めます。これは次の行で実行できます。
MaxStartups 2:30:10
より多くのエラーやその他の有用な情報をログに記録するには、次の行を変更します。
LogLevel INFO
LogLevel VERBOSE に
初心者の攻撃者を追い払うために、バナーを表示することができます。ハッシュタグを先頭から削除します。
#Banner /etc/issue.net
それを作る
Banner /etc/issue.net
次にターミナルに移動して次のように入力します:
sudo gedit /etc/issue.net
***************************************************************************
NOTICE TO USERS
This computer system is the private property of its owner, whether
individual, corporate or government. It is for authorized use only.
Users (authorized or unauthorized) have no explicit or implicit
expectation of privacy.
Any or all uses of this system and all files on this system may be
intercepted, monitored, recorded, copied, audited, inspected, and
disclosed to your employer, to authorized site, government, and law
enforcement personnel, as well as authorized officials of government
agencies, both domestic and foreign.
By using this system, the user consents to such interception, monitoring,
recording, copying, auditing, inspection, and disclosure at the
discretion of such personnel or officials. Unauthorized or improper use
of this system may result in civil and criminal penalties and
administrative or disciplinary action, as appropriate. By continuing to
use this system you indicate your awareness of and consent to these terms
and conditions of use. LOG OFF IMMEDIATELY if you do not agree to the
conditions stated in this warning.
****************************************************************************
次に、設定ファイルを保存して閉じ、ターミナルに次のように入力して ssh を再起動します。
systemctl restart ssh
次にSSHキーを設定します。SSHキーには公開と秘密の2つのペアがあります。公開キーはサーバーに存在し、秘密キーは個人に存在します。誰かが自分の秘密キーを公開キーと一致させることができれば、その人だけがログインできます。さらに、オプションで秘密キーをパスフレーズで保護することもできます。さらに、キーが4096ビット暗号化を使用して生成されると、ブルートフォースで解読することはほぼ不可能です。
ステップ 1 - RSA キー ペアを作成します。
ターミナルに入力
ssh-keygen -t rsa -b 4096
ここではセキュリティを強化するために64ビット暗号化を使用しています
ステップ 2 - キーとパスフレーズを保存します。
画面上の指示に従って、キーを保存する場所を指定します。デフォルトを受け入れることをお勧めします。パスフレーズを選択し、強力なパスフレーズを入力して覚えておいてください。
画面は次のようになります。
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:
+--[ RSA 2048]----+
| .oo. |
| . o.E |
| + . o |
| . = = . |
| = S = . |
| o + = + |
| . o + o . |
| . o |
| |
+-----------------+
ステップ 3 - 公開キーをコピーします。
ターミナルに入力
ssh-copy-id [email protected]
ここで123.45.56.78はサーバーのIPアドレスです
ローカルホストの場合は
ssh-copy-id user@localmachinename
画面はこんな感じです
The authenticity of host '12.34.56.78 (12.34.56.78)' can't be established.
RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts.
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
~/.ssh/authorized_keys
予期しないキーが追加されていないことを確認するためです。
これでインストールは完了です。ログインするには、ターミナルに次のように入力する必要があります。
ssh username@servername
次に、パスフレーズの入力を求められた場合は、それを入力する必要があります。
ここで、opessh サーバーの root ログインを有効にします。Ubuntu ではデフォルトで無効になっているため、まず sudo パスワードを有効にする必要があります。
そのためには、ターミナルに次のように入力します。画面は次のようになります。
sudo passwd
[sudo] password for [username]: [Type your user password and press return]
Type new UNIX password: [Type the root password you want]
Retype new UNIX password: [Retype the root password you chosen before]
passwd: password updated successfully
ここで、/etc/sudoers ファイルを編集する必要があります。
ここではvisudoというエディタを使用します
visudoはsudoesファイルを編集する目的のみであるためです。
Ubuntuでは、デフォルトでは設定ファイルはnanoエディタで開かれます。
変更するには、ターミナルに次のように入力します:
sudo update-alternatives --config editor
次の画面が表示されます。
There are 4 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nano 40 auto mode
1 /bin/ed -100 manual mode
2 /bin/nano 40 manual mode
3 /usr/bin/vim.basic 30 manual mode
4 /usr/bin/vim.tiny 10 manual mode
Press <enter> to keep the current choice[*], or type selection number:
3と入力してEnterキーを押します
次に次のように入力します:
sudo visudo
次の行に移動します
Defaults env_reset
Enterキーを押してください
上に新しい行が作成されます
タイプ:
デフォルト rootpw
Tabキーではなくスペースキーを使用してください
Esc キーを押します --> :x キーを押します --> Enter キーを押します
ターミナルに次のように入力します:
gedit /etc/ssh/sshd_config
次の行に移動します:
PermitRootLogin password-prohibited
それを次のように変更します
PermitRootLogin yes
次の行に移動します:
PasswordAuthentication no
それを次のように変更します
PasswordAuthentication yes
保存して閉じます
次にSSHを再起動します
service ssh restart
ここで、ユーザー root の ssh 公開鍵と秘密鍵のペアを再度生成します。
ターミナルに入力
ssh-keygen -t rsa -b 4096
ステップ 2 - キーとパスフレーズを保存します。
画面上の指示に従って、キーを保存する場所を指定します。今回は新しいペアを作成する必要があるため、デフォルトを受け入れないでください。デフォルトのペアはすでに作成されています。パスフレーズを選択し、強力なパスフレーズを入力して覚えておいてください。
画面は次のようになります。
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa): u-root-id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/demo/.ssh/u-root-id_rsa.
Your public key has been saved in /home/demo/.ssh/u-root-id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:
+--[ RSA 2048]----+
| .oo. |
| . o.E |
| + . o |
| . = = . |
| = S = . |
| o + = + |
| . o + o . |
| . o |
| |
+-----------------+
次にターミナルに次のように入力します:
ssh-copy-id -i u-root-id_rsa.pub root@localmachinename
出力画面には以下が表示されます:
The authenticity of host '12.34.56.78 (12.34.56.78)' can't be established.
RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts.
[email protected]'s password:
では、「ssh」でマシンにログインしてみましょう。[メールアドレス]'" を入力してチェックインします:
~/.ssh/authorized_keys
予期しないキーが追加されていないことを確認するためです。
これで、ログインするための秘密鍵アクセスをルートに許可しました。
テストするには次のように入力します:
ssh root@localmachine
再びターミナルに次のように入力します:
gedit /etc/ssh/sshd_config
次の行に移動します:
PasswordAuthentication yes
それを次のように変更します
PasswordAuthentication no
保存して閉じます
次にSSHを再起動します
service ssh restart
パスフレーズの入力を求められます。パスフレーズは同じままです。入力してください。
これでルートは正常にログインできるようになります
セキュリティを強化するためにファイアウォールを追加する必要があります
タイプ:
apt install ufw
今すぐ始めましょう
enable ufw
現在実行中のプロセスのリストを取得する
ufw app list
OpenSSH がそこにリストされます。
ALow it through firewall
ufw allow OpenSSH
ファイアウォールを再起動する
systemctl restart ufw
インストールが完了しました