【AWS】EC2で作成たいユーザーにsudoできる権限を追加(付与)する

EC2インスタンスを立てた後に、ec2-user以外のユーザーを作成し、作成したユーザーにsudoできるように設定をしておくのは便利で、もしもの時にも役に立ちます。

本日はそんなEC2にsudoできるユーザーを設定する方法を紹介していきます。

そもそもsudoとは

そもそもsudoとはって話になりますが、sudoとはsuperuser(スーパーユーザー)でdo(実行)する、という意味になります。

つまりめちゃくちゃ権限が強いsuperuserのフリをして、実行できるというわけです。

試しに、sudo権限がない状態でsudoコマンドを実行してみます。

[dev@ip-10-1-2-169 .ssh]$ sudo pwd
[sudo] password for dev: 
dev is not in the sudoers file.  This incident will be reported.

このように、sudo権限がないことがわかりました。

設定ファイルを確認する

それではsudoできるユーザーはどこのファイルでどのように管理をされているのでしょうか?

結論から言ってしまうと、/etc/sudoers というファイルの中に設定項目があります。

rootユーザーから、catコマンドで中を見てみましょう。

ちなみに-nオプションをつけることで、catで表示された内容に行数をつけることができます。

/etc/sudoers

[root@ip-10-1-2-169 etc]# cat -n /etc/sudoers
     1	## Sudoers allows particular users to run various commands as
     2	## the root user, without needing the root password.
     3	##
     4	## Examples are provided at the bottom of the file for collections
     5	## of related commands, which can then be delegated out to particular
     6	## users or groups.
     7	## 
     8	## This file must be edited with the 'visudo' command.
     9	
    10	## Host Aliases
    11	## Groups of machines. You may prefer to use hostnames (perhaps using 
    12	## wildcards for entire domains) or IP addresses instead.
    13	# Host_Alias     FILESERVERS = fs1, fs2
    14	# Host_Alias     MAILSERVERS = smtp, smtp2
    15	
    16	## User Aliases
    17	## These aren't often necessary, as you can use regular groups
    18	## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname 
    19	## rather than USERALIAS
    20	# User_Alias ADMINS = jsmith, mikem
    21	
    22	
    23	## Command Aliases
    24	## These are groups of related commands...
    25	
    26	## Networking
    27	# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool
    28	
    29	## Installation and management of software
    30	# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
    31	
    32	## Services
    33	# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable
    34	
    35	## Updating the locate database
    36	# Cmnd_Alias LOCATE = /usr/bin/updatedb
    37	
    38	## Storage
    39	# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount
    40	
    41	## Delegating permissions
    42	# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp 
    43	
    44	## Processes
    45	# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
    46	
    47	## Drivers
    48	# Cmnd_Alias DRIVERS = /sbin/modprobe
    49	
    50	# Defaults specification
    51	
    52	#
    53	# Refuse to run if unable to disable echo on the tty.
    54	#
    55	Defaults   !visiblepw
    56	
    57	#
    58	# Preserving HOME has security implications since many programs
    59	# use it when searching for configuration files. Note that HOME
    60	# is already set when the the env_reset option is enabled, so
    61	# this option is only effective for configurations where either
    62	# env_reset is disabled or HOME is present in the env_keep list.
    63	#
    64	Defaults    always_set_home
    65	Defaults    match_group_by_gid
    66	
    67	# Prior to version 1.8.15, groups listed in sudoers that were not
    68	# found in the system group database were passed to the group
    69	# plugin, if any. Starting with 1.8.15, only groups of the form
    70	# %:group are resolved via the group plugin by default.
    71	# We enable always_query_group_plugin to restore old behavior.
    72	# Disable this option for new behavior.
    73	Defaults    always_query_group_plugin
    74	
    75	Defaults    env_reset
    76	Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
    77	Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
    78	Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
    79	Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
    80	Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
    81	
    82	#
    83	# Adding HOME to env_keep may enable a user to run unrestricted
    84	# commands via sudo.
    85	#
    86	# Defaults   env_keep += "HOME"
    87	
    88	Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
    89	
    90	## Next comes the main part: which users can run what software on 
    91	## which machines (the sudoers file can be shared between multiple
    92	## systems).
    93	## Syntax:
    94	##
    95	## 	user	MACHINE=COMMANDS
    96	##
    97	## The COMMANDS section may have other options added to it.
    98	##
    99	## Allow root to run any commands anywhere 
   100	root	ALL=(ALL) 	ALL
   101	
   102	## Allows members of the 'sys' group to run networking, software, 
   103	## service management apps and more.
   104	# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
   105	
   106	## Allows people in group wheel to run all commands
   107	%wheel	ALL=(ALL)	ALL
   108	
   109	## Same thing without a password
   110	# %wheel	ALL=(ALL)	NOPASSWD: ALL
   111	
   112	## Allows members of the users group to mount and unmount the 
   113	## cdrom as root
   114	# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
   115	
   116	## Allows members of the users group to shutdown this system
   117	# %users  localhost=/sbin/shutdown -h now
   118	
   119	## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
   120	#includedir /etc/sudoers.d

中身はこんな感じになっています。

注目すべきはここです。

   106	## Allows people in group wheel to run all commands
   107	%wheel	ALL=(ALL)	ALL

106行目で、sudoできるグループ管理を説明している行があり、その下の107行目でそのグループを明示的に表示をしています。

107行目のコメントアウトが外れていることを確認できれば大丈夫です。

sudo権限をつける

sudoファイルの設定がわかったところで、sudoできる権限をユーザーにも付与していきましょう。

こちらのコマンドを実行します。

usermodは、ユーザー情報を変更する時のコマンドで、-aオプションはappend(追加)の意味、-Gオプションはgroup(グループ)の意味になります。

[root@ip-10-1-2-169 ~]# usermod -aG wheel <ユーザー名>

今回、私はdevユーザーをwheelグループに追加したいので、以下のようなコマンドになります。

[root@ip-10-1-2-169 ~]# usermod -aG wheel dev

sudo権限を確認する

一通り設定が終了したので、本記事の冒頭で試したsudoを再度実行してみます。

[dev@ip-10-1-2-169 ~]$ sudo pwd
[sudo] password for dev: 
/home/dev

うまく確認できました。

※それでもうまくできない場合は、% systemctl restart sshd で再起動するか、一度サーバーからexitして再度ログインし直して試すことをおすすめします。

最後に

いかがでしたでしょうか。

以上が、「【AWS】EC2で作成たいユーザーにsudoできる権限を追加(付与)する」の紹介記事になります。

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です