linux運維相關操作(centos/Ubuntu)

1.centos 網絡配置相關的文件
/etc/hostname 主機名配置
/etc/sysconfig/network-scrips/ifcfg-enoN 網卡參數配置
/etc/resolv.conf DNS配置
/etc/hosts 主機ip域名配置
重啓網卡 service network restart

2.路由設置
可以將該命令寫入/etc/rc.d/rc.local 每次開機都執行
route -n 顯示路由信息
route add default gw 192.168.1.254
route del default gw 192.168.1.254
ifconfig eth0:1 10.66.47.1/24 up 配置網卡的子ip
route -p add -net 10.66.47.1/24 gw 10.65.255.254 -p永久生效
route del -net 10.66.47.1/24

3.iptables防火牆
防火牆規則可以配置到文件:/proc/sys/net/ipv4/ip_forward
iptables -t filter -F 清除所有filter規則
iptables -nv -L 查看各個鏈的信息
iptables -t filter -A INPUT -s 10.66.47.2/32 -j ACCEPT
-t 表示用於filter(或nat等)的表,-A 表示新增到哪條鏈路(INPUT FORWARD OUTPUT) -s 子網或主機 -j 動作(ACCEPT DROP REJECT)

iptables -A INPUT -p tcp -s 10.66.47.0/24 --dport 22 -j DROP 禁用22號端口
iptables -D INPUT 1 刪除INPUT的第一條規則
nc -v 10.66.47.1 443 -w 3 測試10.66.47.1 的 443端口是否能連通

4.tcpdump
tcpdump -i eth0 tcp and dst host 127.0.0.1 and dst port 3306 -s100 -XX -n (-XX 以16進制和ascii顯示,-n不對主機轉換)
tcpdump tcp port 80 -n -s0 抓取http包 (-s0抓取儘可能大的包65535)
tcpdump tcp host 10.16.2.85 and port 2100 -s 0 -X 

5.dhcp
dpcp服務器可以爲使用dhcp的客戶端網卡動態的分配地址。其過程使用C/S模式,由dhcp客戶端主動請求分配ip地址。

6.策略路由配置
ip rule list 查看所有路由表 (local main default)
ip route list table local 查看local表中的路由策略
echo 100 test1 > /etc/iproute2/rt_tables 建立一張id爲100的test1路由表
ip rule add from 10.66.47.1/24 table test1 ;來自10.66.47.1/24網路的ip都使用test1表中的規則
ip rule del table test1 刪除路由表test1
ip route add default via 10.65.47.1 table test1 在test1表中添加了一條默認路由
ip route add 10.65.0.0/24 via 10.65.47.1 table test1

7.磁盤管理
fdisk分區管理
fdisk -l 查看所有磁盤設備文件及其對應的分區文件信息
fdisk /dev/sda 進入磁盤設備文件/dev/sda
p 顯示該設備文件分區
n 創建一個分區 輸入分區結束位置
d 刪除分區
parted分區管理
parted /dev/sda 進入磁盤設備文件/dev/sda
print 顯示該設備文件分區
mkpart 創建一個分區
rm 刪除分區

8.squid代理服務器配置
局域網通過代理服務器訪問外網

9.nginx.conf反向代理配置和負載均衡配置
location ~* .(mp3|mp4)$ { #不區分大小寫,匹配以mp3或mp4結束的請求,代理到本地的8080;不修改用戶真實的ip
proxy_pass http://localhost:8080
}

location / { #匹配任意url,代理到8000,
proxy_pass http://localhost:8000
proxy_set_header X-Forwarded-For $remote_addr
}

負載均衡配置
http{
upstream backendservers{
ip_hash;
server www.example1.com weight=2;
server www.example2.com weight=1;
server www.example3.com weight=1;
}
server{
listen 80;
server_name www.example.com;
location / {
proxy_pass http://backendservers;
}
}
}

10.mysql複製數據同步
https://www.cnblogs.com/rwxwsblog/p/4542417.html (單向數據同步)
http://blog.csdn.net/swandy45/article/details/6982421 (雙向數據同步)

