1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Linuxオペレーション・メモ

Last updated at Posted at 2018-05-24

はじめに

Linux上で環境整備をする時に必要になるモロモロの操作について備忘録です。
RHEL7.5をベースにしています。
久々に使うと色々忘れちゃうのと、RedHat6系と7系で結構変わってて混乱するので。
都度更新予定。

インストール関連

DVDメディア用のyumリポジトリ設定

/etc/yum.repos.d/にrhel-dvd.repoファイル作成
こんな感じ

/etc/yum.repos.d/rhel-dvd.repo
[rhel-dvd]
name=Red Hat Enterprise Linux 7.5 - x86_64 - DVD
baseurl=file:///run/media/root/RHEL-7.5\ Server.x86_64
enabled=1
gpgcheck=1
gpgkey=file:///run/media/root/RHEL-7.5\ Server.x86_64/RPM-GPG-KEY-redhat-release

リポジトリ確認コマンド: yum repolist

ebabled=1だとデフォルト有効になるので、常にDVDマウントされていないのであれば、enabled=0にしておいてyum実行の度に明示的に有効化するのがよいでしょう。

RHEL V8からは少しisoイメージの構造が変わっているようで、BaseOSとAppStreamという2つのディレクトリに分かれているようなので、こんな感じに定義する必要があります。

/etc/yum.repos.d/rhel-dvd.repo
[rhel-dvd-BaseOS]
name=Red Hat Enterprise Linux 8.2 - x86_64 - BaseOS
baseurl=file:///run/media/root/RHEL-8-2-0-BaseOS-x86_64/BaseOS/
metadata_expire=-1
enabled=1
gpgcheck=1
gpgkey=file:///run/media/root/RHEL-8-2-0-BaseOS-x86_64/RPM-GPG-KEY-redhat-release

[rhel-dvd-AppStream]
name=Red Hat Enterprise Linux 8.2 - x86_64 - AppStream
baseurl=file:///run/media/root/RHEL-8-2-0-BaseOS-x86_64/AppStream
metadata_expire=-1
enabled=1
gpgcheck=1
gpgkey=file:///run/media/root/RHEL-8-2-0-BaseOS-x86_64/RPM-GPG-KEY-redhat-release

isoイメージファイルのマウント

mount -o loop -t iso9660 <iso_image_file> <mount_point>

外付けHDD(NTFS)マウント

Linuxで外付けUSB HDD(NTFS)をマウントする際のメモ

追加パッケージ

コマンド実行しようとして入ってなかった場合、追加するパッケージ
shasumコマンド: perl-Digest-SHA
treeコマンド: tree
virshコマンド: libvirt-client

H/W情報確認

CPU情報
[root@test06 ~]# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 78
model name      : Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
stepping        : 3
microcode       : 0x19
cpu MHz         : 2473.121
cache size      : 6144 KB
physical id     : 0
siblings        : 2
core id         : 0
cpu cores       : 2
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 5
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 pni ssse3 lahf_lm
bogomips        : 4946.24
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 78
model name      : Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
stepping        : 3
microcode       : 0x19
cpu MHz         : 2473.121
cache size      : 6144 KB
physical id     : 0
siblings        : 2
core id         : 1
cpu cores       : 2
apicid          : 1
initial apicid  : 1
fpu             : yes
fpu_exception   : yes
cpuid level     : 5
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 pni ssse3 lahf_lm
bogomips        : 4946.24
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:
Memory情報
[root@test06 ~]# cat /proc/meminfo
MemTotal:        4046204 kB
MemFree:         3257892 kB
MemAvailable:    3404472 kB
Buffers:            3148 kB
Cached:           338416 kB
SwapCached:            0 kB
Active:           350804 kB
Inactive:         272156 kB
Active(anon):     282140 kB
Inactive(anon):     8720 kB
Active(file):      68664 kB
Inactive(file):   263436 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       4063228 kB
SwapFree:        4063228 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        281416 kB
Mapped:            91440 kB
Shmem:              9464 kB
Slab:              72748 kB
SReclaimable:      35232 kB
SUnreclaim:        37516 kB
KernelStack:        5056 kB
PageTables:        22372 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     6086328 kB
Committed_AS:    1959784 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       57928 kB
VmallocChunk:   34359672824 kB
HardwareCorrupted:     0 kB
AnonHugePages:     69632 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       85952 kB
DirectMap2M:     4108288 kB

