Linux Tips

その他の文書

Fedoraは/var/log/messagesが無くなった

journalctlコマンドを使う
tail -f /var/log/messagesの代わりは、journalctl -f

ddでディスクまるごとバックアップ

# dd if=/dev/sda bs=16MB iflag=nocache oflag=nocache,dsync | pv | pigz > /mnt/backup/sda_bak_img.gz 

pvは進捗状態を表示してくれるツール。
pigzは並列処理で圧縮してくれるgzip

CentOSにpvをインストールする

yumでは引っ張ってこれないので、URLを指定してRPMを持ってきてインストールする

  1. 左のPVをクリック
  2. 右上の[rpm64]を右クリックしてリンクをコピー

RPM64の最新版を確認→ Current version: 1.6.20-1 (12 Sep. 2021)

# rpm -ivh http://www.ivarch.com/programs/rpms/pv-1.6.20-1.x86_64.rpm

http://www.ivarch.com/programs/rpms/pv-1.6.20-1.x86_64.rpm を取得中
警告: pv-1.6.20-1.x86_64.rpm: ヘッダー V3 DSA/SHA1 Signature、鍵 ID 3fc56f51: NOKEY
準備しています...              ################################# [100%]
更新中 / インストール中...
  1:pv-1.6.20-1.el6                  ################################# [100%]


$ pv --version
pv 1.6.20 - Copyright 2015 Andrew Wood <andrew.wood@ivarch.com>


Web site: http://www.ivarch.com/programs/pv.shtml

This program is free software, and is being distributed under the
terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

CentOS7にインストールできた。

ストレージからddしてzipする

ddでファイルにダンプしながら、pvで進捗を見ながら、pigzでマルチコア処理で圧縮してgz出力する

dd if=/dev/sdd bs=512 count=12345 | pv | pigz -c > image.gz

ホームディレクトリの隠しファイル(.で始まるファイル)のみをコピーする

$ cd ~
$ find ./ -type f -maxdepth 1 -name '.*' -exec cp -p {} TARGET_DIR/ \;

ttyS0にアクセスできないとき

アクセス権がないだけ。
dialoutグループに所属させるか、デバイスのパーミッションを追加してやる。

S-JISのWebコンテンツが文字化けする

Apacheの設定を追加する。
/etc/httpd/conf.d

firewall

add

# firewall-cmd --add-port=80/tcp --permanent
# firewall-cmd --reload

list

# firewall-cmd --list-all

port

ftp20/tcp 21/tcp
ssh22/tcp
telnet23/tcp
smtp25/tcp
www80/tcp 443/tcp
pop3110/tcp
dns53/udp
dhcp67/udp 68/udp
ntp123/udp
samba137/udp 138/udp 139/tcp 445/tcp
swat901/tcp
RealVNC5900/tcp 5901/tcp 5902/tcp 5903/tcp 5904/tcp 5905/tcp

設定例

firewall-cmd --add-port=20/tcp --permanent
firewall-cmd --add-port=21/tcp --permanent
firewall-cmd --add-port=22/tcp --permanent
firewall-cmd --add-port=23/tcp --permanent
firewall-cmd --add-port=25/tcp --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --add-port=110/tcp --permanent
firewall-cmd --add-port=53/udp --permanent
firewall-cmd --add-port=67/udp --permanent
firewall-cmd --add-port=68/udp --permanent
firewall-cmd --add-port=123/udp --permanent
firewall-cmd --add-port=137/udp --permanent
firewall-cmd --add-port138/udp --permanent
firewall-cmd --add-port=139/tcp --permanent
firewall-cmd --add-port=445/tcp --permanent
firewall-cmd --add-port=901/tcp --permanent
firewall-cmd --add-port=5900/tcp --permanent
firewall-cmd --add-port=5901/tcp --permanent
firewall-cmd --add-port=5902/tcp --permanent
firewall-cmd --add-port=5903/tcp --permanent
firewall-cmd --add-port=5904/tcp --permanent
firewall-cmd --add-port=5905/tcp --permanent
firewall-cmd --reload

