EC2とLinuxあれこれ
rootのパスワードを設定する方法
// ec2-user でログイン $ chmod 600 sshkey.pem $ ssh -i sshkey.pem ec2-user@xxx.xxx.xxx.xxx // rootユーザにクラスチェンジ $ sudo su - // rootユーザのパスワード変更(例:実際はありえないがテキトーにパスワードはrootにしたとする) # passwd Changing password for user root. New password: root Retype new password: root passwd: all authentication tokens updated successfully. // 一旦ec2-userにもどって、rootで入る # su ec2-user $ su root Password: root
起動したEC2のCPU情報
$ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 63 model name : Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz stepping : 2 microcode : 0x43 cpu MHz : 2400.110 cache size : 30720 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm cpuid_fault invpcid_single pti fsgsbase bmi1 avx2 smep bmi2 erms invpcid xsaveopt bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit bogomips : 4800.14 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management:
システムブートの手順
- 電源ON ↓
- BIOSの起動 ↓
- →ハードウェアの初期化・設定、メモリチェック、起動デバイス(OS or Disc)の設定・チェック、Bootloaderの起動 ↓
- Bootloaderの起動 ↓
- →GRAB or GRAB2を使用、OS(Kernel)とinitdをメモリにロード ↓
- Kernelの起動 ↓
- →OS(Kernel)を初期化、カーネルはメモリを初期化し、システムにアクセスする最小限のファイルinitramfsをマウント。 ↓
- initプロセスの起動 ↓
- →/etc/init.dの中のプログラムを起動 ↓
- 起動
$ dmesg
systemctlコマンド
systemdで制御しているサービスを管理するコマンド
$ systemctl list-unit-files --type=service | head -n20 UNIT FILE STATE acpid.service enabled amazon-ssm-agent.service enabled amzn2-early-relabel-modules.service enabled arp-ethers.service disabled atd.service enabled auditd.service enabled auth-rpcgss-module.service static autovt@.service enabled blk-availability.service disabled brandbot.service static chrony-dnssrv@.service static chrony-wait.service disabled chronyd.service enabled cloud-config.service enabled cloud-final.service enabled cloud-init-local.service enabled cloud-init.service enabled console-getty.service disabled console-shell.service disabled
サービスを起動、停止する。
$ systemctl list-unit-files --type=service | grep post* postfix.service enabled $ systemctl status postfix.service ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since 木 2020-12-24 00:19:37 UTC; 23min ago Process: 2883 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 2880 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 2877 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Main PID: 2958 (master) CGroup: /system.slice/postfix.service ├─2958 /usr/libexec/postfix/master -w ├─2959 pickup -l -t unix -u └─2960 qmgr -l -t unix -u 12月 24 00:19:36 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Starting... 12月 24 00:19:37 ip-10-0-0-101.ap-northeast-1.compute.internal postfix/postfix-script[2956]: ... 12月 24 00:19:37 ip-10-0-0-101.ap-northeast-1.compute.internal postfix/master[2958]: ... 12月 24 00:19:37 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Started ... Hint: Some lines were ellipsized, use -l to show in full. // 停止する $ sudo systemctl stop postfix.service $ systemctl status postfix.service ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: inactive (dead) since 木 2020-12-24 00:44:17 UTC; 2s ago Process: 3397 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS) Process: 2883 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 2880 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 2877 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Main PID: 2958 (code=killed, signal=TERM) 12月 24 00:19:36 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Starting Postfix Mail Tra... 12月 24 00:19:37 ip-10-0-0-101.ap-northeast-1.compute.internal postfix/postfix-script[2956]: startin... 12月 24 00:19:37 ip-10-0-0-101.ap-northeast-1.compute.internal postfix/master[2958]: daemon started ... 12月 24 00:19:37 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Started Postfix Mail Tran... 12月 24 00:44:16 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Stopping Postfix Mail Tra... 12月 24 00:44:16 ip-10-0-0-101.ap-northeast-1.compute.internal postfix/postfix-script[3403]: stoppin... 12月 24 00:44:17 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Stopped Postfix Mail Tran... Hint: Some lines were ellipsized, use -l to show in full. // 起動する $ sudo systemctl start postfix.service $ systemctl status postfix.service ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since 木 2020-12-24 00:45:16 UTC; 1s ago Process: 3397 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS) Process: 3423 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 3420 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 3418 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Main PID: 3496 (master) CGroup: /system.slice/postfix.service ├─3496 /usr/libexec/postfix/master -w ├─3497 pickup -l -t unix -u └─3498 qmgr -l -t unix -u 12月 24 00:45:16 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Starting Postfix Mail Tra... 12月 24 00:45:16 ip-10-0-0-101.ap-northeast-1.compute.internal postfix/postfix-script[3494]: startin... 12月 24 00:45:16 ip-10-0-0-101.ap-northeast-1.compute.internal postfix/master[3496]: daemon started ... 12月 24 00:45:16 ip-10-0-0-101.ap-northeast-1.compute.internal systemd[1]: Started Postfix Mail Tran... Hint: Some lines were ellipsized, use -l to show in full.
例えばhttpdを自動で起動する場合、それを解除する場合。
systemctl enable httpd.service systemctl disable httpd.service
yumコマンド(Redhat系)
- yum check-update:アップデート可能なパッケージを表示
- yum update:指定したパッケージをアップデート
- yum install:指定したパッケージをインストール
- yum remove:指定したパッケージをデリートする
- yum info:指定したパッケージの情報を表示
- yum list:全パッケージを表示
- yum search:指定したパッケージを検索
- yum grouplist:パッケージグループの表示
yum groupinstall:指定したグループをインストール
- /etc/yum.repos.d:yumコマンドで使用されるリポジトリ情報
aptコマンド(Debian系)
- apt-get autoclean:ダウンロードしたパッケージから最新版以外を削除
- apt-get clean:ダウンロードしたパッケージを削除
- apt-get install:パッケージをインストール
- apt-get remove:パッケージを削除
- apt-get update:パッケージリストを最新版にアップデート
- apt-get dist-upgrade:ディストリビューションを最新版にアップグレード
apt-get upgrade:全パッケージを最新版にアップグレード
apt-cache search:指定したパッケージを検索
- apt-cache show:パッケージ情報の表示
- apt-cache showpkg:パッケージに依存したパッケージ一覧を表示
apt-cache depends:パッケージが依存したパッケージ一覧を表示
/etc/apt/source.list:パッケージのダウンロードもとを設定したファイル
環境変数
$ echo $HOME /home/ec2-user $ echo $LANG ja_JP.UTF-8 $ echo $PATH /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin $ echo $PWD / $ echo $USER ec2-user
環境変数の設定。
$ export my_env=/test/test/ $ env XDG_SESSION_ID=2 HOSTNAME=ip-10-0-0-101.ap-northeast-1.compute.internal my_env=/test/test/ # 削除 $ unset my_env
シェル環境
- /etc/profile:ユーザーログイン時に読み込まれるファイル。環境変数の設定など。
- ~/.bash_profile:Bash起動時に呼び出される。ユーザー固有の環境変数などを設定。
- ~/.bash_login:~/.bash_profileが存在しない場合に呼び出される。
- ~/.profile:~/.bash_profileと~/.bash_loginが存在しない場合に呼び出される。
- ~/.bashrc:~/.bash_profileのつぎに呼び出され、環境変数ではない変数の設定
- /etc/.bashrc:~/.bashrcの次に呼び出される。すべてのユーザーの共通設定。
ユーザー管理業務
ユーザー管理系のファイル。
- /etc/passwd:ユーザーの設定情報が記載されているファイル
- /etc/shadow:シャドウ化されたパスワード。rootのみ。
- /etc/group:グループの設定情報が記載されているファイル
ユーザー管理系のコマンド。
- id:ユーザーの情報を確認。
- groupadd:グループを追加。rootのみ。
- groupdel:グループを削除。rootのみ。
- groupmod:グループを修正。rootのみ。
- passwd:ユーザーのパスワードを変更。rootのみ。
- useradd:ユーザーを追加。rootのみ。
- userdel:ユーザーを削除。rootのみ。
- usermod:ユーザーを修正。rootのみ。
$ su root パスワード: # useradd new_user1 # ls /home/ ec2-user new_user1 # grep new_user1 /etc/passwd new_user1:x:1001:1001::/home/new_user1:/bin/bash # grep new_user1 /etc/group new_user1:x:1001: // パスワードがxでシャドウ化 # grep new_user1 /etc/passwd new_user1:x:1001:1001::/home/new_user1:/bin/bash // パスワードが!!でシャドウ化する前に、存在しないことになっている # grep new_user1 /etc/shadow new_user1:!!:18620:0:99999:7::: // パスワードを設定 # passwd new_user1 ユーザー new_user1 のパスワードを変更。(例:実際はありえないがテキトーにパスワードはpassにしたとする) 新しいパスワード:pass 新しいパスワードを再入力してください:pass passwd: すべての認証トークンが正しく更新できました。 // パスワードがシャドウ化されている # grep new_user1 /etc/shadow new_user1:$6$NID【略】bofI/:18620:0:99999:7::: // ログインする。パスワードが必要になる $ su new_user1 パスワード:pass // ec2-userもパスワードがない # grep ec2-user /etc/shadow ec2-user:!!:18619:0:99999:7::: // パスワード設定 # passwd ec2-user ユーザー ec2-user のパスワードを変更。(例:実際はありえないがテキトーにパスワードはpassにしたとする) 新しいパスワード:pass 新しいパスワードを再入力してください:pass # grep ec2-user /etc/shadow ec2-user:$6$B/ij0ZpR【略】A3Fal/R.:18620:0:99999:7::: // ログインする。パスワードが必要になる $ su ec2-user パスワード:
ローカライゼーションと国際化
- /etc/timezone:Debianのみ。タイムゾーンに関する設定。
- /etc/localtime:タイムゾーンに関する設定。/user/share/zoneinfoへのシンボリックリンク。
- /usr/share/zoneinfo:ゾーン情報が格納されているファイル
- TZ:タイムゾーンを指定する。/etc/localtimeよりも優先される。
- LC_*:ロケールについての設定をする環境変数。
- LC_ALL:LCで始まる環境変数を設定できる。
- LANG:文字コードを指定する。
/usr/bin/locale:LC_*、LANGなどの環境変数を表示する。
timedatectl:時刻やタイムゾーンの詳細情報を表示するコマンド
- date:システム日時を表示するコマンド
- iconv:文字コードを変換するコマンド
タイムゾーン関係。
// rootに変更 $ su root パスワード: // EC2のデフォルトはUTC # date 2020年 12月 24日 木曜日 04:53:33 UTC // ゾーン情報に東京がある # ls /usr/share/zoneinfo/Asia/Tokyo /usr/share/zoneinfo/Asia/Tokyo // /etc/localtimeへのシンボリックリンクを設定 # ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime // JSTになる # date 2020年 12月 24日 木曜日 13:55:45 JST // TZでタイムゾーンを指定 # export TZ=America/New_York # date 2020年 12月 23日 水曜日 23:58:46 EST // 東京に戻す # export TZ=Asia/Tokyo # date 2020年 12月 24日 木曜日 13:59:29 JST // タイムゾーンの情報を表示 # timedatectl Warning: Ignoring the TZ variable. Reading the system's time zone setting only. Local time: 木 2020-12-24 14:03:09 JST Universal time: 木 2020-12-24 05:03:09 UTC RTC time: 木 2020-12-24 05:03:09 Time zone: Asia/Tokyo (JST, +0900) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a
文字コード関係。
// 文字の表示を変更 # export LC_ALL=en_US.UTF-8 # date Thu Dec 24 14:01:05 JST 2020 // もとに戻す # export LC_ALL=ja_JP.UTF-8 # date 2020年 12月 24日 木曜日 14:01:21 JST // サンプルファイル作成 # touch sample.txt # echo 'あいうえお' > sample.txt # cat sample.txt あいうえお // 文字コードをUTF8からSJISに変換 # iconv -f UTF8 -t SJIS < sample.txt > sample_sjis.txt # ls sample.txt sample_sjis.txt // 文字化け # cat sample_sjis.txt ?????????? // SJISからUTF8に変換 # iconv -f SJIS -t UTF8 < sample_sjis.txt > sample_utf8.txt # cat sample_utf8.txt あいうえお
ジョブスケジューラ
- /etc/cron.{*}:cronで実行されるタスクを格納
- /etc/cron.d:自分で起動する時間でカスタマイズしたタスクを確認
- /etc/crontab:cronの設定ファイル。-eで編集、-lでタスクを表示。
- /etc/cron.allow:cronを実行できるユーザーを表示。
# サンプルのshファイル作成 $ touch hello.sh $ vim hello.sh $ cat hello.sh #!/bin/bash now=$(date +'%Y%m%d_%H%M%S') echo 'hello_'${now}'_fromCron' > /home/ec2-user/hello_cron_${now}.txt // 実行権限の付与、ここでは777 $ ls -l hello.sh -rw-rw-r-- 1 ec2-user ec2-user 78 12月 24 15:28 hello.sh $ chmod 777 hello.sh $ ls -l hello.sh -rwxrwxrwx 1 ec2-user ec2-user 89 12月 24 15:35 hello.sh // テスト実行し、削除 $ bash hello.sh $ ls hello.sh hello_cron_20201224_151255.txt $ rm hello_cron_20201224_151255.txt $ ls hello.sh // cron設定 $ crontab -e crontab: installing new crontab // タスクの確認、毎分実行 $ crontab -l */1 * * * * /bin/bash /home/ec2-user/hello.sh // 現在時間 $ date 2020年 12月 24日 木曜日 16:04:44 JST // cronでshファイルが実行されている $ ls hello.sh hello_cron_20201224_160501.txt $ cat hello_cron_20201224_160501.txt hello_20201224_160501_fromCron // cronでshファイルが実行されている $ date 2020年 12月 24日 木曜日 16:05:43 JST $ ls hello.sh hello_cron_20201224_160501.txt hello_cron_20201224_160601.txt $ cat hello_cron_20201224_160601.txt hello_20201224_160601_fromCron // cronでshファイルが実行されている $ ls hello.sh hello_cron_20201224_160501.txt hello_cron_20201224_160601.txt hello_cron_20201224_160701.txt hello_cron_20201224_160801.txt
sudo
sudoはsudoersファイルに記載がないとつかえない。
# useradd sample # passwd sample ユーザー sample のパスワードを変更。 新しいパスワード:sample 新しいパスワードを再入力してください:sample passwd: すべての認証トークンが正しく更新できました。 # su sample $ sudo yum install nmap あなたはシステム管理者から通常の講習を受けたはずです。 これは通常、以下の3点に要約されます: #1) 他人のプライバシーを尊重すること。 #2) タイプする前に考えること。 #3) 大いなる力には大いなる責任が伴うこと。 [sudo] sample のパスワード: sample は sudoers ファイル内にありません。この事象は記録・報告されます。
sudoersファイルの変更。
// sampleユーザーからログアウト $ exit // sudoersファイルの権限変更 # ls -l /etc/sudoers -r--r----- 1 root root 4328 3月 13 2020 /etc/sudoers # chmod 640 /etc/sudoers # ls -l /etc/sudoers -rw-r----- 1 root root 4328 3月 13 2020 /etc/sudoers # vim /etc/sudoers // 下記の箇所にsampleの情報を追記 -------------------------------------- ## Allow root to run any commands anywhere root ALL=(ALL) ALL sample ALL=(ALL) ALL -------------------------------------- // sampleに戻って実行 # su sample $ sudo yum install nmap [sudo] sample のパスワード: 読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd amzn2-core | 3.7 kB 00:00:00 amzn2extra-docker | 3.0 kB 00:00:00 依存性の解決をしています --> トランザクションの確認を実行しています。
ちなみにec2-userは最初から実行できる。
$ groups ec2-user ec2-user : ec2-user adm wheel systemd-journal // new_user1をwheelに追加 $ sudo usermod -G wheel new_user1 $ groups new_user1 new_user1 : new_user1 wheel $ getent group wheel wheel:x:10:ec2-user,new_user1 $ su new_user1 パスワード:pass // wheelグループなのでsudoが実行できる $ sudo yum install nmap あなたはシステム管理者から通常の講習を受けたはずです。 これは通常、以下の3点に要約されます: #1) 他人のプライバシーを尊重すること。 #2) タイプする前に考えること。 #3) 大いなる力には大いなる責任が伴うこと。 [sudo] new_user1 のパスワード:pass 読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd amzn2-core | 3.7 kB 00:00:00
IP制限
下記のファイルに記載する。
$ ls /etc/hosts.* /etc/hosts.allow /etc/hosts.deny // 全部不可 $cat /etc/host.deny ALL:ALL // 192.168.0.0からのみアクセス可能 $cat /etc/hosts.allow ALL:192.168.0.0