1. 设置语言及主机名
# hostnamectl set-hostname imxhy #设置主机名
# 在有时候需要设置主机名及系统语言
# localectl set-locale LANG=zh_CN.utf8 #设置为中文
# localectl set-locale LANG=en_US.UTF-8 #设置为英文
2. 关闭SELinux及防火墙
# cp -brpf /etc/selinux/config /etc/selinux/config.bak
# sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
# systemctl disable firewalld --now
3. 优化ssh访问速度
对于某些情况,ssh登录Linux的时候,会尝试解析为DNS name,需要花费时间,可进行关闭。
# sed -i 's/^UseDNS.*/UseDNS no/' /etc/ssh/sshd_config # sed -i 's/^#UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
4. 安装常用软件
CentOS7常用软件安装
# yum install -y lrzsz tree ntp NetworkManager-tui vim net-tools mtr cronie
5. 修改IP地址
确保在root用户下进行操作
进入网络配置文件network-scripts目录下。
# cd /etc/sysconfig/network-scripts/
# vi ifcfg-ens192
BOOTPROTO="static" #dhcp改为static,
ONBOOT="yes" #将网卡设置为开机启用
IPADDR=192.168.0.230 #静态IP
GATEWAY=192.168.0.1 #默认网关
NETMASK=255.255.255.0 #子网掩码
DNS1=192.168.0.1 #DNS 配置
DNS2=8.8.8.8 #谷歌地址
重启网络服务
# service network restart #重启网络服务
6. 配置NTP服务
# yum -y install ntp
# date #查看本机时间
# ntpdate ntp.chinafsl.com #确认NTP服务器的可用性
# vim /etc/ntp.conf
# 注释配置文件中原有的NTP服务器,并在其中添加自己所需的NTP服务器地址
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
# server 2.centos.pool.ntp.org iburst
# server 3.centos.pool.ntp.org iburst
server ntp.chinafsl.com iburst
保存退出后
# systemctl enable ntpd.service #安装为服务
Created symlinkfrom/etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
# systemctl start ntpd #启动运行
7. 防止密码被暴力破解
修改配置文件
# vi /etc/pam.d/sshd
在文末增加以下内容
auth required pam_tally2.so deny=5 unlock_time=180 even_deny_root root_unlock_time=180
文件参数说明:
even_deny_root 也限制root用户;
deny 设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户
unlock_time 设定普通用户锁定后,多少时间后解锁,单位是秒;
root_unlock_time 设定root用户锁定后,多少时间后解锁,单位是秒;
保存退出
CentOs 重启ssh服务:
# service sshd restart #或者使用下面的命令
# systemctl restart sshd.service
8. Linux CentOS中防火墙的关闭及开启端口
方法一、在外部访问CentOS中部署应用时,需要关闭防火墙。
#关闭防火墙命令
systemctl stop firewalld.service
#开启防火墙
systemctl start firewalld.service
#关闭开机自启动
systemctl disable firewalld.service
#开启开机启动
systemctl enable firewalld.service
方法二、CentOS7使用firewall工具管理防火墙,代替了原来的iptables
操作步骤如下:
#查看防火墙状态=》使用root的身份=》结果为running
firewall-cmd --state
#永久性的开放8080端口
firewall-cmd --add-port=8080/tcp permanent
#重载生效刚才的端口设置
firewall-cmd --reload
常用的firewall命令常用命令介绍
firewall-cmd --state ##查看防火墙状态,是否是running
firewall-cmd --reload ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones ##列出支持的zone
firewall-cmd --get-services ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --query-service ftp ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent ##永久开放ftp服务
firewall-cmd --remove-service=ftp --permanent ##永久移除ftp服务
firewall-cmd --add-port=80/tcp --permanent ##永久添加80端口
firewall-cmd --remove-port=80/tcp --permanent ##永久移除80端口
firewall-cmd --list-ports ##查看已经开放的端口
iptables -L -n ##查看规则,这个命令是和iptables的相同的
man firewall-cmd ##查看帮助