この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
セッションマネージャーで Linux インスタンスに接続するときに利用するシェルを Bash に変更する設定を試してみます。
ユーザーガイドは次のページが該当します。
設定可能なシェルプロファイルを有効にする - AWS Systems Manager
また、AWS のナレッジセンターにおいても Bash に変更する方法が紹介されています。
シェルを Bash に変更してみる
セッションマネージャーのシェルは、デフォルトでは Bourne Shell (sh
) となります。
sh-4.2$
今回の設定では、シェルを Bash に変更します。下記は Amazon Linux 2 の場合の例です。
[ssm-user@ip-10-0-0-126 ~]$
マネジメントコンソールで設定する
設定はセッションマネージャーの設定から実施します。
デフォルトのシェルを Bash にするためには、Linux shell profile の設定に/bin/bash
を入力します。
セッションマネージャー経由で Amazon Linux 2 にログインしてみます。
[ssm-user@ip-10-0-0-126 bin]$ echo $SHELL
/bin/bash
Bash が起動しています。
Linux shell profile を利用することで、環境変数の設定や作業ディレクトリの移動など任意のコマンドを実行するができます。
例えば、/home/ssm-user/
ディレクトリに移動する場合は次の値を設定します。
/bin/bash
cd /home/ssm-user/
ログイン後のカレントディレクトリが/home/ssm-user/
になります。
[ssm-user@ip-10-0-0-126 ~]$ pwd
/home/ssm-user
Amazon Linux 2 インスタンスのみを利用する環境では、次の値を設定することで ec2-user としてログインすることもできます。
sudo su --login ec2-user
セッションマネージャーでログイン後の状態です。
[ec2-user@ip-10-0-0-126 ~]$ whoami
ec2-user
AWS CLI で設定する
AWS CLI を用いたセッションマネージャーの設定は次のユーザーガイドに記載があります。
Session Manager 設定の作成 (コマンドライン) - AWS Systems Manager
今回は AWS CloudShell から実行します。
まずは、セッションマネージャーの設定ファイルを作成します。
SessionManagerRunShell.json
{
"schemaVersion": "1.0",
"description": "Document to hold regional settings for Session Manager",
"sessionType": "Standard_Stream",
"inputs": {
"s3BucketName": "",
"s3KeyPrefix": "",
"s3EncryptionEnabled": true,
"cloudWatchLogGroupName": "",
"cloudWatchEncryptionEnabled": true,
"idleSessionTimeout": "20",
"maxSessionDuration": "",
"cloudWatchStreamingEnabled": true,
"kmsKeyId": "",
"runAsEnabled": false,
"runAsDefaultUser": "",
"shellProfile": {
"windows": "",
"linux": "/bin/bash"
}
}
}
作成後(または、アップロード後)にcreate-document
コマンドを実行して設定を反映します。
aws ssm create-document \
--name SSM-SessionManagerRunShell \
--content "file://SessionManagerRunShell.json" \
--document-type "Session" \
--document-format JSON
実行結果です。
$ aws ssm create-document --name SSM-SessionManagerRunShell --content "file://SessionManagerRunShell.json" --document-type "Session" --document-format JSON
{
"DocumentDescription": {
"Hash": "7229c3fa50b40a3af0a98d6c45f32169f0d740b67e1895163630d9e5ef1e3c2b",
"HashType": "Sha256",
"Name": "SSM-SessionManagerRunShell",
"Owner": "111122223333",
"CreatedDate": "2022-11-07T03:43:30.983000+00:00",
"Status": "Creating",
"DocumentVersion": "1",
"PlatformTypes": [
"Windows",
"Linux",
"MacOS"
],
"DocumentType": "Session",
"SchemaVersion": "1.0",
"LatestVersion": "1",
"DefaultVersion": "1",
"DocumentFormat": "JSON",
"Tags": []
}
}
なお、下記エラーが出力される場合は既にSSM-SessionManagerRunShell
ドキュメントが存在している状態です
$ aws ssm create-document --name SSM-SessionManagerRunShell --content "file://SessionManagerRunShell.json" --document-type "Session" --document-format JSON
An error occurred (DocumentAlreadyExists) when calling the CreateDocument operation: Document with same name SSM-SessionManagerRunShell already exists
delete-document
で削除してから作成するか、次のようにupdate-document
を利用します。
$ aws ssm update-document --name SSM-SessionManagerRunShell --content "file://SessionManagerRunShell.json" --document-version \$LATEST
{
"DocumentDescription": {
"Hash": "f97d18ce2cca7f64b79138baf20897c88d50c7ad101c953114472f1f0aa64029",
"HashType": "Sha256",
"Name": "SSM-SessionManagerRunShell",
"Owner": "111122223333",
"CreatedDate": "2022-11-07T04:05:31.740000+00:00",
"Status": "Updating",
"DocumentVersion": "2",
"PlatformTypes": [
"Windows",
"Linux",
"MacOS"
],
"DocumentType": "Session",
"SchemaVersion": "1.0",
"LatestVersion": "2",
"DefaultVersion": "1",
"DocumentFormat": "JSON",
"Tags": []
}
}
以上で設定は終了です。
さいごに
セッションマネージャで利用するシェルを Bash に変更する方法を試してみました。
セッションマネージャーの設定については次のブログでも紹介されており、最大セッション期間の変更もできます。
以上、この記事がどなたかのご参考になれば幸いです。