记录一下自己在CentOS下的一些调整。我笔记本虚拟机的初始化操作的记录。
Systemd
这是CentOS 7最大改动的地方,让大家需要时间去熟悉
所有的服务
systemctl list-units --type service
启动的服务
systemctl list-unit-files --type=service | grep enabled
一个服务
# systemctl status <name>
# systemctl stop <name>
# systemctl disable <name>
# systemctl start <name>
# systemctl restart <name>
https://www.rayheffer.com/essential-linux-skills-with-centos-7-managing-services-with-systemd/
Chrony服务
CentOS 7 其实是建议采用chrony服务来实现同步时间。我采用最小化安装CentOS 7的时候,Chrony服务是没安装
yum install chrony
systemctl start chronyd
systemctl enable chronyd
默认同步时间的服务器是CentOS,你可以修改成国内的时间服务器
编辑 /etc/chrony.conf
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
这个时候,重启chronyd服务
systemctl restart chronyd
chronyc sources
这个时候你就会发现有国内的时间服务器。需要注意,如果你机器时间和标准时间差异很大,你需要强制同步
当你强制同步时间前,先要把chrony服务停止
systemctl stop chronyd
chronyd -q 'pool pool.ntp.org iburst'
强制同步时间后,再启动chrony服务
红帽的文档,是世界上最好的,准确的文档
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-using_chrony
chronyc tracking
chronyc sources
chronyc sourcestats
Date and Time
查看当前时间和时区
# timedatectl status
Local time: Tue 2017-12-12 18:02:46 CST
Universal time: Tue 2017-12-12 10:02:46 UTC
RTC time: Tue 2017-12-12 10:02:46
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
如果你希望手工修改时间,这个时候,你需要先停止同步时间
这个时候,你就可以设置时间
timedatectl set-time 17:00:00
如果启用ntp 服务
修改日期
timedatectl set-time ‘YYYY-MM-DD HH:MM:SS’
查看硬件时间
同步system时间到硬件
http://linuxtechlab.com/playing-date-time-rhelcentos/
搜索
我们经常需要搜索某个配置文件
find /etc -name "yum.*" -type f
很多技巧,-type f,搜索文件,-type d 搜索目录
还有一种简单办法,就是安装mlocate的包,可以直接搜索
更新数据库
updatedb
# locate yum.conf
/etc/yum.conf
/usr/share/man/man5/yum.conf.5
这种方式其实其实是不建议使用,还是使用find比较好。
查看一个文件是属于哪个rpm包
# rpm -qf /etc/yum.conf
yum-3.4.3-154.el7.centos.noarch
查看rpm包包含哪些文件,已经安装的rpm包
rpm -ql chrony
/etc/NetworkManager/dispatcher.d/20-chrony
/etc/chrony.conf
/etc/chrony.keys
如果需要搜索关键字
grep -rlw "tecadmin" /var/log
加上 -i参数,就是不区分大小写
https://tecadmin.net/find-all-files-containing-specific-text-on-linux/
内核版本
查看操作系统版本
cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
查看内核版本,现在内核的版本是
# uname -a
Linux server01.chenshake.com 3.10.0-693.el7.x86_64
#1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
以前内核版本号很难记忆,现在其实已经加上时间。这个内核版本,是8月22日的版本。这是centos 7.4默认的内核版本号。
如果升级一下,你会发现最新的版本号是
]# uname -a
Linux server01.chenshake.com 3.10.0-693.11.1.el7.x86_64
#1 SMP Mon Dec 4 23:52:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
升级
升级系统的包
可能会导致内核升级,需要重启机器才能生效。
升级操作系统版本,例如从7.3升级7.4
vim
编辑 /etc/profile
# add at the last line
alias vi='vim'
让设置马上生效
CentOS7 的vim 默认已经已经支持恢复到上次光标位置的功能,基本不需要啥调整就可以使用。
如果需要做修改配置就编辑 /etc/vimrc 文件就可以。
我增加搜索的时候忽略大小写。
Selinux
关闭selinux是一个惯例,包括检查selinux是否关闭。
# sestatus
SELinux status: enabled
关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
我的习惯就是重启机器,确保生效。
# sestatus
SELinux status: disabled
设置Repo
为了方便,其实最好是把红帽的yum的工具包都装上。
查看repo
cd /etc/yum.repos.d/
mkdir backup
move CentOS* ./backup/
设置阿里源
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
检查
yum clean all
yum makecache
yum list | wc -l
如果我们希望增加一个docker的repo,可以用这个方法
yum-config-manager \
--add-repo https://download.docker.com/linux/centos/docker-ce.repo
lrzsz
这个工具,其实非常方便xshell下非常方便,上传和下载文件。
关闭防火墙
systemctl status firewalld
systemctl disable firewalld.service
systemctl stop firewalld.service
主机名
需要搞清楚hostname和FQDN Name的区别
设置hostname
# cat /etc/hostname
server01
如果希望修改,可以使用
hostnamectl –static set-hostname server01
让修改马上生效
hostname -F /etc/hostname
设置FQDN
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.10 server01.chenshake.com server01
查看
# hostname
server01
hostname -f
server01.chenshake.com
-f,含义是FQDN,类似域名的名称。
通过hostnamectl 还能查看系统的整体状况,是vm还是物理机器。
# hostnamectl status
Static hostname: server01
Icon name: computer-vm
Chassis: vm
Machine ID: 7c4bea6eadac4a1cae068a167a0c961b
Boot ID: 173cfb58e1c948ce947bd8261b327f91
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-693.11.1.el7.x86_64
Architecture: x86-64
网络工具
一些常用的网络工具需要安装
- netstat 需要安装 net-tools
- nmap,需要安装 nmap
- lsof,需要安装lsof
yum install net-tools lsof nmap
使用
需要对参数有啥了解
- -n 表示不解析ip到域名,这个参数必须
- -l 表示显示打开的端口
- -p 表示显示端口应用名字
- -t 表示tcp协议
- -u 表示 udp协议
查看22端口情况
lsof 功能很强, 包括查看某个文件是否给某个进程使用
nmap其实是扫描工具,可以扫描自己,查看端口的情况
https://www.cyberciti.biz/faq/howto-install-nmap-on-centos-rhel-redhat-enterprise-linux/
关闭IPv6
编辑 /etc/sysctl.conf.
net.ipv6.conf.all.disable_ipv6 = 1
马上生效
对比一下
关闭前
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9e:48:a3 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.10/24 brd 192.168.100.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::7a67:a172:a8cd:635/64 scope link
valid_lft forever preferred_lft forever
关闭后
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9e:48:a3 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.10/24 brd 192.168.100.255 scope global ens33
valid_lft forever preferred_lft forever
http://aws-labs.com/disable-ipv6-centos-7-rhel-7/
最大打开文件数量
下面内容还没完全理解
在开发运维的时候我们常常会遇到类似“Socket/File: Can’t open so many files”,“无法打开更多进程”,这个时候,我们需要调整系统的默认参数。
file-max
内核可分配的最大文件数
# cat /proc/sys/fs/file-max
396334
系统最大打开文件描述符数
cat /proc/sys/fs/file-nr
960 0 396334
临时修改
echo 1000000 > /proc/sys/fs/file-max
永久性修改 在/etc/sysctl.conf中设置
单个进程最大打开文件描述符数,nr_open是单个进程可分配的最大文件数
soft link
hard link
修改
http://blog.csdn.net/superchanon/article/details/13303705
http://blog.51cto.com/qujunorz/1703295
https://unix.stackexchange.com/questions/127777/how-to-configure-the-process-open-file-limit-of-a-user