セキュリティ関連

SELinux

こいつがなかなかやっかい。面倒なのでテスト環境だととりあえず一旦無効化しまうことが多い。

参考:
SELinux ユーザーおよび管理者のガイド
SELinux の状態とモード
「SELinuxのせいで動かない」撲滅ガイド

ステータス確認
[root@test06 ~]# getenforce
Enforcing

[root@test06 ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31
permissiveに変更(一時的)
[root@test06 ~]# setenforce 0

[root@test06 ~]# getenforce
Permissive

[root@test06 ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

上の変更だとOS再起動すると元に戻るので恒久的な変更を行うには、/etc/selinux/configを書き換える必要がある。

/etc/selinux/config
SELINUX=permissive

変更したらOS再起動。

firewalld

firewalldも基本デフォルトで有効になってるようだ。
何か製品追加したりサービス立ち上げたりすると基本通信が抑止されてしまうので、必要に応じて穴を開ける必要がある。

firewalld自体のサービスの制御はsystemctlで実施

firewalldステータス確認
# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 木 2018-05-24 20:05:36 JST; 30min ago
     Docs: man:firewalld(1)
 Main PID: 730 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           mq730 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

 5月 24 20:05:35 test06 systemd[1]: Starting firewalld - dynamic firewall daemon...
 5月 24 20:05:36 test06 systemd[1]: Started firewalld - dynamic firewall daemon.
ゾーンの確認
[root@test06 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources:
  services: ssh dhcpv6-client
  ports: 9200/tcp 9100/tcp 5601/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
9200ポートをpublicゾーンに追加して疎通できるようにする例
[root@test06 /etc/firewalld/zones]# firewall-cmd --zone=public --add-port=9200/tcp --permanent
success

[root@test06 /etc/firewalld/zones]# more public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only s
elected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <port protocol="tcp" port="9200"/>
</zone>

[root@test06 /etc/firewalld/zones]# firewall-cmd --reload
success

最後にreloadして変更を反映する!

ネットワーク関連

構成ツール

CUIのネットワーク構成ツール: nmtuiコマンド

仮想ブリッジ無効化

仮想ブリッジがデフォルトで有効になっていて、それがネットワーク接続上色々問題がある場合がるので、無効化する!
http://www.orangetakam.com/blog/archives/1034

[root@test06 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.52540023ca8d       yes             virbr0-nic

[root@test06 ~]# virsh net-list --all
 名前               状態     自動起動  永続
----------------------------------------------------------
 default              動作中  はい (yes)  はい (yes)

[root@test06 ~]# virsh net-destroy default
ネットワーク default は強制停止されました

[root@test06 ~]# virsh net-autostart default --disable
ネットワーク default の自動起動設定が解除されました


[root@test06 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces

[root@test06 ~]# virsh net-list --all
 名前               状態     自動起動  永続
-------------------------------------------------------

[root@test06 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s3

その他もろもろ

コマンドプロンプトにフルパス表示

/etc/bashrc
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \w]\\$ "

41行目あたりのPS1の指定の中の大文字Wを小文字wに変更

ユーザー

ユーザー作成: useradd
パスワード変更: passwd

各種サービス制御

参考:
systemctl コマンド
はじめてのsystemdサービス管理ガイド
もはやserviceコマンドとかcheckconfigなんて使わないのね...

一覧表示
systemctl list-uinits

サービスのステータス確認、起動、停止など
systemctl status <service_name>.service
systemctl start <service_name>.service
systemctl stop <service_name>.service
systemctl restart <service_name>.service

自動起動設定有効化/無効化
systemctl enable <service_name>.service
systemctl disable <service_name>.service

cron設定確認

cronはユーザー毎に設定される。
cron設定内容確認: crontab -l
cron設定用エディター起動: crontab -u <user> -e

ファイルシステム拡張手順例

参考
http://d.hatena.ne.jp/nuko_yokohama/20150223/1424664122

VM環境でアサインするディスクを増やして、ゲストOS側でファイルシステムを拡張したい!という場合のイメージ。
結構めんどうなのね...

fdiskでパーティション情報更新

[root@rdt03 ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


コマンド (m でヘルプ): p

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000c4a30

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM

コマンド (m でヘルプ): m
コマンドの動作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

コマンド (m でヘルプ): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
パーティション番号 (3,4, default 3):
最初 sector (104857600-419430399, 初期値 104857600):
初期値 104857600 を使います
Last sector, +sectors or +size{K,M,G} (104857600-419430399, 初期値 419430399):
初期値 419430399 を使います
Partition 3 of type Linux and of size 150 GiB is set

コマンド (m でヘルプ): p

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000c4a30

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM
/dev/sda3       104857600   419430399   157286400   83  Linux


コマンド (m でヘルプ): t
パーティション番号 (1-3, default 3):
Hex code (type L to list all codes): L

 0  空              24  NEC DOS         81  Minix / 古い Li bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux スワップ  c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 隠し C: ド c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux 拡張領域  c7  Syrinx
 5  拡張領域        41  PPC PReP Boot   86  NTFS ボリューム da  非 FS データ
 6  FAT16           42  SFS             87  NTFS ボリューム db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux プレーン  de  Dell ユーティリ
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX ブート可能  4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 ブートマネ 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad ハ eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 拡張領域 (L 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC
11  隠し FAT12      56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq 診断     5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  隠し FAT16 <32M 61  SpeedStor       ab  Darwin ブート   f2  DOS セカンダリ
16  隠し FAT16      63  GNU HURD または af  HFS / HFS+      fb  VMware VMFS
17  隠し HPFS/NTFS  64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI スワップ   fd  Linux raid 自動
1b  隠し W95 FAT32  70  DiskSecure Mult bb  隠し Boot Wizar fe  LANstep
1c  隠し W95 FAT32  75  PC/IX           be  Solaris ブート  ff  BBT
1e  隠し W95 FAT16  80  古い Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

コマンド (m でヘルプ): p

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000c4a30

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM
/dev/sda3       104857600   419430399   157286400   8e  Linux LVM

コマンド (m でヘルプ): w
パーティションテーブルは変更されました!

ioctl() を呼び出してパーティションテーブルを再読込みします。

WARNING: Re-reading the partition table failed with error 16: デバイスもしくはリソースがビジー状態です.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
ディスクを同期しています。

リブート

vg, lv確認

[root@rdt03 ~]# vgdisplay -C
  VG   #PV #LV #SN Attr   VSize  VFree
  rhel   1   2   0 wz--n- 49.00g 4.00m
[root@rdt03 ~]# vgextend rhel /dev/sda3
  Physical volume "/dev/sda3" successfully created.
  Volume group "rhel" successfully extended
[root@rdt03 ~]# vgdisplay -C
  VG   #PV #LV #SN Attr   VSize   VFree
  rhel   2   2   0 wz--n- 198.99g 150.00g


[root@rdt03 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                ESKcRi-05X4-7DBB-kHPn-GJA8-AvVx-JZ2rnv
  LV Write Access        read/write
  LV Creation host, time rdt03, 2018-05-17 17:40:50 +0900
  LV Status              available
  # open                 1
  LV Size                45.99 GiB
  Current LE             11774
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                YNcd9f-U2yX-ACpP-QAH6-szN9-K4fE-IHfHti
  LV Write Access        read/write
  LV Creation host, time rdt03, 2018-05-17 17:40:51 +0900
  LV Status              available
  # open                 2
  LV Size                3.00 GiB
  Current LE             768
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

lv拡張!

[root@rdt03 ~]# lvextend -l +100%FREE /dev/rhel/root
  Size of logical volume rhel/root changed from 45.99 GiB (11774 extents) to 195.99 GiB (50174 extents).
  Logical volume rhel/root successfully resized.


[root@rdt03 ~]# lvdisplay /dev/rhel/root
  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                ESKcRi-05X4-7DBB-kHPn-GJA8-AvVx-JZ2rnv
  LV Write Access        read/write
  LV Creation host, time rdt03, 2018-05-17 17:40:50 +0900
  LV Status              available
  # open                 1
  LV Size                195.99 GiB
  Current LE             50174
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

dfで見るとまだサイズ拡張されてない。

[root@rdt03 ~]# df -k
ファイルシス          1K-ブロック     使用   使用可 使用% マウント位置
/dev/mapper/rhel-root    48202756 13026444 35176312   28% /
devtmpfs                  1524716        0  1524716    0% /dev
tmpfs                     1540520       92  1540428    1% /dev/shm
tmpfs                     1540520     9160  1531360    1% /run
tmpfs                     1540520        0  1540520    0% /sys/fs/cgroup
/dev/sda1                 1038336   158208   880128   16% /boot
tmpfs                      308108       12   308096    1% /run/user/0
/dev/sr0                  3704296  3704296        0  100% /run/media/root/RHEL-7.3 Server.x86_64

xfsファイルシステムなのでxfs_growfsというコマンドを叩く必要があるらしい

[root@rdt03 ~]# xfs_growfs /dev/mapper/rhel-root
meta-data=/dev/mapper/rhel-root  isize=512    agcount=4, agsize=3014144 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12056576, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=5887, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12056576 to 51378176

確認

[root@rdt03 ~]# df -k
ファイルシス          1K-ブロック     使用    使用可 使用% マウント位置
/dev/mapper/rhel-root   205489156 13026892 192462264    7% /
devtmpfs                  1524716        0   1524716    0% /dev
tmpfs                     1540520       92   1540428    1% /dev/shm
tmpfs                     1540520     9160   1531360    1% /run
tmpfs                     1540520        0   1540520    0% /sys/fs/cgroup
/dev/sda1                 1038336   158208    880128   16% /boot
tmpfs                      308108       12    308096    1% /run/user/0

サイズ増えた!

サスペンド設定解除

ノートPCの蓋を閉じてもサスペンド(スリープ)にしないように設定変更。

/etc/systemd/logind.confを編集して、以下の設定を追記。

/etc/systemd/logind.conf
...
HandleLidSwitch=ignore
...

その後、systemd-logindサービスを再起動

# systemctl restart systemd-logind.service

sudo設定

通常rootは直接使わないようにしたいので別の管理用ユーザーを作成し、sudo実行できるようにします。
まずユーザーを作成してパスワードを設定します。

[root@zdt01 ~]# useradd taguchi
[root@zdt01 ~]# passwd taguchi
Changing password for user taguchi.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

visudoで以下を編集します。

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

## Same thing without a password
%wheel  ALL=(ALL)       NOPASSWD: ALL

デフォルトだと上の設定が有効になっているので(=rootパスワード入力が必要)、上をコメントアウトして下のコメントアウトをはずします。
これによって、wheelグループはパスワード無しでsudoコマンド実行できるようになります。

上で作ったユーザーにwheelグループを付与します。

[root@zdt01 ~]# usermod -aG wheel taguchi
[root@zdt01 ~]# id taguchi
uid=1000(taguchi) gid=1000(taguchi) groups=1000(taguchi),10(wheel)

これで新たな管理用ユーザーでsudoできるようになります。
(sudo vi /etc/ssh/sshd_config など実行できればOK)

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?