プログラミングとかLinuxとかの備忘録

プログラミング、Linuxでハマった箇所や環境構築のメモ

debian jessieインストール後の設定

リポジトリの設定

リポジトリのリストからcdromを削除する

sudoが使えないのでsuしてから作業を行う

$ su
(root)$ sed -i -e 's/^deb cdrom/# deb cdrom/' /etc/apt/sources.list

一応アップデートしておく

(root)$ aptitude update
(root)$ aptitude -y upgrade

sudoの有効化

sudoのコマンドのインストール

$ su
(root)$ aptitude -y install sudo 

ユーザhogesudoグループに追加し,sudoできるようにする

(root)$ gpasswd -a hoge sudo

ログインし直すとsudoできるようになる.

IPの固定

staticに変更し,IPアドレス等を追加

$ sudo vim /etc/network/interfaces
#iface eth0 inet dhcp
iface eth0 inet static
address         192.168.1.5
network         192.168.1.0
netmask         255.255.255.0
broadcast       192.168.1.255
gateway         192.168.1.1
dns-nameservers 192.168.1.1

$ sudo ifdown eth0 && sudo ifup eth0

chrony

ntpよりchronyの方が高速に時間を同期できるらしい

インストール

$ sudo aptitude -y install chrony

設定ファイルの編集

$ sudo vim /etc/chrony/chrony.conf
#server 0.debian.pool.ntp.org offline minpoll 8
#server 1.debian.pool.ntp.org offline minpoll 8
#server 2.debian.pool.ntp.org offline minpoll 8
#server 3.debian.pool.ntp.org offline minpoll 8
server ntp1.jst.mfeed.ad.jp iburst
server ntp2.jst.mfeed.ad.jp iburst
server ntp3.jst.mfeed.ad.jp iburst

サービス起動

$ sudo systemctl restart chrony
$ sudo systemctl enable chrony

動作状況の確認(*がついているサーバと同期している)

$ chronyc sources
210 Number of sources = 3
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* ntp1.jst.mfeed.ad.jp          2   6    17     2   -847us[ -529us] +/-   81ms
^- ntp2.jst.mfeed.ad.jp          2   6    17     2   -967us[ -967us] +/-   90ms
^+ ntp3.jst.mfeed.ad.jp          2   6    17     2  +1293us[+1611us] +/-  123ms

ファイアウォールのインストール

$ sudo aptitude -y install firewalld

$ sudo systemctl restart firewalld
$ sudo systemctl enable firewalld

[随時更新] Gitの使い方まとめ

インストール

  • yum
$ sudo yum -y install git
  • apt-get
$ sudo apt-get -y install git

初期設定

デフォルトのユーザー名、メールアドレスの指定

$ git config --global user.name "${USER_NAME}"
$ git config --global user.email "${EMAIL}"

Pushのデフォルトの挙動の指定

  • nothing
    毎回ブランチを指定するモード
  • matching
    ローカルとリモートで同じ名前のブランチがあったらすべてpush
  • upstream
    現在のブランチにupstreamが設定されている場合,pushする(upstreamって何?)
  • simple
    upstreamが設定されている,かつ,それが同名ブランチであるときpushする
  • current
    現在のブランチをリモートに同名でpushする

一番事故が少なそうなcurrentをデフォルトにする

$ git config --global push.default current

init

空のディレクトリを作成する

$ mkdir /path/to/project

リポジトリを初期化する

$ cd /path/to/project
$ git init
Initialized empty Git repository in /path/to/project/.git/

リモートリポジトリがある場合は追加する

$ git remote add origin ${リモートリポジトリのURL}

add

$ git add ファイル名        # 特定のファイルをステージに追加
$ git add --all             # 全ての変更をステージに追加(ファイルの削除も反映)
$ git add --ignore-removal  # 全ての変更をステージに追加(削除したファイルを無視する)

commit

git commitでコミットはできるが,コメントも同時に記述したい場合は
以下の様にする

$ git commit -m "${COMMENT}"

複数行のコメントを記述したい場合は以下の用に-mを追加する

$ git commit -m "${COMMENT_LINE1}" -m "${COMMENT_LINE2}"

remote

リモートリポジトリの追加

$ git remote add origin ${リモートリポジトリのアドレス}

現在のリモートURLを確認する

$ git config -l | grep url
remote.origin.url=https://...

リモートURLを変更する

$ git remote set-url origin ${リモートリポジトリのURL}

$ git config -l | grep url
remote.origin.url=git@bitbucket.org:...

push

  • ローカルのNEW_BRANCHをリモートにプッシュする
$ git push -u origin ${BRANCH_NAME}

pull

ローカルの変更を破棄し,リモートで上書き

$ git fetch origin
$ git reset --hard origin/${BRANCH_NAME}

branch

ブランチの一覧を取得

$ git branch -a
master
remotes/origin/HEAD -< origin/master
remotes/origin/master
...

ローカルブランチの作成

