顯示具有 Linux 標籤的文章。 顯示所有文章
顯示具有 Linux 標籤的文章。 顯示所有文章

2019年3月22日 星期五

在 CentOS 7 上安裝設定 NTP


在安裝伺服器時,尤其是分散式系統,非常需要使用NTP來校時,避免各伺服器上的時間不同步
設定NTP
yum install ntp
編輯/etc/ntp.conf

將原有的NTP伺服器註解,並加入下列伺服器
server      tock.stdtime.gov.tw
server      time.stdtime.gov.tw
server      tick.stdtime.gov.tw
server      watch.stdtime.gov.tw
server      clock.stdtime.gov.tw

如果要指向公司內部的Time Server(底下IP為舉例說明)
restrict    192.168.1.2
server      192.168.1.2

啟動ntpd服務

systemctl start ntpd
檢查ntpd服務的狀態
systemctl status ntpd
設定開機自動啟動ntpd服務
systemctl enable ntpd
更新校正時間後,可以透過下列指令將時間寫入BIOS
hwclock -w




2019年1月2日 星期三

Ubuntu 變更時區


執行下列指令

sudo dpkg-reconfigure tzdata

選擇自己所屬的地區,這裡我是選擇Asia/Taipei

可透過timedatectl指令查看時區及時間是否正確


2018年12月22日 星期六

在 CentOS 7 上更新 Kernel


因為安全性,或者某些套件需要更高的Kernel版本時,需要將Kernel升級

1. 將目前系統所有套件升級

yum update -y
2. 透過ELRepo下載最新穩定的 Kernel 版本
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
下載後再執行一次將所以套件升級
yum update -y
備註:最新的Kernel可以到這個網址查看
http://elrepo.org/tiki/tiki-index.php
5. 安裝Kernel
yum  --enablerepo=elrepo-kernel install kernel-ml
6. 將最新安裝Kernel的啟動順序設為預設後重新開機
grub2-set-default 0
reboot
7. 將GRUB中的第一個Kernel作為默認的Kernel
vi /etc/default/grub
第一行新增內容
GRUB_DEFAULT=0
存檔
8. 重新配置 Kernel
grub2-mkconfig -o /boot/grub2/grub.cfg
9. 查看Kernel的啟動順序
awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
10. 重新開機後驗證最新的Kernel作為預設的Kernel
reboot
uname -sr

參考文件:

CentOS7 更换内核






2018年12月18日 星期二

在 CentOS 7 上開啟 BBR 功能


作業系統:CentOS 7
Linux Kernel版本:4.9以上
備註:如果Linux Kernel版本不是4.9以上,請先參考這篇做升級動作

開啟BBR功能
echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf sudo
sysctl -p
確認BBR功能是否已開啟
lsmod | grep bbr
如果BBR功能已開啟,會看到類似下列訊息
tcp_bbr       20480   2





在 CentOS 7 上安裝 LiberNMS - 使用 Nginx


LibreNMS 1.49已規定PHP最小的版本為7.1.3,下列文章做部分修改(2019/03/15



作業系統:CentOS 7
PHP版本:PHP 7.1.3以上

安裝需要的套件

yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install composer cronie fping git ImageMagick jwhois mtr nginx MySQL-python net-snmp net-snmp-utils nmap python-memcached unzip php72w php72w-cli php72w-common php72w-curl php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-process php72w-snmp php72w-xml php72w-zip php72w-ldap rrdtool mariadb mariadb-server
備註:如要用最新的MariaDBRRDTool版本取代原有Repository提供的舊版本,可以參考這裡MariaDB)及這裡RRDTool
備註:如果已經手動安裝RRDToolMariaDB,可以把上述套件從安裝的套件中移除(橘色部分)不要安裝
備註:不要將PHP 7.2升級到PHP 7.3,因為現有composer套件需要PHP 7.2
新增LiberNMS使用者帳戶
useradd librenms -d /opt/librenms -M -r
usermod -a -G librenms nginx
下載LiberNMS
cd /opt
composer create-project --no-dev --keep-vcs librenms/librenms librenms dev-master
備註:因為是使用root帳號,所以在安裝過程中會遇到警告不要使用root帳號的訊息,但不影響後續安裝
2018-12-19_105750
設定MySQL
systemctl start mariadb
mysql -u root
執行SQL指令建立使用者帳號及密碼
CREATE DATABASE librenms CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'librenms'@'localhost' IDENTIFIED BY '設定密碼';
GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost'; FLUSH PRIVILEGES;
exit
編輯MySQL設定檔
vi /etc/my.cnf
備註:MariaDB 10.x版本是編輯 /etc/my.cnf.d/server.cnf
[mysqld]區塊中新增
innodb_file_per_table=1
lower_case_table_names=0
重新啟動MySQL
systemctl enable mariadb
systemctl restart mariadb
設定並啟動PHP-FPM
修改時區
vi /etc/php.ini
找到[Date]新增
date.timezone = Asia/Taipei
設定網頁設定檔
vi /etc/php-fpm.d/www.conf
變更內容為
;user = apache
user = nginx

