Linux服务器系统配置初始化配置及调优

禁用selinux

禁用SELinux有两种主要方法:临时禁用和永久禁用。下面将分别介绍这两种方法。

方法一:临时禁用SELinux

临时禁用SELinux只在当前会话中有效,系统重启后会恢复启用状态。

使用以下命令将SELinux切换到宽松模式(Permissive mode):

sudo setenforce 0

验证SELinux当前状态:

getenforce

输出应为 `Permissive`,表示SELinux已临时禁用。

方法二:永久禁用SELinux

要永久禁用SELinux,需要修改SELinux的配置文件并重启系统。

1)  编辑SELinux配置文件:

sudo vi /etc/selinux/config

将 `SELINUX` 设置为 `disabled`:

   SELINUX=disabled

或者使用命令

sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

说明:

● sudo: 以超级用户权限执行命令。

● sed -i: 直接在文件中进行替换。

● s/^SELINUX=.*/SELINUX=disabled/: 这部分表示将以 SELINUX= 开头的行替换为 SELINUX=disabled

修改后文件的内容应如下所示:

   # This file controls the state of SELinux on the system.
   # SELINUX= can take one of these three values:
   #     enforcing - SELinux security policy is enforced.
   #     permissive - SELinux prints warnings instead of enforcing.
   #     disabled - No SELinux policy is loaded.
   SELINUX=disabled
   # SELINUXTYPE= can take one of these two values:
   #     targeted - Targeted processes are protected,
   #     mls - Multi Level Security protection.
   SELINUXTYPE=targeted

2)  保存文件并退出编辑器。

3)  重启系统以使更改生效:

sudo reboot

4)  验证SELinux状态:

5)  重启后,使用以下命令确认SELinux已禁用:

getenforce

输出应显示 `SELinux status: disabled`。

(1)  关闭防火墙开放端口

# 停止并禁用FirewallD

sudo systemctl stop firewalld
sudo systemctl disable firewalld

# 停止并禁用iptables(适用于CentOS 6及以下)

sudo service iptables stop
sudo chkconfig iptables off

# 若使用CentOS 7及以上版本,停止并禁用iptables的方法如下:

sudo systemctl stop iptables
sudo systemctl disable iptables

(2)  设置最大打开文件数

设置最大打开文件数涉及修改两个地方:一是用户级别的限制设置,二是系统级别的限制设置。

下面是详细的步骤:

步骤 1: 修改 /etc/security/limits.conf

方法一 首先,编辑 /etc/security/limits.conf 文件来设置用户级别的最大文件打开数

1.打开终端并编辑 /etc/security/limits.conf 文件:

sudo vi /etc/security/limits.conf

在文件的末尾添加以下行来设置软限制和硬限制:

[root@sjk ~]# cat  /etc/security/limits.conf
# End of file
#add by kingbase
*       soft        nofile      655360
root    soft        nofile      655360
*       hard        nofile      655360
root    hard        nofile      655360
*       soft        nproc       655360
root    soft        nproc       655360
*       hard        nproc       655360
root    hard        nproc       655360
*       soft        core        unlimited
root    soft        core        unlimited
*       hard        core        unlimited
root    hard        core        unlimited
*       soft        memlock     50000000
root    soft        memlock     50000000
*       hard        memlock     50000000
root    hard        memlock     50000000

保存并关闭文件。

方法二 使用命令

sudo bash -c 'cat >> /etc/security/limits.conf <<EOL
# Batch add by $(whoami)
*       soft        nofile      655360
root    soft        nofile      655360
*       hard        nofile      655360
root    hard        nofile      655360
*       soft        nproc       655360
root    soft        nproc       655360
*       hard        nproc       655360
root    hard        nproc       655360
*       soft        core        unlimited
root    soft        core        unlimited
*       hard        core        unlimited
root    hard        core        unlimited
*       soft        memlock     50000000
root    soft        memlock     50000000
*       hard        memlock     50000000
root    hard        memlock     50000000
EOL'

这里的 * 代表对所有用户生效。nofile 表示打开文件的数量限制。

验证更改

查看当前会话的限制:

ulimit -Sn  # 查看软限制
ulimit -Hn  # 查看硬限制

这应显示你刚刚设置的值,即 655360

步骤 2: 修改系统级设置

在 /etc/sysctl.conf 文件添加以下行:

快速添加

cat >> /etc/sysctl.conf << EOF
vm.max_map_count=262144
vm.overcommit_memory=1
kernel.pid_max=1000000
EOF

(1)  其他优化配置

# 系统内核优化参数
cat >> /etc/sysctl.conf << EOF
#add by kingbase
kernel.sem = 5010 641280 5010 256
kernel.shmall = 2097152
kernel.shmmax = 53687091200
kernel.shmmni = 8192
vm.mmap_min_addr = 65536
vm.dirty_writeback_centisecs = 100
vm.dirty_background_ratio = 10
vm.dirty_ratio = 60
vm.swappiness = 20
vm.min_free_kbytes = 512000
vm.vfs_cache_pressure = 200
fs.aio-max-nr = 1048576
fs.file-max = 76724600
fs.nr_open = 2097152

net.core.netdev_max_backlog = 32768
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.somaxconn = 4096
net.core.wmem_default = 262144
net.core.wmem_max = 4194304

net.ipv4.ip_local_port_range = 9000 65500
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_wmem = 8192 436600 873200
net.ipv4.tcp_rmem  = 32768 436600 873200
net.ipv4.tcp_mem = 94500000 91500000 92700000
net.ipv4.tcp_max_orphans = 3276800
EOF

这里设置的 fs.file-max 是系统级别的最大文件描述符数。

查看系统级别的文件描述符限制:

cat /proc/sys/fs/file-max

应显示为你设置的 76724600 或更高,取决于具体配置。

修改 net.ipv4.ip_forward 的值为 1

临时修改:

sudo sysctl -w net.ipv4.ip_forward=1

永久修改:

sudo sed -i '/^net.ipv4.ip_forward/c\net.ipv4.ip_forward = 1' /etc/sysctl.conf || echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf

应用更改:

sudo sysctl -p

立即使 /etc/sysctl.conf 中的更改生效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值