温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux运维相关操作(centos/Ubuntu)

发布时间:2020-03-15 16:02:57 来源:网络 阅读:576 作者:a_liujin 栏目:系统运维

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 /

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI