
Linux配置
一、/etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information. # 此文件为全局SSH服务配置文件
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin # SSH服务编译时包含的路径
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value. # 默认配置文件中的策略是将选项初始化为默认值并注释
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change. # 如果在SELinux系统上更改端口,则需告知SELinux
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22 # SSH服务监听的端口,默认为22
#AddressFamily any # 指定地址族,any表示IPv4和IPv6
#ListenAddress 0.0.0.0 # 监听所有IPv4地址
#ListenAddress :: # 监听所有IPv6地址
HostKey /etc/ssh/ssh_host_rsa_key # RSA公钥位置
#HostKey /etc/ssh/ssh_host_dsa_key # DSA公钥位置(已弃用)
HostKey /etc/ssh/ssh_host_ecdsa_key # ECDSA公钥位置
HostKey /etc/ssh/ssh_host_ed25519_key # Ed25519公钥位置
# Ciphers and keying # 加密算法和密钥策略
#RekeyLimit default none # 重置密钥的限制
# Logging # 日志设置
#SyslogFacility AUTH # 设置系统日志类型为身份验证
SyslogFacility AUTHPRIV # 设置系统日志为保密身份验证日志
#LogLevel INFO # 设置日志记录级别
# Authentication: # 身份验证设置
#LoginGraceTime 2m # 登录宽限时间,默认2分钟
PermitRootLogin yes # 允许根用户登录,注意安全风险
#StrictModes yes # 启用严格模式,检查授权文件权限
#MaxAuthTries 6 # 最大身份验证次数
#MaxSessions 10 # 最大会话数
#PubkeyAuthentication yes # 启用公钥认证
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys # 公钥文件位置
#AuthorizedPrincipalsFile none # 授权主体文件(未启用)
#AuthorizedKeysCommand none # 授权密钥命令(未启用)
#AuthorizedKeysCommandUser nobody # 授权密钥命令用户(未启用)
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no # 主机基础认证,未启用
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no # 是否忽略已知主机文件
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes # 不读取用户的.rhosts和.shosts文件
# To disable tunneled clear text passwords, change to no here!
#PermitEmptyPasswords no # 禁用空密码用户登录
PasswordAuthentication yes # 允许使用密码进行身份验证
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes # 启用挑战响应身份验证
ChallengeResponseAuthentication no # 禁用挑战响应身份验证
# Kerberos options # Kerberos选项
#KerberosAuthentication no # 禁用Kerberos身份验证
#KerberosOrLocalPasswd yes # Kerberos或本地密码身份验证
#KerberosTicketCleanup yes # 清理Kerberos票据
#KerberosGetAFSToken no # 禁用获取AFS令牌
#KerberosUseKuserok yes # 使用kuserok检查
# GSSAPI options # GSSAPI选项
GSSAPIAuthentication yes # 启用GSSAPI身份验证
GSSAPICleanupCredentials no # 禁用清理凭据
#GSSAPIStrictAcceptorCheck yes # 严格检查接收者
#GSSAPIKeyExchange no # 禁用GSSAPI密钥交换
GSSAPIEnablek5users no # 禁用k5用户支持
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes # 启用PAM身份验证和会话处理
#AllowAgentForwarding yes # 允许代理转发
#AllowTcpForwarding yes # 允许TCP转发
#GatewayPorts no # 网关端口设置
X11Forwarding yes # 启用X11转发
#X11DisplayOffset 10 # X11显示偏移
#X11UseLocalhost yes # X11使用本地地址
#PermitTTY yes # 允许TTY
#PrintMotd yes # 打印消息
#PrintLastLog yes # 打印最后登录信息
#TCPKeepAlive yes # TCP保持连接
#UseLogin no # 使用登录方式
#UsePrivilegeSeparation sandbox # 使用特权隔离
#PermitUserEnvironment no # 禁止用户环境设置
#Compression delayed # 延迟压缩
#ClientAliveInterval 0 # 客户端存活检查间隔
#ClientAliveCountMax 3 # 最大存活检查次数
#ShowPatchLevel no # 显示补丁级别
UseDNS no # 禁用DNS反向解析
#PidFile /var/run/sshd.pid # PID文件位置
#MaxStartups 10:30:100 # 最大启动数
#PermitTunnel no # 禁用隧道
#ChrootDirectory none # Chroot目录设置
#VersionAddendum none # 版本附加信息
# no default banner path # 无默认标语路径
#Banner none # 标语设置
# Accept locale-related environment variables # 接受区域相关的环境变量
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems # 覆盖默认无子系统
Subsystem sftp /usr/libexec/openssh/sftp-server # SFTP子系统
# Example of overriding settings on a per-user basis # 按用户覆盖设置示例
#Match User anoncvs # 匹配特定用户
# X11Forwarding no # 禁用X11转发
# AllowTcpForwarding no # 禁止TCP转发
# PermitTTY no # 禁止TTY
# ForceCommand cvs server # 强制执行cvs server命令
二、/etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet" # 网络接口类型,通常为Ethernet类型
PROXY_METHOD="none" # 指定代理方法,none表示不使用代理
BROWSER_ONLY="no" # 指定接口是否仅用于浏览器,no表示不是
BOOTPROTO="static" # IP地址获取方式为静态,需手动设置
DEFROUTE="yes" # 设置为默认路由,表示此接口为默认网关
IPV4_FAILURE_FATAL="no" # IPv4失败是否致命,no表示不致命
IPV6INIT="yes" # 启用IPv6支持
IPV6_AUTOCONF="yes" # 启用IPv6自动配置
IPV6_DEFROUTE="yes" # 将此接口用于IPv6默认路由
IPV6_FAILURE_FATAL="no" # IPv6失败是否致命,no表示不致命
IPV6_ADDR_GEN_MODE="stable-privacy" # IPv6地址生成模式,稳定隐私地址
NAME="ens33" # 网络接口名称
UUID="48e59915-ec65-476f-a79d-7ee9ba668791" # 唯一识别码,用于标识此网络接口
DEVICE="ens33" # 设备名称,通常与NAME相同
ONBOOT="yes" # 在系统启动时启用此网络接口
IPV6_PRIVACY="no" # 禁用IPv6隐私扩展,设置为no
IPADDR="192.168.149.134" # 指定静态IPv4地址
GATEWAY="192.168.149.1" # 默认网关地址
PREFIX="24" # 子网掩码的前缀长度,表示255.255.255.0
DNS1="119.29.29.29" # 主DNS服务器地址
DNS2="114.114.114.114" # 备用DNS服务器地址
三、yum源配置
# 路径:/etc/yum.repos.d
# 备份原有yum源
mkdir /etc/yum.repos.d/repo_bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo_bak
# 在CentOS中配置使用阿里的开源镜像
wget http://mirrors.aliyun.com/repo/Centos-7.repo
# 清除系统所有的yum缓存
yum clean all
# 重新生成yum缓存
yum makecache
# 查看是否安装epel源,没有则安装
yum list | grep epel-release
四、vm.max_map_count
vm.max_map_count
是 Linux 内核中的一个虚拟内存参数,用于限制单个进程可以创建的内存映射区域的最大数量。它主要影响使用内存映射文件的程序,例如数据库、搜索引擎等需要大量内存映射的应用程序。
配置的作用
限制内存映射区域的数量:
vm.max_map_count
定义了每个进程可以拥有的最大内存映射区域数量。如果一个进程试图创建的映射超过了这个限制,系统将会拒绝该映射请求,并返回错误。防止过度消耗内存资源:通过限制内存映射区域的数量,可以防止某些进程过度消耗系统资源,导致系统不稳定或性能下降。过多的内存映射区域可能导致系统在创建新映射时出现性能问题或崩溃。
影响特定应用:某些应用(例如 Elasticsearch 和某些数据库)可能需要较高的内存映射区域数量。默认的
vm.max_map_count
值通常是65530,但某些应用可能需要将其增加到更多的值,以便正常运行。配置方法:可以通过以下命令查看当前值:
sysctl vm.max_map_count
可以通过以下命令临时设置新的值(会在重新启动后失效):
bash
sysctl -w vm.max_map_count=<new_value>
若要使其在重启后保持可用,可以将其添加到
/etc/sysctl.conf
文件中:vm.max_map_count=<new_value>
然后运行
sysctl -p
应用更改。
五、修改文件描述符限制
文件描述符是 Linux 系统中用于处理打开文件、网络连接和其他 I/O 操作的资源
1. 查看当前文件描述符限制
首先,可以使用以下命令检查当前的文件描述符限制:
ulimit -n
2. 临时增加文件描述符限制
要在当前会话中临时增加文件描述符限制,可以使用 ulimit
命令,例如:
ulimit -n 65535
注意:此更改只在当前 shell 会话有效,重新启动后将恢复默认值。
3. 永久增加文件描述符限制
要永久增加文件描述符限制,需要修改系统配置文件。以下是常见的方法:
3.1 编辑 /etc/security/limits.conf
使用编辑器打开
/etc/security/limits.conf
文件:sudo nano /etc/security/limits.conf
在文件末尾添加以下行,以设置用户的软限制和硬限制。可以替换
username
为你的用户名,或使用*
代表所有用户:username soft nofile 65535 username hard nofile 65535
或使用
*
来设置所有用户:* soft nofile 65535 * hard nofile 65535
保存并关闭文件。
3.2 更新 PAM(可插拔认证模块)
在某些系统中,你可能需要更新 PAM 配置来启用新的限制。编辑 /etc/pam.d/common-session
文件(在某些系统中可能是 /etc/pam.d/login
):
sudo nano /etc/pam.d/common-session
在文件的末尾添加以下行:
session required pam_limits.so
4. 修改系统级别文件描述符限制
要修改整个系统的最大文件描述符限制,请编辑 /etc/sysctl.conf
文件:
打开
/etc/sysctl.conf
文件:sudo nano /etc/sysctl.conf
添加或修改以下行,以设置系统范围内的文件描述符限制:
fs.file-max = 100000
应用更改:
sudo sysctl -p
5. 重新启动你的会话或系统
为了使更改生效,建议重新启动你的 shell 会话,或者如果需要,可以重启系统。
6. 检查和验证
在重新启动后,可以再次使用 ulimit -n
验证更改:
ulimit -n