group = apache   ; keep group as apache
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php7.2-fpm.sock

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

重新啟動php-fpm服務
systemctl enable php-fpm
systemctl restart php-fpm
設定NGINX
新增設定檔librenms.conf
vi /etc/nginx/conf.d/librenms.conf
新增內容
server {
  listen      80;
  server_name <輸入伺服器名稱>;
  root        /opt/librenms/html;
  index       index.php;

charset utf-8;
  gzip on;
  gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
  location / {
   try_files $uri $uri/ /index.php?$query_string;
  }
  location /api/v0 {
   try_files $uri $uri/ /api_v0.php?$query_string;
  }
  location ~ \.php {
   include fastcgi.conf;
   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_pass unix:/var/run/php-fpm/php7.2-fpm.sock;
  }
  location ~ /\.ht {
   deny all;
  }
}

重新啟動NGINX服務
systemctl enable nginx
systemctl restart nginx
設定SELinux
安裝SELinuxPolicy Tool
yum install policycoreutils-python
執行下列指令配置LibreNMS需要的內容
semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/logs(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/logs(/.*)?'
restorecon -RFvv /opt/librenms/logs/
semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/rrd(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/rrd(/.*)?'
restorecon -RFvv /opt/librenms/rrd/
semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/storage(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/storage(/.*)?'
restorecon -RFvv /opt/librenms/storage/
semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/bootstrap/cache(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/bootstrap/cache(/.*)?'
restorecon -RFvv /opt/librenms/bootstrap/cache/
setsebool -P httpd_can_sendmail=1
setsebool -P httpd_execmem 1

新增fping模組
在任何一個路徑的資料夾下新增http_fping.tt檔案,並新增下列內容
module http_fping 1.0;
require {
type httpd_t;
class capability net_raw;
class rawip_socket { getopt create setopt write read };
}

#============= httpd_t ==============
allow httpd_t self:capability net_raw;
allow httpd_t self:rawip_socket { getopt create setopt write read };

產生檔案後執行下列指令
checkmodule -M -m -o http_fping.mod http_fping.tt
semodule_package -o http_fping.pp -m http_fping.mod
semodule -i http_fping.pp
設定防火牆規則
firewall-cmd --zone public --add-service http
firewall-cmd --permanent --zone public --add-service http
firewall-cmd --zone public --add-service https
firewall-cmd --permanent --zone public --add-service https
設定SNMP
librenmssnmp範例取代原有的snmp設定檔
cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf
編輯snmpd.conf,並將RANDOMSTRINGGOESHERE改為自己的community string
vi /etc/snmp/snmpd.conf
下載librenms-agent,並重新啟動snmpd
curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
chmod +x /usr/bin/distro
systemctl enable snmpd
systemctl restart snmpd
複製建立Cron job
cp /opt/librenms/librenms.nonroot.cron /etc/cron.d/librenms
設置logrotate
LibreNMS會將log保存在/opt/librenms/logs中,隨著時間推移,不必要的log會一直佔據磁碟空間,所以需要設定logrotate清除過期不再需要的log
複製logrotate設定檔
cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms
設定資料夾權限
chown -R librenms:librenms /opt/librenms
setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
chgrp apache /var/lib/php/session/
將電腦名稱及IP加入到/etc/hosts
透過瀏覽器安裝LibreNMS
http://<librenms.example.com>/install.php
2018-12-19_113454
Pre-Install Checks如果沒問題就點選Next Stage
2018-12-19_113750
DB Pass處輸入之前設定的MySQL密碼
2018-12-19_114348
如果沒任何錯誤訊息就點選Goto Add User
2018-12-19_114657
建立LibreNMS的管理者帳號、密碼及Email
2018-12-19_115035
點選Generate Config
2018-12-19_115225
這裡出現一個錯誤,因為沒有權限在/opt/librenms/下建立config.php檔案,所以要請我們複製下方內容,並手動建立config.php檔案
(備註:每個人的config.php有可能不同,請依照當下LibreNMS的建議將內文複製貼上)
(備註:測試過,config.php檔案不能先建立,並新增檔案的權限,不然會跳過LibreNMS的安裝步驟)
vi /opt/librenms/config.php
檔案建立後執行下列指令新增檔案的權限
chown librenms:librenms /opt/librenms/config.php
完畢後點選Finish
2018-12-19_120423
安裝結束後,點選”validate your install and fix any issues.”
2018-12-19_165427
輸入剛才在安裝步驟中建立的管理者帳號及密碼登入
2018-12-19_165707
登入LibreNMS管理頁面後,可能會有需要修正的錯誤,或是警告訊息,只要依照LibreNMS的步驟做修正就行了
2018-12-19_172841
全部錯誤修正完之後,就可以把系統或設備加入到LibreNMS


備註:非常重要
當發生問題時,可以執行下列指令做TroubleshootingLibreNMS會提供方法及解決步驟(真是太感恩了,居然可以遇到這麼人性化的系統,只要照著做就能解決問題)
cd /opt/librenms
./validate.php

參考資料:
LibreNMS官網