1
0

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 3 years have passed since last update.

EC2インスタンスでLinuxユーザーを作成し、sudo権限を付与する

Last updated at Posted at 2022-03-05

内容

・EC2インスタンスにログインし、homeディレクトリにLinuxユーザーを追加しsudo権限を付与する方法。
・SSHの際に毎回コマンドが面倒くさいので、作成したLinuxユーザーの情報で~/.ssh/configファイルを設定する

目次

サーバーにユーザーを作成

# EC2上で
sudo su -
useradd -m <ユーザー名> # -mオプション:/homeディレクトリにユーザーのディレクトリの作成も行う
passwd <ユーザー名> # ユーザーのパスワード変更

# SSH秘密鍵の登録
cd /home/<ユーザー名>
mkdir .ssh
vim authorized_keys # 公開鍵の内容を記述、保存
chown <ユーザー名>:<グールプ名> authorized_keys
chmod 600 authorized_keys

# sudo権限の付与
visudo # /etc/sudoers.tmp のファイルを編集(sudo権限の設定)するコマンド
<ユーザー名> ALL=(ALL) ALL

# EC2を抜けて、接続確認
ssh -i ~/.ssh/<ログインするユーザーの秘密鍵> <ユーザー名>@<ip>
# sudoの確認
sudo su -

補足)sudoersファイルの内容

<ユーザー名> ALL=(ALL) ALL
ユーザー ホスト=(誰々として) コマンドパス

例えば

snow tty7=(vagrant) /usr/sbin/useradd
「ホストtty7からアクセスしたユーザーsnowは、vagrantとして/usr/sbin/useraddコマンドをsudoで実行できる」

SSH接続情報を登録

~/.ssh/config
Host <任意の接続先名>
  HostName     <ssh接続先のIP(ドメイン)>
  Port         22
  User         <ユーザー名>
  IdentityFile ~/.ssh/秘密鍵ファイル

これでSSHする時のコマンドが以下のように変わる

ssh -i ~/.ssh/<ログインするユーザーの秘密鍵> <ユーザー名>@<ip>
↓↓↓
ssh <任意の接続先名>

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?