$ git branch ${NEW_BRANCH}

ローカルブランチの削除

$ git branch --delete ブランチ名    # マージ済みブランチの削除
$ git branch -D ブランチ名          # マージしたかどうかに関わらず削除

リモートブランチの削除

i$ git push --delete origin ブランチ名

checkout

ブランチの切り替えを行う

$ git checkout ブランチ名       # ローカルに切り替え先のブランチがある場合
$ git checkout -b ブランチ名    # ローカルに新しいブランチを作成し,移動
$ git checkout -b ブランチ名 remotes/origin/ブランチ名 # リモートに切り替え先のブランチがある場合

merge

ローカルブランチ同士をマージする

  1. マージ先のブランチにcheckout
  2. マージ

する.
branch1branch2にマージする場合は

$ git checkout branch2
$ git merge branch1

CentOS7でatdのCPU使用率が高い

状況

KVMホストでCPU使用率が異常に高くなっていたのでtopしてみると
ゲストの1つがCPU200%になっていた

$ top
 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND          
2598 qemu      20   0 2665936 2.072g   9320 S 205.0  6.7   1551:03 qemu-kvm         
2562 qemu      20   0 2820920 1.385g   9640 S   8.3  4.5  65:58.03 qemu-kvm         
2627 qemu      20   0 4743384 1.362g   9096 S   0.7  4.4  65:39.06 qemu-kvm         

そのゲストでは,下のようにatdがCPU40%になっていた

$ top
 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
1069 root      20   0   25844   1056    852 R  40.0  0.1  82:20.66 atd
 476 root      20   0   36820   2032   1716 R  13.3  0.1  25:15.65 systemd-journal
 957 root      20   0  322352   4224   3396 S   3.3  0.2   4:54.21 rsyslogd

このゲストではepgrecを動かしていて,前日にepgrecを新しくしたついでにUIDを変更したのが原因だろうと思われる

解決方法

[CentOS]atdとrsyslogdのCPU使用率が高い - Qiitaの通りに,日時が過ぎているジョブを削除すると治った

$ sudo atq
1311    実行日時 a ユーザ名
1312    実行日時 a ユーザ名
1313    実行日時 a ユーザ名
1315    実行日時 a ユーザ名
1316    実行日時 a ユーザ名
1317    実行日時 a ユーザ名
1318    実行日時 a ユーザ名
1319    実行日時 a ユーザ名
1302    実行日時 = ユーザ名

atdを停止させてから

$ sudo systemctl stop atd

修了しているジョブを削除する

$ sudo at -d ジョブ番号

atdの起動

$ sudo systemctl start atd

CentOS7にNTPクライアントをインストールする

CentOS7にNTPクライアントをインストールする方法
CentOS7にはNTPクラインとが2種類(Chrony, ntp)あるが,基本的にはChronyで問題ないと思う

Chrony

インストール

$ sudo yum -y install chrony

NTPサーバーの編集

/etc/chrony.confの3~6行目をコメントアウトし,その下にntpサーバーを指定

$ sudo vi /etc/chrony.conf
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp1.jst.mfeed.ad.jp iburst
server ntp2.jst.mfeed.ad.jp iburst
server ntp3.jst.mfeed.ad.jp iburst

サービス起動

$ sudo systemctl enable chronyd
$ sudo systemctl start chronyd

しばらくすると,正しい時刻に更新される

$ date
xxxx年 xx月 xx日 x曜日 xx:xx:xx JST

NTP

インストール

$ sudo yum -y install ntp

NTPサーバーの編集

/etc/ntp.confにntpサーバーを指定

$ sudo vi /etc/ntp.conf
# Use public servers from the pool.ntp.org project
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp1.jst.mfeed.ad.jp iburst
server ntp2.jst.mfeed.ad.jp iburst
server ntp3.jst.mfeed.ad.jp iburst

サービス起動

$ sudo systemctl enable ntpd
$ sudo systemctl start ntpd

しばらくすると,正しい時刻に更新される

$ date
xxxx年 xx月 xx日 x曜日 xx:xx:xx JST

KVMでゲストがDNS名前解決できない

エラー内容

CentOS7へのKVMのインストール方法で構築したサーバを再起動するとゲストがDNSでの名前解決を行えなくなった

ホスト (問題なし)

$ ping 192.168.1.1      =< OK
$ ping 8.8.8.8          =< OK
$ ping google.com       =< OK

ゲスト (DNSが動かない)

$ ping 192.168.1.1      =< OK
$ ping 8.8.8.8          =< OK
$ ping google.com       =< OUT

解決方法

[SOLVED] KVM and guest DNS - CentOSの最後に書いてあるとおり,ブリッジにはiptablesのルールを適用しない設定を追加すれば動くようになった

$ sudo vi /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0

$ sudo sysctl -p /etc/sysctl.conf

2.2.10. ソースルーティングの無効化
に,/etc/sysctl.confに追加しておけば永続化されると書いてあるが,再起動しても自動設定されていなかった...

