Centos7操作系统常规设置

Centos7操作系统常规设置

1、网络类

安装ifconfig、arp等mii-tool等命令

1
yum install -y net-tools

安装图形化网络配置

1
2
#nmtui 命令取代了 setup中的网络配置
yum install NetworkManager-tui -y

安装nslookup命令

1
yum install bind-utils -y

网卡及网络信息

1
2
3
4
ip n
ip -s l
ss -tlnup
netstat -tlnup

查看MAC地址

1
2
arp -a
ip addr show

查看路由

1
2
3
route -n
ip route show
ip -6 route show

2、系统类

查看操作系统版本

1
cat /etc/redhat-release

查看操作系统内核版本

1
uname -a

时区和时间设置

1
2
3
4
5
6
7
8
9
10
11
#时区配置方法1
timedatectl set-timezone Asia/Shanghai
timedatectl status
/etc/locale.conf
#时区配置方法2
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#时间配置
yum install dnf ntpdate -y
dnf makecache
ntpdate ntp.aliyun.com

语言设置

1
2
3
4
5
6
7
8
#方法1
locale -a |grep CN
vim /etc/locale.conf
source /etc/locale.conf

#方法2
localectl set-locale LANG=zh_CN.utf8
localectl status

修改主机名

1
vim /etc/hostname

服务的管理和控制

1
2
3
4
#systemctl是最主要的工具。它融合service 和chkconfig的功能于一体
systemctl --help
#启动脚本存放目录
/usr/lib/systemd/system

服务启动与停止

1
2
3
systemctl start service_name
systemctl stop service_name
systemctl restart/status/reload sshd

服务自启动

1
2
systemctl enable service_name
systemctl disable service_name

服务一览

1
2
3
systemctl list-unit-files
systemctl --type service
systemctl is-enabled sshd

重载服务配置文件

1
systemctl daemon-reload

杀死服务

1
2
systemctl kill --signal=9 sshd
kill -9 <PID>

切换运行级别

1
2
3
4
5
6
7
systemctl isolate multi-user.target
systemctl isolate graphical.target
#默认
systemctl set-default graphical.target
systemctl set-default multi-user.target
#当前
systemctl get-default

关闭服务器

1
2
3
init 0
poweroff
systemctl poweroff

重启服务器

1
2
3
init 6
reboot
systemctl reboot

单用户模式

1
systemctl rescue

查看开机时间

1
2
3
4
5
6
#查看看机时间
systemd-analyze time
#查看占用开机时间的程序和服务
systemd-analyze blame
#将开机时间所占用的程序到出成图形化进行分析
systemd-analyze plot > bootime.svg

文件结构变化

1
centos7的/bin, /sbin, /lib, and /lib64移到/usr下

通过repoquery查看rpm包中包含哪些文件

1
2
yum install yum-utils
repoquery -q -l <包名>

3、优化类

内核参数配置文件

1
2
3
4
#centos7采用文件夹引用方式加载配置
/etc/sysctl.d/
/usr/lib/sysctl.d/
/run/sysctl.d/

内核参数优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#filemax
fs.file-max = 65535
#关闭ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
# 避免放大攻击
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 开启恶意icmp错误消息保护
net.ipv4.icmp_ignore_bogus_error_responses = 11
#决定检查过期多久邻居条目
net.ipv4.neigh.default.gc_stale_time=120
#使用arp_announce / arp_ignore解决ARP映射问题
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
#处理无源路由的包
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
#core文件名中添加pid作为扩展名
kernel.core_uses_pid = 1
#修改消息队列长度
kernel.msgmnb = 65536
kernel.msgmax = 65536
#开机路由转发
net.ipv4.ip_forward = 1
#开启反向路径过滤
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
#设置最大内存共享段大小bytes
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
#timewait的数量,默认180000
net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
#每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。
net.core.netdev_max_backlog = 32768
#记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有128M内存的系统而言,缺省值是1024
net.ipv4.tcp_max_syn_backlog = 65536
#web应用中listen函数的backlog默认会给我们内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值。
net.core.somaxconn = 32768
#限制仅仅是为了防止简单的DoS 攻击
net.ipv4.tcp_max_orphans = 3276800
#未收到客户端确认信息的连接请求的最大值
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
#内核放弃建立连接之前发送SYNACK 包的数量
net.ipv4.tcp_synack_retries = 1
#内核放弃建立连接之前发送SYN 包的数量
net.ipv4.tcp_syn_retries = 1
#启用timewait 快速回收
net.ipv4.tcp_tw_recycle = 1
#开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
# 开启SYN洪水攻击保护(防范少量SYN攻击)
net.ipv4.tcp_syncookies = 1
#允许系统打开的端口范围
net.ipv4.ip_local_port_range = 32768 61000

开机启动脚本需加X权限

1
chmod +x /etc/rc.d/rc.local

安装系统基本管理工具

1
2
yum groupinstall "Development Tools" --setopt=group_package_types=mandatory,default,optional
yum install tree net-tools bind-utils tree sysstat NetworkManager-tui ntp iftop vim telnet wget nethogs htop glances dstat traceroute lrzsz goaccess ntpdate dos2unix openssl-devel tcpdump lrzsz fio nss curl ca-certificates socat -y

优化SSH相关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vim /etc/ssh/sshd_config
#AddressFamily any
AddressFamily inet
#注:(inet is ipv4 only; inet6 is ipv6 only)

Port 60002
#禁止root用户登录,根据需求设定
# PermitRootLogin no
#禁止空密码登录
PermitEmptyPasswords no
#不使用DNS解析,可以加快访问速度
UseDNS no
# 是一套类似Kerberos 5的通用网络安全系统接口。该接口是对各种不同的客户端服务器安全机制的封装,以消除安全接口的不同,降低编程难度。但该接口在目标机器无域名解析时会有问题
GSSAPIAuthentication no

关闭防火强和setlinux

1
2
3
4
5
6
#停止防火墙服务并取消开机自启
systemctl disable --now firewalld

#关闭selinux
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
-------------本文结束感谢您的阅读-------------
LiGuanCheng wechat
如有问题,请与我微信交流或通过右下角“daovoice”与我联系~。
请我喝一杯咖啡~