LoginSignup
0
0

More than 3 years have passed since last update.

AWS で ssh with pem ユーザを追加

Posted at
  • ssh するだけのユーザをつくる
  • sudo は許可しない

環境

  • サーバ Ubuntu 14.04 LTS
  • クライアント Ubuntu 20.04 LTS

クライアントで鍵ファイル作成

今回は、鍵ファイルにパスフレーズをつけました。


$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nanbuwks/.ssh/id_rsa): ./hogehoge
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in ./hogehoge.
Your public key has been saved in ./hogehoge.pub.
The key fingerprint is:
SHA256:IVTCsgbteJ/XiZmY2n0v7TcDFgYJ20qFqSIqzPzgmH8 nanbuwks@nanbuwks-ThinkPad-X230
The key's randomart image is:
+---[RSA 3072]----+
|   . .o.o+..     |
|  . o...o+o      |
|   + o..+ ..     |
|  o * .o o  o    |
|+. + o +S= o .   |
|o=    = = o o    |
|+.o  o o   o .   |
|o. .E . . o . +  |
| ...     . +o. o |
+----[SHA256]-----+

秘密鍵と公開鍵ができたことを確認します。


$ ls hogehoge*
hogehoge  hogehoge.pub

秘密鍵の名前を変更しておきます。


$ mv hogehoge hogehoge.pem

サーバでユーザ追加

パスワード無しで追加


$ sudo adduser hogehoge --disabled-password
Adding user `hogehoge' ...
Adding new group `hogehoge' (1007) ...
Adding new user `hogehoge' (1007) with group `hogehoge' ...
The home directory `/home/hogehoge' already exists.  Not copying from `/etc/skel'.
Changing the user information for hogehoge
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] 

サーバで hogehoge ユーザのログオン環境を調整

まず hogehoge ユーザでログオンしてユーザディレクトリなどを作成してもらう


$ sudo su - hogehoge

次に SSH に必要なファイルなどを作成する


$ mkdir .ssh
$ chmod 700 .ssh
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

public キーを登録する

scp などでクライアントからサーバに公開鍵を転送し、登録する
例えば /tmp/hogehoge.pub に転送していたとした場合:


$ cp /tmp/hogehoge.pub .ssh/authorized_keys

クライアントからアクセスしてみる


$ ssh -i hogehoge.pem    hogehoge@www.example.com
Enter passphrase for key 'hogehoge.pem':
.
.
.
hogehoge@example:~$

アクセスできました。うまくいったので、 pem ファイルを秘密の場所に保存し、400 にパーミッションを変更しておきましょう。

0
0
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
0