VNCは末尾がスクリーン番号なので、基本的に5901だけ開けておけばOK,

https://ja.wikipedia.org/wiki/TCP%E3%82%84UDP%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B%E3%83%9D%E3%83%BC%E3%83%88%E7%95%AA%E5%8F%B7%E3%81%AE%E4%B8%80%E8%A6%A7

tarを使ってオーナーとパーミッションを維持したままコピー(バックアップ)

/mnt/raid以下を全て/mnt/backup以下にコピー

# cd /mnt/raid
# tar -cpvf - * |(cd /mnt/backup/ ; tar xpf -)

USB3.0の扱い

Kernel3.2以降であれば扱える。
CentOS7は3.10なのでOK。

CentOS7でNTFSディスクをマウント

$ sudo yum install epel-release
$ sudo yum install ntfsprogs

ユーティリティ>ディスクでマウントできるようになる

CentOS7でexFATディスクをマウント

$ wget https://forensics.cert.org/cert-forensics-tools-release-el7.rpm
$ sudo rpm -Uvh cert-forensics-tools-release*rpm
$ sudo yum --enablerepo=forensics install exfat-utils

ユーティリティ>ディスクでマウントできるようになる

イメージファイルをマウント

単独パーティションのイメージ

# mount -t ext4 -o loop [イメージファイル] [マウントポイント]

複数パーティションのイメージ

# mount -t ext4 -o loop,offset=[オフセット値] [イメージファイル] [マウントポイント]

オフセット値はfdiskで調べる。

# fdisk -l -u [イメージファイル]

マウントしたいパーティションの開始点×ブロックサイズ(512)の値がオフセット値になる。

VMware上のCentOS7のパーティションを拡張する

LVMで作られているファイルシステムの場合、後でパーティションを追加して領域の拡張ができる。
つまり、パーティションそのもののサイズを変更せずに結合することが可能。