11.防火牆規則添加
systemctl restart firewalld //重啓防火牆
firewall-cmd --reload //重新加載防火牆配置
systemctl start firewalld.service //開啓服務
systemctl enable firewalld.service //開機制動啓動
systemctl stop firewalld.service //關閉服務
systemctl disable firewalld.service //禁止開機啓動
–permanent #永久生效,沒有此參數重啓後失效
在每次修改 端口和服務後 /etc/firewalld/zones/public.xml 文件就會被修改 所以也可以在文件中直接修改 然後reload重新加載

firewall-cmd --permanent --add-port=1234/tcp 對外暴露該端口
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.0.4/24" service name="http" accept"//設置某個ip 訪問某個服務
firewall-cmd --permanent --zone=public --remove-rich-rule="rule family="ipv4" source address="192.168.0.4/24" service name="http" accept" //刪除配置

firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.0.1/24 port port=80 protocol=tcp accept' //設置某個ip段訪問某個端口
firewall-cmd --permanent --remove-rich-rule 'rule family=ipv4 source address=192.168.0.1/2 port port=80 protocol=tcp accept' //刪除配置

firewall-cmd --query-masquerade # 檢查是否允許僞裝IP
firewall-cmd --add-masquerade # 允許防火牆僞裝IP(端口轉發)
firewall-cmd --remove-masquerade# 禁止防火牆僞裝IP

firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 # 將80端口的流量轉發至8080
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.1.0.1 # 將80端口的流量轉發至192.168.0.1
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.0.1:toport=8080 # 將80端口的流量轉發至192.168.0.1的8080端口

firewall-cmd –state //獲取 firewalld 狀態
firewall-cmd –state && echo “Running” || echo “Not running” //狀態輸出
firewall-cmd –reload //重新加載防火牆
firewall-cmd –get-zones //獲取支持的區域列表
firewall-cmd –get-services //獲取所有支持的服務
firewall-cmd –list-all-zones //列出全部啓用的區域的特性
firewall-cmd [–zone=] –add-interface= //將接口增加到區域
firewall-cmd [–zone=] –change-interface= //修改接口到區域
firewall-cmd [–zone=] –remove-interface= //刪除接口到區域
firewall-cmd [–zone=] –query-interface= //查詢區域中的接口

12.apache2和centos 網站配置
apache2
1.sudo vi /etc/apache2/sites-available/public_cloud.conf

<VirtualHost *:8000>
ServerName www.nsfocuscontrol.com
ServerAlias nsfocuscontrol12.com
DocumentRoot /home/shanghai/pc_django/PCCon/PCCon

<Directory /home/shanghai/pc_django/PCCon/PCCon>
    Require all granted
</Directory>
WSGIScriptAlias / /home/shanghai/pc_django/PCCon/PCCon/wsgi.py
# WSGIDaemonProcess ziqiangxuetang.com python-path=/home/tu/blog:/home/tu/.virtualenvs/blog/lib/python2.7/site-packages
# WSGIProcessGroup ziqiangxuetang.com

<Directory /home/shanghai/pc_django/PCCon/PCCon>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

</VirtualHost>

2.sudo vim /etc/apache2/ports.conf
// If you just change the port or add more ports here, you will likely also
// have to change the VirtualHost statement in
// /etc/apache2/sites-enabled/000-default.conf

Listen 8000

<IfModule ssl_module>
Listen 443
</IfModule>

<IfModule mod_gnutls.c>
Listen 443
</IfModule>

3.更改apache2的運行用戶
apache 服務器運行用戶可以在 /etc/apache2/envvars 文件裏面改,這裏使用的是默認值,當然也可以更改成自己的當前用戶,這樣的話權限問題就簡單很多,但在服務器上推薦有 www-data 用戶,更安全。以下是默認設置:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

4.添加認證HEAD
添加文件 /etc/apache2/.htacess 內容:
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.)
RewriteRule .
- [e=HTTP_AUTHORIZATION:%1]

