1.はじめに
- 目的
Linux環境においてユーザの管理をすることが多いので備忘として対応した内容を記載する
2.前提条件・目標
- 使用サーバ
- Amazon Linux2023:1台
- 目標
- パスワードでのログインを可能な状態にする
- 特定のグループ所属のユーザのみログインできるようにする
- パスワード認証を不可にして公開鍵認証のみとする
3.設定を実施
3.1パスワードログインができるように設定を変更
- もともとの設定を確認
# ll
total 592
-rw-r--r--. 1 root root 578094 Jun 5 10:32 moduli
-rw-r--r--. 1 root root 1921 Jun 5 10:32 ssh_config
drwxr-xr-x. 2 root root 28 Nov 4 02:08 ssh_config.d
-rw-r-----. 1 root ssh_keys 557 Nov 6 03:12 ssh_host_ecdsa_key
-rw-r--r--. 1 root root 214 Nov 6 03:12 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys 452 Nov 6 03:12 ssh_host_ed25519_key
-rw-r--r--. 1 root root 134 Nov 6 03:12 ssh_host_ed25519_key.pub
-rw-------. 1 root root 3855 Nov 6 03:16 sshd_config
drwx------. 2 root root 28 Nov 4 02:08 sshd_config.d
#
#
#
# cat sshd_config
# $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# To modify the system-wide sshd configuration, create a *.conf file under
# /etc/ssh/sshd_config.d/ which will be automatically included below
Include /etc/ssh/sshd_config.d/*.conf
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# Explicitly disable PasswordAuthentication. By presetting it, we
# avoid the cloud-init set_passwords module modifying sshd_config and
# restarting sshd in the default instance launch configuration.
PasswordAuthentication no
PermitEmptyPasswords no
# Change to no to disable s/key passwords
#KbdInteractiveAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Fedora and may cause several
# problems.
#UsePAM no
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
AuthorizedKeysCommand /opt/aws/bin/eic_run_authorized_keys %u %f
AuthorizedKeysCommandUser ec2-instance-connect
#
- 現時点だとパスワードログインができないので一部設定を変更してsshdを再起動する
# cat sshd_config | grep "PasswordAuthentication yes"
PasswordAuthentication yes
#
#
# systemctl restart sshd
#
- パスワードでログインできるようになった
3.2特定のグループのみログインできるように設定を変更
- SSHログインを許可するグループを作成
# groupadd sshusers
#
#
# cat /etc/group | grep sshusers
sshusers:x:1001:
#
- SSH可能ユーザの作成
ログイン可能ユーザにはsshusersのグループを追加する
# adduser tanaka-tarou
#
#
# id tanaka-tarou
uid=1001(tanaka-tarou) gid=1002(tanaka-tarou) groups=1002(tanaka-tarou)
#
#
# usermod -aG sshusers tanaka-tarou
#
#
# id tanaka-tarou
uid=1001(tanaka-tarou) gid=1002(tanaka-tarou) groups=1002(tanaka-tarou),1001(sshusers)
#
#
# ll /home/
total 0
drwx------. 3 ec2-user ec2-user 95 Nov 6 03:17 ec2-user
drwx------. 2 tanaka-tarou tanaka-tarou 62 Nov 6 03:35 tanaka-tarou
#
- SSH不可ユーザの作成
グループに属していないユーザがログインできないことを確認する用のユーザを作成する
# adduser satou-jirou
#
#
# id satou-jirou
uid=1002(satou-jirou) gid=1003(satou-jirou) groups=1003(satou-jirou)
#
#
# ll /home/
total 0
drwx------. 3 ec2-user ec2-user 95 Nov 6 03:17 ec2-user
drwx------. 2 satou-jirou satou-jirou 62 Nov 6 03:37 satou-jirou
drwx------. 2 tanaka-tarou tanaka-tarou 62 Nov 6 03:35 tanaka-tarou
#
- パスワードの設定をする
# passwd tanaka-tarou
Changing password for user tanaka-tarou.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
#
#
# passwd satou-jirou
Changing password for user satou-jirou.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
#
- 設定ファイルに以下を追加してsshdを再起動する
# cat sshd_config | grep AllowGroups
AllowGroups sshusers
#
#
# systemctl restart sshd
#
-
パスワードでのログインができた
-
次にSSH許可していないユーザでログインを試みる
4.公開鍵認証への移行
-
先に設定したパスワード認証は手軽ですが、実務で公開鍵認証が必須とされることが多いため公開鍵の設定についても備忘として記載する
-
コマンドプロンプトで鍵の生成を行う
C:\Users\User>ssh-keygen -t rsa -b 4096
$ mkdir -p .ssh
$
$
$ chmod 700 .ssh
$
$
$ vi .ssh/authorized_keys
$
$
$ chmod 600 .ssh/authorized_keys
$
$
$ ls -al
total 16
drwx------. 3 tanaka-tarou tanaka-tarou 90 Nov 6 04:04 .
drwxr-xr-x. 5 root root 61 Nov 6 03:37 ..
-rw-r--r--. 1 tanaka-tarou tanaka-tarou 18 Jan 28 2023 .bash_logout
-rw-r--r--. 1 tanaka-tarou tanaka-tarou 141 Jan 28 2023 .bash_profile
-rw-r--r--. 1 tanaka-tarou tanaka-tarou 492 Jan 28 2023 .bashrc
drwx------. 2 tanaka-tarou tanaka-tarou 29 Nov 6 04:04 .ssh
-rw-------. 1 tanaka-tarou tanaka-tarou 869 Nov 6 04:04 .viminfo
$
$
$ ls -al .ssh/
total 4
drwx------. 2 tanaka-tarou tanaka-tarou 29 Nov 6 04:04 .
drwx------. 3 tanaka-tarou tanaka-tarou 90 Nov 6 04:04 ..
-rw-------. 1 tanaka-tarou tanaka-tarou 740 Nov 6 04:04 authorized_keys
$
- パスワード認証の無効化しsshdを再起動する
# vi sshd_config
#
#
# cat sshd_config | grep "PasswordAuthentication no"
PasswordAuthentication no
#
#
# systemctl restart sshd
#
5.まとめと運用上の備忘
- 本記事では、LinuxサーバーにおけるSSHアクセス制御の基礎として、AllowGroupsによるグループ単位のログイン制限を検証しました。
- さらに、現場の運用要件に柔軟に対応するため、パスワード認証と公開鍵認証の両方を設定・検証する流れを整理しました。
- グループ制限の機能
- AllowGroupsは、アクセス権限を持つユーザを明確に限定するための基本的な手段であり、ユーザ管理をグループ単位で行うことで、柔軟な権限付与が可能になる。
- 認証方式の切り替え
- sshd_configのPasswordAuthenticationの設定(yes/no)により、現場のポリシーに応じて認証方式を柔軟に切り替えることができる。