Ubuntu16.04をNISクライアントにする

NISサーバ

CentOS7にNISサーバ,クライアントをインストールする
でセットアップしたNISサーバを使用する

インストール

$ sudo apt-get -y install nis
...
Setting up nis (3.17-34ubuntu3) ...   # ここで,ある程度時間がかかった

設定

$ sudo vim /etc/hosts
NISサーバIP  NISサーバ名

$ sudo vim /etc/yp.conf
domain ドメイン名 server サーバ名

nsswhitch.confの編集

$ sudo vim /etc/nsswitch.conf   # 以下の行にnisを追加する
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat nis
group:          compat nis
shadow:         compat nis
gshadow:        files

hosts:          files mdns4_minimal [NOTFOUND=return] dns nis
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

ログイン時にホームディレクトリがなければ,自動生成するようにする設定を最終行に追加

$ sudo vi /etc/pam.d/common-session
session optional        pam_mkhomedir.so skel=/etc/skel umask=077

サービス起動

$ sudo systemctl start nis
$ sudo systemctl enable nis

これでだけでは,yptestの結果が以下の様になり,ypbindと通信できなかったので

$ yptest
Test 1: domainname
Configured domainname is "ドメイン名"

Test 2: ypbind
Can't communicate with ypbind

[SOLVED] (K)ubuntu 16.04 NIS client does not work after installingに書いてある通りに,以下のコマンドを実行すると正常に動いた

$ sudo systemctl add-wants multi-user.target rpcbind.service

References

CentOS7にNISサーバ,クライアントをインストールする

今回は,サーバ,クライアントともにCentOS7で行った

サーバの設定

インストール

$ sudo yum -y install ypserv rpcbind

ドメイン名の設定

$ ypdomainname ドメイン名

$ sudo vi /etc/sysconfig/network
# Created by anaconda
NISDOMAIN=ドメイン名

アクセス範囲の設定

$ sudo vi /var/yp/securenets
# 255.0.0.0       127.0.0.0
255.255.255.0   192.168.1.0

NISサーバ,クライアントのIPを登録

$ sudo vi /etc/hosts
192.168.1.2     サーバ名
192.168.1.3     client1
192.168.1.4     client2
192.168.2.2     client3

サービスの起動

$ systemctl start rpcbind ypserv ypxfrd yppasswdd
$ systemctl enable rpcbind ypserv ypxfrd yppasswdd

データベースの更新

$ sudo /usr/lib64/yp/ypinit -m

At this point, we have to construct a list of the hosts which will run NIS
servers.  サーバ名 is in the list of NIS server hosts.  Please continue to add
the names for the other hosts, one per line.  When you are done with the
list, type a >control D<.
        next host to add:  サーバ名
        next host to add:                       # コントロール+D
The current list of NIS servers looks like this:

サーバ名

Is this correct?  [y/n: y]  y
We need a few minutes to build the databases...
Building /var/yp/ドメイン名/ypservers...
Running /var/yp/Makefile...
gmake[1]: ディレクトリ `/var/yp/ドメイン名` に入ります
Updating passwd.byname...
Updating passwd.byuid...
Updating group.byname...
Updating group.bygid...
Updating hosts.byname...
Updating hosts.byaddr...
Updating rpc.byname...
Updating rpc.bynumber...
Updating services.byname...
Updating services.byservicename...
Updating netid.byname...
Updating protocols.bynumber...
Updating protocols.byname...
Updating mail.aliases...
gmake[1]: ディレクトリ `/var/yp/ドメイン名` から出ます

サーバ名 has been set up as a NIS master server.

Now you can run ypinit -s サーバ名 on all slave server.

クライアントの設定

インストール

$ sudo yum -y install ypbind rpcbind

SELinuxの無効化

$ sudo vi /etc/selinux/config
SELINUX=disabled

ドメイン名の設定

$ sudo ypdomainname ドメイン名

$ sudo vi /etc/sysconfig/network
# Created by anaconda
NISDOMAIN=ドメイン名

NISサーバ,自身のIPを登録

$ sudo vi /etc/hosts
192.168.1.2    サーバ名
192.168.1.3    自身のホスト名

NISサーバ情報を登録

$ sudo authconfig --enablenis --nisdomain=ドメイン名 --nisserver=サーバ名 --enablemkhomedir --update
getsebool:  SELinux is disabled
getsebool:  SELinux is disabled

NISクライアントの起動

$ sudo systemctl start rpcbind ypbind
$ sudo systemctl enable rpcbind ypbind

NISサーバへのユーザの追加

ユーザを追加

NISサーバ上で普段どおりに追加する

$ sudo useradd ...

NISデータベースへの登録

以下のコマンドでさーばのデータベースを更新し,NISを再起動する

$ cd /var/yp
$ sudo make
$ sudo systemctl restart ypserv ypxfrd yppasswdd

References