記事一覧

    Linux CentOS 7 routeを変更するコマンド

    今回は、CentOSでルーティングを変更したいって時に役に立つコマンドと設定方法をご紹介致します。

     ネットワーク172.16.0.0 宛を gw 172.16.0.1 へ向けるルーティング
    route add -net 172.16.0.0 gw 172.16.0.1 netmask 255.255.0.0 eth0

     ホスト172.16.0.10 宛をgw 172.16.0.1 へ向けるルーティング
    route add -host 172.16.0.10 gw 172.16.0.1 eth0

     gw 172.16.0.1 へ向いてるネットワーク172.16.0.0 のルーティングを削除
    route delete -net 172.16.0.0 gw 172.16.0.1 netmask 255.255.0.0 eth0

     gw 172.16.0.1 へ向いてるホスト172.16.0.10 のルーティングを削除
    route delete -host 172.16.0.10 gw 172.16.0.1 eth0

    上記のコマンドはルーティングを変更できるが一時的でありLinuxを再起動すると設定ファイルのルーティング情報が読み込まれるので、permanent設定をするには以下のファイルに設定を行う。
     vi /etc/sysconfig/network-scripts/route-インターフェイス名

    [設定例]/etc/sysconfig/network-scripts/route-インターフェイス名

     

    ADDRESS0=172.16.0.0
    NETMASK0=255.255.0.0
    GATEWAY0=172.16.0.1
    ADDRESS1=172.17.0.0
    NETMASK1=255.255.0.0
    GATEWAY1=172.17.0.1

     

    設定が完了したらnetwork.serviceを再起動
    systemctl restart network

     

    その他、ip route コマンドでもルーティングを変更することができる。

     ネットワーク172.16.0.0 宛を gw 172.16.0.1 へ向けるルーティング
    ip route add 172.16.0.0/16 via 172.16.0.1 dev eth0

     ホスト172.16.0.10 宛をgw 172.16.0.1 へ向けるルーティング
    ip route add 172.16.0.10 via 172.16.0.1 dev eth0

     ネットワーク172.16.0.0 のルーティングを削除
    ip route del 172.16.0.0/16

     ホスト172.16.0.10 のルーティングを削除
    ip route del 172.16.0.10

    /etc/sysconfig/network-scripts/route-インターフェイス名の設定ファイルをviで開いて編集しなくても nmcliコマンドを使えばpermanentの設定ができる。
    nmcli con eth0 +ipv4.routes “172.18.0.0/16 172.18.0.1”

    cat /etc/sysconfig/network-scripts/route-eth0

     

    ADDRESS0=172.16.0.0
    NETMASK0=255.255.0.0
    GATEWAY0=172.16.0.1
    ADDRESS1=172.17.0.0
    NETMASK1=255.255.0.0
    GATEWAY1=172.17.0.1
    ADDRESS3=172.18.0.0
    NETMASK3=255.255.0.0
    GATEWAY3=172.18.0.1

     

    ADDRESS3、NETMASK3、GATEWAY3が新たに追加される。

     

    Linux centOS 7 に外部diskを追加する設定方法

    今回は、Linux centOS 7 に外部diskを追加する方法をご紹介致します。

    [増設内容]

    増設ボリューム /vdb
    パーティション番号 1
    増設パーティション名 /vdb1
    増設したボリュームにマウントするフォルダー /data01

     

    [設定方法]

    1. 事前確認

    ls -l /dev

     

    brw-rw—-. 1 root disk 253, 0 Jul 11 03:27 vda
    brw-rw—-. 1 root disk 253, 1 Jul 11 03:27 vda1
    brw-rw—-. 1 root disk 253, 16 Jul 11 05:36 vdb

     

    この時点では /vdb 配下に /vdb1 が無いが fdisk をすると vdb1 ができる

     

    2. マウントされていないdiskを確認

    fisk でまだマウントされていないdiskを確認する。
    今回は110GBのディスクを増設で既に接続をしているので、/dev/vdb となる。

    fdisk -l(エル)

     

    Disk /dev/vda: 32.2GB 32212254720 bytes, 62914560 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type:dos
    Disk identifier: 0x000b14a4

    Device Boot start end Blocks ID System
    /dev/vda1 * 2048 62910539 31454246 83 Linux

    Disk /dev/vdb: 107.4GB 107374182400 bytes, 209715200 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size(minimum/optimal): 512 bytes / 512 bytes

     

     

    3. fdisk /dev/vdb で “m” を選択しメニューを表示

     

    Command (m for help): m
    Command action
    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)

    Command (m for help):

     

     

    4. “p” のプリントを選択し表示をさせる

     

    Command (m for help): p

    Disk /dev/vdb1: 107.4 GB, 107373133824 bytes, 209713152 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000c4793

    Device Boot Start End Blocks Id System

     

     

    5. “n” を選択して “p” の primary から ”1″ を選択をする。

     

    Command (m for help): n
    Partition type:
    p primary (2 primary, 0 extended, 2 free)
    e extended
    Select (default p): p
    Partition number(1-4, default 1): 1
    First sector(2048-209713151,default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-209713151,default 209713151):
    Using default value 209713151
    Partition 1 of type Linux and of size 100GiB is set

     

     

    6. “w” で書き込む

     

    Command(m for help):w
    The Partition table has been altered!

    Calling ioctl() to re-read Partition table.

    WARNING: Re-reading the partition table failed with error 22: Invalid argument.
    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)
    Syncing disks.

     

     

    7. 確認作業

    fdisk で /dev/vdb1 が作成されたことを確認する。

    fdisk -l(エル)

     

    Disk /dev/vda: 32.2GB 32212254720 bytes, 62914560 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type:dos
    Disk identifier: 0x000b14a4

    Device Boot start end Blocks ID System
    /dev/vda1 * 2048 62910539 31454246 83 Linux

    Disk /dev/vdb: 107.4GB 107374182400 bytes, 209715200 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size(minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x5e1483cc

    Device Boot start end Blocks ID System
    /dev/vdb1 2048 209715199 104856576 83 Linux

     

     

    8. xfsファイルシステムで初期化

    mkfs -t xfs /dev/vdb1
    以下が表示されることを確認する。

     

    meta-data=/dev/vdb1 isize=512 agcount=4 agsize=6553536 blks
         = sectsz=512 attr=2, projid32bit=1
         = crc=1 finobt=0 spinodes=0
    data = bsize=4096 blocks=26214144, imaxpct=25
         = sunit=0 swidth=0 blks
    naming =version 2 bsize=4096 ascii-ci=0 ftype=1
    log =internal log bsize=4096 blocks=12799, version=2
        = sectsz=512 sunit=0 blks, lazy-count=1
    realtime =none extsz=4096 blocks=0, rtextents=0

     

     

    9. /dev/vdb1 が増設されたことを確認

    ls -l /dev

     

    brw-rw—-. 1 root disk 253, 0 Jul 11 03:27 vda
    brw-rw—-. 1 root disk 253, 1 Jul 11 03:27 vda1
    brw-rw—-. 1 root disk 253, 16 Jul 11 05:36 vdb
    brw-rw—-. 1 root disk 253, 17 Jul 11 05:48 vdb1

     

     

    10. /(root) 配下に /data01 のフォルダーを作成する。

    mkdir /data01

     

    11. マウント

    mount /dev/vdb1 /data01

     

    Filesystem  1K-blocks    Used   Available   Use%   Mounted on
    /dev/vda1   31444004   966956  30477048   4%     /
    devtmpfs   1921420    0    1921420   0%    /dev
    tmpfs    1941020    0    1921420    0%    /dev/shm
    tmpfs    1941020   16704   1924316    1%    /run
    tmpfs    1941020    0    1921420    0%    /sys/fs/cgroup
    tmpfs    388204     0    388204    0%    /run/user/0
    /dev/vdb1  104805380  32944  104772436    1%    /data01

     

     

    12. /etc/fstab に追加したディスクを設定

    blkid
    → UUID を確認する。

     

    /dev/vdb1:UUID=”d9c86367-5e26-4f76-9644-e8316b1ffg7a” TYPE=”xfs”

     

     

    UUIDが分かったので/etc/fstab に設定を行う。

     

    UUID=d9c86367-5e26-4f76-9644-e8316b1ffg7a  /data01  xfs  defaults 0 0

     

    これでディスクの増設が完了です!

    httpdデーモンをport#80とは違うpot#8080の別デーモンで起動する設定

    httpd(apache)をport#80とは別にport#8080でもう一つ作りたい時の設定をご紹介致します。

    デーモンを別にすることでメンテナンスや設定の反映、障害対応など起動・停止に伴う影響を最小限にとどめることが出来ます。

     

    [設定内容]

    デーモンネーム httpd-new
    設定フォルダー /etc/httpd-new/
    設定ファイル /etc/httpd-new/conf/httpd.conf
    VirtualHost設定ファイル /etc/httpd-new/conf.d/httpd-new.conf
    Listen port番号 8080
    アクセスログ保存先 /var/log/httpd-new/japan/access_log combined
    エラーログ保存先 /var/log/httpd-new/japan/error_log combined
    DocumentRoot /www/docs/

     

    [設定方法]

    1. serverへログイン

    sudo su –
    cd /etc
    pwd
    → /etc に移動されたことを確認

     

    2. フォルダー “httpd-new” を作成

    ls -l grep ^d | grep httpd*

     

    drwxr-xr-x 5 root root 92 Sep 7 2018 httpd

     

    今回作成するフォルダー “httpd-new” がないことを確認。

    mkdir httpd-new
    ls -l grep ^d | grep httpd*
    フォルダーhttpd-newが作成されたことを確認

     

    drwxr-xr-x 5 root root 92 Sep 7 2018 httpd
    drwxr-xr-x 5 root root 92 Sep 8 2021 httpd-new

     

     

    3. httpd-newへ移動し作業を開始

    cd httpd-new
    pwd
    → httpd-new であることを確認する。

    cp -pir /etc/httpd/conf /etc/httpd-new/
    cp -pir /etc/httpd/conf.d /etc/httpd-new/
    cp -pir /etc/httpd/conf.modules.d /etc/httpd-new/
    ※rオプションは再帰的(recursion)に繰り返すのでサブディレクトリー毎にコピーをしてくれます(^^♪

     

    4. シンボリックリンクの作成

    ln -s /var/log/httpd-new logs
    ln -s ../../usr/lib64/httpd/modules modules
    ln -s /run/httpd run

    ※ln -s リンク先 リンク元(アクセスが来た時の名前)
    シンボリックリンクを張る時は、そのリンク元があるディレクトリーまで移動してからコマンドを投入すること。移動しないと正しくシンボリックリンクが張れない。

     

    5. httpd.conf の編集

    cd /etc/httpd-new/conf/
    pwd
    → /etc/httpd-new/conf であることを確認する。

    httpd.conf バックアップ
    cp -pi /etc/httpd-new/conf/httpd.conf /etc/httpd-new/conf/httpd.conf.20210331

    vi httpd.conf
    ※httpd を httpd-new へ変更、listen port番号を8080へ変更をする。
    ※必要に応じてDirectoryなども変更をする

     

    ServerRoot “/etc/httpd-new”
    Listen 8080

     

     

    6. conf.d の個別設定ファイルhttpd-new.confを作成する。

    cd ../conf.d
    pwd
    → /etc/httpd-new/conf.d であることを確認する。

    vi httpd-new.conf
    #httpd-new

     

    <VirtualHost *:8080>
    ServerAdmin root@httpd-new.co.jp
    DocumentRoot /www/docs/
    ServerName httpd-new.co.jp
    CustomLog /var/log/httpd-new/japan/access_log combined
    ErrorLog /var/log/httpd-new/japan/error_log combined
    ScriptAlias /cgi-bin/ “www/docs/cgi-bin”
    DirectoryIndex index.php index.html index.htm
    <Directory “/www/docs/”>
    Options FollowSymLinks ExecCGI IncludesNOEXEC
    AllowOverride all
    Require all granted
    </Directory>
    </VirtualHost>
    # end httpd-new.com

     

    ※CustomLogとErrorLogのcombined フォーマットは common フォーマットに”リファラー”と”User Agent”が追加されています。

     

     

    7. 必要かどうか検討をするファイル

    autoindex.conf welcomeページやREADMEで表示されるアイコン画像が設定されている。 不要なので消すべし!
    php.conf phpのconfigファイル。必要に応じて設定するべし!
    userdir.conf userのhomeディレクトリへのアクセス設定。個別にconfファイルを作る必要がなし。
    welcome.conf welcomeページなので必要無し

     

    8. sysconfig の設定

    cp -pi /etc/sysconfig/httpd /etc/sysconfig/httpd-new
    ls -l /etc/sysconfig/httpd*
    → httpd-new が作成されたことを確認

    vi /etc/sysconfig/httpd-new

     

    OPTIONS=”-f /etc/httpd-new/conf/httpd.conf”

     

     

    9. httpdコマンドとシンボリックリンク

    ll /usr/sbin/httpd*
    → httpd が存在していることを確認
    cd /usr/sbin
    pwd
    ln -s httpd httpd-new

     

    lrwxrwxrwx 1 root root 5 Apr 17 12:27 httpd-new -> httpd

     

     

    10. ロギングフォルダーの作成

    mkdir -p /var/log/httpd-new/japan/
    ※ -p オプションは、サブディレクトリまで作成する。

     

    11. 起動用httpd-new.serviceの作成

    cd /etc/systemd/system/
    pwd
    ls -l httpd*

     

    -rw-r–r– 1 root 993 5月 17 2019 httpd.service

     

    cp -pi /etc/systemd/system/httpd.service /etc/systemd/system/httpd-new.service
    ls -l
    vi httpd-new.service
    ※3箇所の修正

     

    EnvironmentFile=/etc/sysconfig/httpd-new
    Environment=LANG-C

    ExecStart=/usr/sbin/httpd-new $OPTIONS -DFOREGROUND
    ExecReload=/usr/sbin/httpd-new $OPTIONS -k graceful

     

     

    11. 構文チェック

    httpd-new -t
    ※syntax errorが出ないことを確認する。

     

    12. 動作確認

    プロセスの起動確認・起動
    systemctl status httpd-new
    systemctl start httpd-new
    systemctl status httpd-new
    ps -e | grep httpd-new

    port#8080で起動していることを確認
    netstat -nat | grep 8080

    サービス自動起動設定確認
    systemctl is-enabled httpd-new.service

    サービス自動起動設定
    systemctl enable httpd-new

     

    13. logrotateの設定

    cd /etc/logrotate.d
    pwd
    → /etc/logrotate.dであることを確認
    ll

    vi httpd-new

     

    /var/log/httpd-new/*log /var/log/httpd-new/*/*log {
    daily
    rotate 30
    missingok
    notifempty
    sharedscripts
    compress
    delaycompress
    postrotate
    /bin/systemctl reload httpd-new.service > /dev/null 2 > /dev/null || true
    endscript
    }