LoginSignup
0
0

AWSのメンテナンスサーバーを作る(メモ)

Posted at

メンテナンス用サーバを作ろう

コンソール操作をするのをなるべくセキュアにするために。

EC2のインスタンスを作る

接続元を限定したセキュリティグループにする

設定ファイルいじる

ssh -i ~/.ssh/favy-maintenance.pem ubuntu@***.***.***.***
sudo su
vi /etc/ssh/sshd_config
...
#PermitRootLogin prohibit-password ←これコメントアウト(デフォルトでコメントアウトしてあるはず)
...
PubkeyAuthentication yes ←コメントインする
...
PasswordAuthentication no ←本来YesだけどNoに変更させてコメントイン
PermitEmptyPasswords no ←コメントイン
...

接続後ユーザー作成する

ubuntu@ip-***-***-***-***:~$ sudo adduser maintenance

コマンドうつと以下のような対話型が始まる

Adding user `maintenance' ...
Adding new group `maintenance' (1001) ...
Adding new user `maintenance' (1001) with group `maintenance' ...
Creating home directory `/home/maintenance' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for maintenance
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] Y

passwordは適当に作る。あとは空白でもOK。

maintenance君の想定

ログインユーザー と 実際のコンソールコマンド運用するユーザーは別にしたほうがいい。
ログインユーザがバレたらなんでも出来ちゃえるから。
つまりmaintenance君はコマンドをする担当。直接ログインはできないようにする

色々インストールする

ruby install

sudo apt install ruby
sudo gem install bundler

aws cli のインストール

mkdir ~/tmp; cd ~/tmp
curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo mv aws /opt/
cd /opt/aws
sudo ./install

session manager plugin のインストール

cd ~/tmp
curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_arm64/session-manager-plugin.deb" -o "session-manager-plugin.deb"
sudo dpkg -i session-manager-plugin.deb

ssh-ecs 関係ファイルの配置, bundle install

cd /opt
sudo mkdir -p ops-utils/sg-maintenance
cd ops-utils/sg-maintenance/
sudo vi Gemfile
sudo vi Gemfile.lock
sudo vi ssh-ecs
sudo chmod +x ssh-ecs
sudo bundle install
sudo ln -s /opt/ops-utils/sg-maintenance/ssh-ecs /usr/local/bin/

IAMの作成

ecsいじれる系のIAMの調整

  • e.g. ecs-exec-hogehoge を新規に作成
  • 既存のIAMを参考に権限周りはコピー
    • aws:SourceIp 変える必要あり

credentials の配置

ecs-exec-hogehoge の認証情報を発行。

vi ~/.aws/credentials
chmod 600 ~/.aws/credentials

実際のコンソール接続テスト

# メンテサーバに入る
ssh -i ~/.ssh/favy-maintenance.pem ubuntu@***.***.***.***
# メンテ君に切り替え
ubuntu@ip-***-***-***-***:~$ sudo su - maintenance

maintenance@ip-***-***-***-***:~$ ssh ~/.aws/credentials/*** root@***.***.***.***

1回メンテサーバに入って、maintenance君に切り替えて、そいつがcredentialsを使って入りたいサーバへ入る。
いわゆる踏み台サーバ的な感じでのログイン。

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