0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Linux ubuntu SSH公開鍵設定時の操作 メモ

Last updated at Posted at 2021-08-14

秘密鍵と公開鍵

項目 説明 備考
公開鍵 サーバーに設置する データを暗号化するための鍵。
秘密鍵 自分の端末で持っておく 公開鍵で暗号化されたデータを復号化するための鍵

RLoginで秘密鍵と公開鍵の作成

RLogin:Windows用のターミナルソフト
ターミナルソフト:ネットワークを通じて他のコンピュータに接続し、コマンドライン方式で遠隔操作するためのソフトウェア。

→Macは不要(ターミナルからログイン可能)

https://knowledge.sakura.ad.jp/3543/
https://soji256.hatenablog.jp/entry/2021/03/04/070000

sshとsshdの違い

https://qiita.com/pocket8137/items/e504fa2481d5b9f0c064
 

ユーザーの追加

adduser user_name

sudo グループに追加

gpasswd -a user_name sudo

参考
https://qiita.com/white_aspara25/items/c1b9d02310b4731bfbaa
 

rootユーザーへ昇格 / 元のユーザーに戻る【Ubuntu】

rootユーザーへ昇格

sudo su -
#  password

元のユーザーに戻る

exit

vim

鍵ディレクトリの作成と権限変更

一般ユーザーのIDでログインしたあと、ホームディレクトリ(ログイン直後のディレクトリ)で以下のコマンドを実行

mkdir .ssh
chmod 700 .ssh
cd .ssh

コマンドのオプション

引数 内容
-v コマンド実行結果を表示 chmod -vR 700 .ssh
-c 変更があった場合のみ、実行結果を表示
-R 再帰的に変更 chmod -R 700 .ssh
コピーした公開鍵を張り付ける
vim authorized_keys
chmod 600 .ssh/authorized_keys

鍵の権限変更(change mode)

パーミッション の変更
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

権限の区分

自分の権限 グループユーザーの権限 その他ユーザーの権限 対象
# chmod 700 .ssh (7) rwx (0) --- (0) --- .ssh
# chmod 600 .ssh/authorized_keys (6) rw- (0) --- (0) --- .ssh/authorized_keys

権限の種類

r w x -
読み込み(read) 書き込み(write) 実行(execute) 無し
0 1 2 3 4 5 6 7
--- --x -w- -wx r-- r-x rw- rwx

権限確認

ll  もしくは  ls -l

パーミッション

参考
https://qiita.com/ntkgcj/items/6450e25c5564ccaa1b95

一般ユーザーのアクセスを制限する

ファイルのバックアップ / 書き換え

sshd_config ファイルのバックアップ

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.old

sshd_config ファイルの書き換え

sudo vi /etc/ssh/sshd_config

  • 変更箇所
    【注意】"# "はコメントなので、外す。
  • PermitRootLogin no
  • PubkeyAuthentication yes
  • PermitEmptyPasswords no
  • AllowUsers

PermitRootLogin (rootユーザーのログイン許可・拒否)

説明
yes rootユーザのログインを許可
no rootユーザのログインを拒否
prohibit-password 初期値 ≒ without-password
without-password パスワードを使用したrootユーザのログインを拒否
forced-commands-only rootユーザの直接ログインを拒否するが、root権限を使うコマンドのアクセスは許可
その他 パラメータ 説明
PubkeyAuthentication 公開鍵認証の許可・拒否 yes/no
PasswordAuthentication パスワード認証の許可・拒否 yes/no
PermitEmptyPasswords パスワードの空文字許可・拒否 yes/no
AllowUsers ログイン可能ユーザー設定 yes/no
#  AllowUsers user ユーザー名@192.168.1.10

ログファイル確認(参考までに)

sudo vi /var/log/auth.log

参考
https://www.server-memo.net/server-setting/ssh/allow-users.html

参考
https://qiita.com/ine1127/items/b50b9a8f831736cf14ea

設定の反映

sudo systemctl restart sshd

Ubuntuの再起動

sudo reboot

注意点

SSH公開鍵をきちんと間違えずに設定しても、さくらVPSのVNCコンソールからパスワードでログインできてしまうが、これは問題ない。
→SSH設定とコンソールからのログインは無関係。

参考
https://qiita.com/YuichiTanaka007/questions/32e3f70f4210f4c69b4e
https://teratail.com/questions/183493

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?