編輯/etc/apache2/apache2.conf 添加一行:
AccessFileName .htaccess
WSGIPAssAuthorization On

5.sudo vi ~/pc_django/PCCon/PCCon/wsgi.py
添加
import sys
from os.path import join,dirname,abspath
PROJECT_DIR = dirname(dirname(abspath(file)))
sys.path.insert(0,PROJECT_DIR)

6.設置目錄和文件權限
假如項目位置在 ~/pc_django/PCCon (在zqxt 下面有一個 manage.py,zqxt 是項目名稱)
一般目錄權限設置爲 755,文件權限設置爲 644
cd ~/pc_django/PCCon
sudo chmod -R 644 PCCon
sudo find PCCon -type d | xargs chmod 755

如果是上傳文件的保存目錄或者sqlite3的數據庫文件,需要apache執行用戶的寫權限
sudo chgrp -R www-data uploads_dir
sudo chmod -R g+w uploads_dir

7.激活新網站並重啓服務器
  a2ensite /etc/apache2/sites-available/public_cloud.conf
服務重啓
  sudo service apache2 restart 或 sudo service apache2 reload

cat /etc/passwd|cut -f 1 -d:

mount -rw -o remount /

centos

1.sudo vi /etc/apache2/sites-available/public_cloud.conf

<VirtualHost *:8000>
ServerName www.nsfocuscontrol.com
ServerAlias nsfocuscontrol12.com
DocumentRoot /home/shanghai/pc_django/PCCon/PCCon

<Directory /home/shanghai/pc_django/PCCon/PCCon>
    Require all granted
</Directory>
WSGIScriptAlias / /home/shanghai/pc_django/PCCon/PCCon/wsgi.py
# WSGIDaemonProcess ziqiangxuetang.com python-path=/home/tu/blog:/home/tu/.virtualenvs/blog/lib/python2.7/site-packages
# WSGIProcessGroup ziqiangxuetang.com

<Directory /home/shanghai/pc_django/PCCon/PCCon>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

</VirtualHost>

2.sudo vim /etc/apache2/ports.conf
// If you just change the port or add more ports here, you will likely also
// have to change the VirtualHost statement in
// /etc/apache2/sites-enabled/000-default.conf

Listen 8000

<IfModule ssl_module>
Listen 443
</IfModule>

<IfModule mod_gnutls.c>
Listen 443
</IfModule>

3.更改apache2的運行用戶
apache 服務器運行用戶可以在 /etc/apache2/envvars 文件裏面改,這裏使用的是默認值,當然也可以更改成自己的當前用戶,這樣的話權限問題就簡單很多,但在服務器上推薦有 www-data 用戶,更安全。以下是默認設置:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

4.添加認證HEAD
添加文件 /etc/apache2/.htacess 內容:
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.)
RewriteRule .
- [e=HTTP_AUTHORIZATION:%1]

編輯/etc/apache2/apache2.conf 添加一行:
AccessFileName .htaccess
WSGIPAssAuthorization On

5.sudo vi ~/pc_django/PCCon/PCCon/wsgi.py
添加
import sys
from os.path import join,dirname,abspath
PROJECT_DIR = dirname(dirname(abspath(file)))
sys.path.insert(0,PROJECT_DIR)

6.設置目錄和文件權限
假如項目位置在 ~/pc_django/PCCon (在zqxt 下面有一個 manage.py,zqxt 是項目名稱)
一般目錄權限設置爲 755,文件權限設置爲 644
cd ~/pc_django/PCCon
sudo chmod -R 644 PCCon
sudo find PCCon -type d | xargs chmod 755

如果是上傳文件的保存目錄或者sqlite3的數據庫文件,需要apache執行用戶的寫權限
sudo chgrp -R www-data uploads_dir
sudo chmod -R g+w uploads_dir

7.激活新網站並重啓服務器
  a2ensite /etc/apache2/sites-available/public_cloud.conf
服務重啓
  sudo service apache2 restart 或 sudo service apache2 reload

cat /etc/passwd|cut -f 1 -d:
mount -rw -o remount /

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章