CentOS7にpostgresをインストール
CentOS7にpostgresql-9.6をインストールしてクライアント(OSはWin10 DB接続ツールはA5:SQL MK2)からアクセスしてみました。
CentOS7とクライアントは同セグメントです。
rpmパッケージをインストール
wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm rpm -ivh pgdg-centos96-9.6-3.noarch.rpm
続いてyumにて下記をインストール
rpmパッケージインストール後でないと下記のコマンドでは何もインストールされませんでした。
yum -y install postgresql96-server postgresql96-devel postgresql96-contrib
セットアップファイルを検索
find / -name "postgresql*-setup*" -print
下記場所が出力される
/usr/pgsql-9.6/bin/postgresql96-setup
dbinitを実行
/usr/pgsql-9.6/bin/postgresql96-setup initdb
postgresユーザが作成されるので、必要であればパスワード変更。
コンフィグファイルの修正(場所はfindコマンドで検索)
pg_hba.confの編集
IPv4 local connectionsにローカルエリアを追記
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 ident host all all 192.168.0.0/16 ident # IPv6 local connections: host all all ::1/128 ident # Allow replication connections from localhost, by a user with the # replication privilege. #local replication postgres peer #host replication postgres 127.0.0.1/32 ident #host replication postgres ::1/128 ident
postgresql.confの編集
中頃にCONNECTIONS AND AUTHENTICATIONがあるのでリッスンアドレスやポートを設定する
#------------------------------------------------------------------------------ # CONNECTIONS AND AUTHENTICATION #------------------------------------------------------------------------------ # - Connection Settings - listen_addresses = '*' # what IP address(es) to listen on; #listen_addresses = 'localhost' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart) port = 5432 # (change requires restart) max_connections = 100 # (change requires restart) #superuser_reserved_connections = 3 # (change requires restart) #unix_socket_directories = '/var/run/postgresql, /tmp' # comma-separated list of directories # (change requires restart) #unix_socket_group = '' # (change requires restart) #unix_socket_permissions = 0777 # begin with 0 to use octal notation # (change requires restart) #bonjour = off # advertise server via Bonjour # (change requires restart) #bonjour_name = '' # defaults to the computer name # (change requires restart)
起動する。
systemctl start postgresql-9.6
外部から接続出来るようにポートが開いているかどうか確認
テスト環境なのでどこからでもアクセスおk。
netstat -antu Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
ファイアーウォールの許可
firewall-cmd --add-service=postgresql --zone=public --permanent systemctl restart firewalld
postgresの初期設定
# su postgres $ psql
DBが持っているユーザ postgresのパスワードを変更
postgres=# alter role postgres with password 'パスワード';
テスト接続用DBを作成
postgres=# CREATE DATABASE TEST;
クライアントから接続してみます。
接続出来なかったらログを参照。
tail -f /var/lib/pgsql/9.6/data/pg_log/postgresql-Sun.log
< 2017-04-23 13:49:24.699 JST > FATAL: ユーザ"postgres"のパスワード認証に失敗しました < 2017-04-23 13:49:24.699 JST > 詳細: User "postgres" has no password assigned. 接続はpg_hba.confの行83に一致しました: "host all all 192.168.0.0/16 md5"
上で設定したDBが持っているユーザ postgresのパスワード変更を
してないと上記のように出力されていました。
使用するデータベースに接続
postgres=# \connect TEST
テスト用テーブル作成
CREATE TABLE testTbl (No INT NOT NULL, username varchar(100), create_datetime timestamp, PRIMARY KEY(No, username));
テスト用データインサート
insert into testTbl values (1, "テスト", current_timestamp);
クライアントからデータ取得出来るか確認できれば完了です。
lets encryptとお名前.comでドメイン取得
ssl証明書を無料で取得出来るなんて良い時代になりました。
https://letsencrypt.jp/
インストールです。
apacheに設定してやります。
opensslとmod_sslのインストールが済んでいないようならインストールします。
yum install -y openssl mod_ssl
epelリポジトリを有効にして下記コマンドを実行
wget https://dl.eff.org/certbot-auto chmod 700 certbot-auto ./certbot-auto
いよいよ証明書を取得します。
取得用にサーバが起動するようなのでapacheを止めます。
service httpd stop
以下は覚書なのでうまくいかなかったら調べ直す必要があるかもしれません。あってるかな。
ポート80なら↓
./certbot-auto certonly --standalone-supported-challenges http-01 -d ドメイン名
ポート443なら↓
./certbot-auto certonly --standalone-supported-challenges tls-sni-01 -d ドメイン名
mydnsのドメインでは証明書がなかなか発行されないので(1週間で同ドメインには20通まで?)
お名前.comにドメインを取得しcnameを張りました。
更新はwebサーバを止めて下記コマンドで実施することが出来ます。
./letsencrypt-auto renew
CentOS7 IPの固定
毎度毎度調べて設定するIPアドレス
VMWare Player上に構築する際の設定です。
BOOTPROTOをdhcpからstaticに変更
DNS1はVMware Virtual Ethernet Adapter for VMnet8の
アダプタ設定が192.168.31.1となっているのであれば
192.168.31.2を設定。GateWayについても同様に192.168.31.2。
BOOTPROTO=static DNS1="192.168.31.2" DOMAIN=test.com IPADDR=192.168.31.131 NETMASK=255.255.255.0 GATEWAY=192.168.31.2
この書き方でバックアップを作成することが可能です。
cp -p test{,.org}
Linuxサーバに公開鍵認証でsshログイン
クライアントからLinuxサーバにSSHアクセスする方法の一つに鍵認証を用いる方法があります。
この図のように、クライアントに秘密鍵を配布しサーバに公開鍵を配置します。
秘密鍵と公開鍵を作成します。
作成するサーバはどのサーバでも構いません。
(teratermの機能を用いても同様のことが出来ますが今回は割愛します。)
# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx root@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | | | NYAN | |*_* :o | | . + = | | :)S= = o | | o.+ .^o^ | | +.+ + . . | | . o.= = . | | .+.+ . | +-----------------+
Enter passphrase (empty for no passphrase):の箇所で入力するパスワードを
空にすると、ログイン時にパスワードを入力する必要がなくなります。
今回は空にしました。
ここまで終わるとid_rsaとid_rsa.pubの2つのファイルが作成されます。
リナックスサーバに公開鍵を設定します。
ログインしたいユーザのホームディレクトリに.sshディレクトリを作成します。
所有者とグループはそのユーザとし、パーミッションを700とします。
# mkdir ~/.ssh # chown ユーザ:ユーザ ~/.ssh # chmod 700 ~/.ssh
公開鍵ファイルを~/.sshディレクトリに配置し、
authorized_keysファイルに名称を変更します。
パーミッションを600とします。
# mv id_rsa.pub authorized_keys # chmod 600 authorized_keys # rm rsa.pub
既にauthorized_keysが存在するようならcatします。
# cat rsa.pub >> authorized_keys
続いて秘密鍵をクライアントに渡してやります。
一度権限をrw-r--r--くらいにしてやります。
# chmod 644 id_rsa
これでファイルをクライアントに渡してください。
そのファイルをTeraTermでこのように設定すれば、ログイン出来るかと思います。
クライアントがCentOSなどのLinuxの場合は、
~/.ssh/の下にid_rsaを配置。
複数id_rsaがある場合は任意の名前に変更する。
その場合~/.ssh/configファイルを作成し
IdentityFile ~/.ssh/id_rsa IdentityFile ~/.ssh/任意の名前
↑のように列挙していく。
Host test HostName サーバの名前あるいはアドレス User ユーザ名
とするとssh testで接続出来ます。
もしログイン出来ないようなら、サーバの/etc/ssh/sshd_configの
設定を見直します。
下記3行のコメントブロックが外れているかどうか確認してください。
RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
下記のようにしたい場合について
rootユーザのパスワード認証でのログインは拒否
rootユーザの鍵認証でのログインを許可
PermitRootLogin without-password
普段はNoにしているかと思いますがwithout-passwordを指定することで
パスワード以外での認証を許可します。
CentOS7とCentOS6までの違い
今更ながらCentOS7の操作に戸惑っています。
CentOS6までと違うとこを記載していきたいと思います。
ipconfig
↓
ip addr
service XXX start
↓
systemctl start XXX
chkconfig XXX on (off)
↓
systemctl enable(disable) XXX
※chkconfigコマンドはまだ使用することができます。
・サービス名の変更
iptables
↓
firewalld
Firewalldは/etc/firewalld/zones/public.xmlが設定ファイルです。
みたいな感じに追加してsystemctl restart firewalld
まだ随時追記していきます。
ファイルの転送を行うshell(bash)
普段使わないshellを使う、の2回目です。
scpでファイル転送を行うshellを書いてみたんですが、
scp実行時のエラーをログファイルに出力する方法について
ちょっと困ったのでその方法について記載したいと思います。
まずエラー出力をリダイレクトしようと考えました。
command > logfile コマンドの実行結果をファイルに出力する場合 command >> logfile コマンドの実行結果をファイルに追記する場合 command 1>logfile 2>&1 1が標準出力で2が標準エラー出力 標準出力をlogfileに、標準エラー出力を標準出力と同じ場所に出力
上記の方法だとscpでエラーとなった場合に思うように出力してくれない。
空のファイルになってしまう。
なのでこういう風にしてみました。
scp -rp ファイル名 ユーザ名@サーバ名:ディレクトリ if [ $? -eq 1 ]; then echo "Error "サーバ名 > ログファイル fi
$?は特殊変数と呼ばれるもので色々な使われ方をするようなのですが、今回のケースでは
他の言語でいうtry ~ catch exception eのeに当たる部分となるようです。
(非常にざっくりですが)