パーティションを拡張する手順

  1. 仮想マシンの設定でハードディスクを「展開...」で容量を増やす
    仮想マシンのHDD容量が増える(50GB→100GBのようにサイズが大きい場合は時間がかかる)
  2. CentOS7を起動して、新しいパーティションを作る
    たとえば/dev/sdaにsda1とsda2ができていたらsda3を作る
    [root@pigmon sawada]# 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: 107.4 GB, 107374182400 bytes, 209715200 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
    Disk label type: dos
    ディスク識別子: 0x000b6f97
    
    デバイス ブート      始点        終点     ブロック   Id  システム
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   104857599    51379200   8e  Linux LVM
    
    コマンド (m でヘルプ): n
    Partition type:
       p   primary (2 primary, 0 extended, 2 free)
       e   extended
    Select (default p): p
    パーティション番号 (3,4, default 3): 3
    最初 sector (104857600-209715199, 初期値 104857600): 
    初期値 104857600 を使います
    Last sector, +sectors or +size{K,M,G} (104857600-209715199, 初期値 209715199): 
    初期値 209715199 を使います
    Partition 3 of type Linux and of size 50 GiB is set
    
    コマンド (m でヘルプ): p
    
    Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
    Disk label type: dos
    ディスク識別子: 0x000b6f97
    
    デバイス ブート      始点        終点     ブロック   Id  システム
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   104857599    51379200   8e  Linux LVM
    /dev/sda3       104857600   209715199    52428800   83  Linux
    
    コマンド (m でヘルプ): t
    パーティション番号 (1-3, default 3): 3
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    コマンド (m でヘルプ): p
    
    Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
    Disk label type: dos
    ディスク識別子: 0x000b6f97
    
    デバイス ブート      始点        終点     ブロック   Id  システム
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   104857599    51379200   8e  Linux LVM
    /dev/sda3       104857600   209715199    52428800   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)
    ディスクを同期しています。
    [root@pigmon sawada]#
  3. いったんリブート
    [root@pigmon sawada]# reboot
  4. 新しく作ったパーティションにPV(Physical Volume)を作成
    [root@pigmon sawada]# pvcreate /dev/sda3
      Physical volume "/dev/sda3" successfully created.
  5. 作成したPVを追加すべきVG(Volume Group)名を確認する
    [root@pigmon sawada]# vgdisplay
      --- Volume group ---
      VG Name               centos
      System ID             
      Format                lvm2
      Metadata Areas        1
      Metadata Sequence No  3
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                2
      Open LV               2
      Max PV                0
      Cur PV                1
      Act PV                1
      VG Size               <49.00 GiB
      PE Size               4.00 MiB
      Total PE              12543
      Alloc PE / Size       12542 / 48.99 GiB
      Free  PE / Size       1 / 4.00 MiB
      VG UUID               is2aM1-Yd8d-9w2i-u3F0-pNOH-SlXp-vfOGcu
  6. VG「centos」に新しく作ったパーティションを追加
    [root@pigmon sawada]# vgextend centos /dev/sda3
      Volume group "centos" successfully extended
  7. VG Sizeが増えたことを確認
    [root@pigmon sawada]# vgdisplay
      --- Volume group ---
      VG Name               centos
      System ID             
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  5
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                2
      Open LV               2
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               98.99 GiB
      PE Size               4.00 MiB
      Total PE              25342
      Alloc PE / Size       25342 / 98.99 GiB
      Free  PE / Size       0 / 0   
      VG UUID               is2aM1-Yd8d-9w2i-u3F0-pNOH-SlXp-vfOGcu
  8. 領域を拡張(/dev/sda3に50GB作ったので、50GB拡張する)
    [root@pigmon sawada]# lvextend -L +50Gb /dev/mapper/centos-root
      Size of logical volume centos/root changed from <45.12 GiB (11550 extents) to <95.12 GiB (24350 extents).
      Logical volume centos/root successfully resized.
    メモ:作成した領域をすべて使う場合は、次のコマンドでも可
    # lvextend -l +100%FREE /dev/mapper/centos-root
  9. ファイルシステムを拡張(XFSでresize2fsが使えないのでxfs_growsを使う)
    [root@pigmon sawada]# xfs_growfs /dev/mapper/centos-root 
    meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=2956800 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0 spinodes=0
    data     =                       bsize=4096   blocks=11827200, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=5775, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 11827200 to 24934400
  10. ファイルシステムが拡張されたかを確認
    [root@pigmon sawada]# df
    ファイルシス            1K-ブロック     使用   使用可 使用% マウント位置
    devtmpfs                    1913668        0  1913668    0% /dev
    tmpfs                       1930648        0  1930648    0% /dev/shm
    tmpfs                       1930648    12768  1917880    1% /run
    tmpfs                       1930648        0  1930648    0% /sys/fs/cgroup
    /dev/mapper/centos-root    99714500 35378716 64335784   36% /
    /dev/sda1                   1038336   406628   631708   40% /boot
    tmpfs                        386132       20   386112    1% /run/user/1000

コンソールのサイズ変更を反映するスクリプト

シリアルコンソール等で端末サイズにエディタとかのサイズが合わないのを直す。

Pythonで書かれたツールが便利

$ wget https://raw.githubusercontent.com/akkana/scripts/master/termsize
$ chmod +x termsize
$ sudo cp termsize /usr/local/bin

使い方

自動ではやってくれないので、TeraTermとかの端末サイズを変えたら実行する。

CentOSのデスクトップアイコンの表示を小さくする

$ gsettings set org.gnome.nautilus.icon-view default-zoom-level small

実行しているファイルを指定してKillするスクリプト

ps aux | grep ファイル名 | grep -v grep | awk '{ print "kill -9", $2 }' | sh

CentOS7でyum updateがエラーして実行できない場合

GUIのシステムツール>ソフトウエアの更新でもエラーが出てダメだった。
以下、対応手順。


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-11-27 (月) 23:39:34