CentOS:全名为“社区企业操作系统(Community Enterprise Operating System)”,提供长期免费升级和更新服务,自由使用。国内最大的服务器操作系统,现在基本所有的互联网公司后台服务器都采用CentOS。
自CentOS7后跟以前版本有了很大变化,此文详细的列出需要注意的地方.
禁止root使用ssh登入
vim /etc/ssh/sshd_config
查找:#PermitRootLogin yes
修改为 PermitRootLogin no
重启sshd服务:systemctl restart sshd
对于一些需要root权限才能修改的目录(如:/var/www/html),可使用 ACL 给普通用户单独赋值
setfacl -m user:ryan:rwx /var/www/html
使用非常规的ssh端口
vim /etc/ssh/sshd_config
查找:#Port 22
修改为 Port 9999
可以把9999改为任何1024 – 65535之间的任何数字
重启动sshd服务:systemctl restart sshd
最后是设定防火墙放行新端口:
firewall-cmd --zone=public --add-port=9999/tcp --permanent
完成后不要登出系统,使用另一个视窗尝试登入,例如:ssh -p 9999 ryan@ip
登录正常即可放心登出。
开启防火墙
查看一下防火墙现在编辑了哪些服务和端口:
firewall-cmd --list-all
一般防火墙只开启了DHCP 客户端和ssh 两个服务的通讯端口,倘若日后安装了其他网络软件,如web服务器,必须要检查安装程式有否编辑他们的通讯端口,没有的话便要手动编辑。firewall几个常用命令:
#查看防火墙状态
firewall-cmd --state
#启动防火墙
systemctl start firewalld
#停止防火墙
systemctl stop firewalld
#重启防火墙
systemctl restart firewalld
#设置开机自启
systemctl enable firewalld
#查看防火墙设置开机自启是否成功
systemctl is-enabled firewalld
#添加删除一些基本的端口
firewall-cmd --zone=public --add-service=ftp --permanent
firewall-cmd --zone=public --remove-service=ftp --permanent
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
#防火墙重新加载
firewall-cmd --reload
#查看放行的端口
firewall-cmd --list-ports
直接修改配置文件
用户自定义的规则文件/etc/firewalld/zones/public.xml
修改该配置文件,来添加8080端口。修改后的内容为
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connec
tions are accepted.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
<port protocol="tcp" port="8080"/>
</zone>
关闭SElinux
这个是一个安全服务,但是配置复杂,会影响很多软件的部署,如samba会被阻止访问。
vi /etc/selinux/config
找到#SELINUX=enforcing
,改成SELINUX=disabled
,保存。这个操作需要重启才能永久生效,所以可以临时关闭一下。
执行命令setenforce 0
查看是否生效的命令是: sestatus
再把 selinux-policy 卸载: yum remove selinux-policy
Centos 7 开机一直转圈 提示failed to load SELinux policy freezing的解决方法
在一直转圈停留界面处点击键盘Esc键,即可看到 failed to load SELinux policy. freezing.
解决方法如下:
1、重启系统,进入boot选择系统内核的界面,按e键,找到linux16 那一行,在language 后面 也就是LANG=zh_CN.UTF-8,加上空格,再加上selinux=0。 之后按ctrl+x,稍等片刻即可进入系统登录界面;
一些优化
/etc/security/limits.conf
文件追加
#wjw_add
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
安装一些小工具
先安装额外的仓库源
cd /etc/yum.repos.d
mv ./CentOS-Media.repo ./CentOS-Media.repo.BAK
yum install epel-release -y
epel是社区强烈打造的免费开源发行软件包版本库。
EPEL,即Extra Packages for Enterprise Linux的简称,是为企业级Linux提供的一组高质量的额外软件包,包括但不限于Red Hat Enterprise Linux (RHEL)
如果在安装系统的时候选择minimal,会比之前6的时候以更小的包来安装,比如:vim、ifconfig、route、setup、netstat等等很多命令都没有了,在安装系统后可加入以下软件包:
yum install -y htop lrzsz tree net-tools nmap vim bash-completion lsof dos2unix nc telnet ntp wget rng-tools psmisc screen
htop 图示化查看系统负载
lrzsz 支持windowns平台的上传下载linux。在windowns远程连接工具上可以使用。上传:rz
; 下载:sz filename
net-tools CentOS 7.2.11 默认没有ifconfig命令。net-tools提供一些网络命令(netstat)。
bash-completion 自动命令补全的工具。
rng-tools 生成随机数嫡池的一个工具,有了这个工具tomcat 启动会变得非常快。
psmisc 这个包含有killall命令。
screen 可以新建一个窗口,把任务放在后台运行。
字符集修改
字符集配置文件: /etc/locale.conf
设置字符集: localectl set-locale LANG=zh_CN.UTF-8
查看字符集: localectl status
修改hostname
[root@centos7 ~]$ hostnamectl set-hostname centos77.magedu.com # 使用这个命令会立即生效且重启也生效
[root@centos7 ~]$ hostname # 查看下
centos77.magedu.com
[root@centos7 ~]$ vim /etc/hosts # 编辑下hosts文件, 给127.0.0.1添加hostname
[root@centos7 ~]$ cat /etc/hosts # 检查
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 centos77.magedu.com
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
安装图形界面
如果安装的时候不是图形界面,现在又要想使用图形界面,那么:
1: 先安装带pinyin的所有安装包
yum install *pinyin*
2:安装X Window System,命令如下:
yum groupinstall "X Window System"
大概200个文件,会经过下载、安装、校验几个过程。安装完成会出现complete
3:然后安装图形界面软件,命令如下:
yum groupinstall "GNOME Desktop"
790多个文件,会稍微慢一点,和1一样,安装完成会出现complete
4:在命令行中输入init 5
,就可以进入图形化环境了.
记住一定不要使用startx,否则界面菜单是英文的且不能输入中文!
打开中文输入法
centos7自带中文输入法,可能我们在安装时会跳过选择汉语拼音,我们来重新设置一下吧
假如你在命令行界面,输入Ctrl+Alt+F1进入图形界面
点击左上角系统工具 --》 设置 --》 区域和语言,选择汉语(Intelligent Pinyin)
好啦!最后点击桌面右上角切换至此中文输入法。(注意:不同输入法之间切换快捷键为win+空格
,二者之间切换为win+shift
)在终端下试试这个汉语拼音输入法吧
在此输入法下,按左shift
快捷键可中英互换哦!(桌面右上角也可切换)。
假如系统没有安装汉语拼音,可以在终端输入命令安装: yum install ibus-libpinyin
如果系统按照中文环境安装完成后还是不能出现输入法,在系统【设置】->【Region&Language】->【输入源】+ 添加汉语输入源后,仍然不能输入汉语,那么:查看【设置】->【Region&Language】->【输入源】->【汉语(中国)】里面,有没有添加【汉语(intelligent Pinyin)】选项
注意:是汉语(intelligent Pinyin)
选项。添加成功,点击齿轮图标进入输入法设置。
开机启动管理
方法一:(rc.local)
1、因为在centos7中/etc/rc.d/rc.local
的权限被降低了,所以需要赋予其可执行权
chmod +x /etc/rc.d/rc.local
2、赋予脚本可执行权限
假设/usr/local/script/autostart.sh
是你的脚本路径,给予执行权限
chmod +x /usr/local/script/autostart.sh
3、打开/etc/rc.d/rc.local
文件,在末尾增加如下内容
/usr/local/script/autostart.sh
方法二:(chkconfig)
1、将脚本移动到/etc/rc.d/init.d
目录下
mv /usr/local/script/autostart.sh /etc/rc.d/init.d
2、增加脚本的可执行权限
chmod +x /etc/rc.d/init.d/autostart.sh
3、添加脚本到开机自动启动项目中
cd /etc/rc.d/init.d
chkconfig --add autostart.sh
chkconfig autostart.sh on
chkconfig启动脚本规范 在脚本开头加入下面内容:
#!/bin/sh
#chkconfig:2345 80 90
#decription:autostart
说明: chkonfig后面是启动级别和优先级,description后面是服务描述。如上面脚本意思是,
服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,停止的优先级是10。
优先级范围是0-100,数字越大,优先级越低。
方法三:(systemctl)
#设置开机自启
systemctl enable ${服务名}
#例如: systemctl enable mysqld.service
#查看开机自启是否成功
systemctl is-enabled ${服务名}
运行级别runlevel
/etc/inittab
是无效的被system target替代
1: 临时切换
# #切换到5(图形界面)
init 5
#切换到3(终端界面)
init 3
2: 永久切换,需要重启.
#切换到5(图形界面)
systemctl set-default graphical.target
#切换到3(终端界面)
systemctl set-default multi-user.target
只有五种运行级别
[root@centos7 ~]# ls -lh /usr/lib/systemd/system/runlevel*.target
lrwxrwxrwx. 1 root root 15 Apr 20 22:31 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Apr 20 22:31 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Apr 20 22:31 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 20 22:31 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 20 22:31 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Apr 20 22:31 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Apr 20 22:31 /usr/lib/systemd/system/runlevel6.target -> reboot.target
配置yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
官方epel源
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
配置IP地址
进入网卡配置文件目录cd /etc/sysconfig/network-scripts/
centos7默认没有ifconfig命令,需yum install net-tools
安装; 可通过命令ip addr
查看
动态获取ip(前提是你的路由器已经开启了DHCP)
修改网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens32
(最后一个为网卡名称)
动态获取IP地址需要修改两处地方即可
(1) bootproto=dhcp
(2) onboot=yes
修改后重启一下网络服务即可
systemctl restart network
配置静态IP地址
设置静态IP地址与动态iIP差不多,也是要修改网卡配置文件vi /etc/sysconfig/network-scripts/ifcfg-ens32
(最后一个为网卡名称)
(1)bootproto=static
(2)onboot=yes
(3)在最后加上几行,IP地址、子网掩码、网关、dns服务器
IPADDR=192.168.1.160
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
DNS2=8.8.8.8
修改后重启一下网络服务即可
systemctl restart network
禁用ipv6
编辑文件/etc/sysctl.conf
,添加下面的行:
net.ipv6.conf.all.disable_ipv6 =1
net.ipv6.conf.default.disable_ipv6 =1
如果你想要为特定的网卡禁止IPv6,比如,对于enp0s3
,添加下面的行。
net.ipv6.conf.enp0s3.disable_ipv6 =1
保存并退出文件。执行下面的命令来使设置生效。
sysctl -p
systemctl restart network
服务管理
centos6 采用 service和chconfig管理服务
centos7 采用systemctl 管理服务
下面以mysqld服务为实例
#显示状态
systemctl status mysqld.service
#启动
systemctl start mysqld.service
#停止
systemctl stop mysqld.service
#设置开机启动
systemctl enable mysqld.service
#关闭开机启动
systemctl disable mysqld.service
#查看开机自启是否成功
systemctl is-enabled mysqld.service
centos7查看系统开机时间占用(优化开机)
systemd-analyze time
systemd-analyze blame
文件结构变化
centos6的/bin, /sbin, /lib, and /lib64在/
下
centos7的/bin, /sbin, /lib, and /lib64移到/usr
下