天天看點

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

</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>

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

上一篇: IOS地圖開發

繼續閱讀