centos修改limit centos如何安装软件
很多朋友对于centos修改limit和centos如何安装软件不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
CentOS关于quota的总结与实践详解
1理论基础
1.1 Quota的概念
Quota即限额的意思,用来限制用户、组、文件夹的空间使用量。
1.2用途范例
- web服务器控制站点可用空间大小
- mail服务器控制用户可用空间大小
- file服务器控制用户可用空间大小
1.3限制模式
-根据用户(UID)控制每个用户的可用空间大小
-根据组(GID)控制每个组的可用空间大小
-根据目录(directory,project)控制每个目录的可用空间大小(xfs可用project模式)
1.4使用条件
- EXT格式只能对文件系统进行限制,xfs可用对project进行限制
-内核需要预开启对Quota支持
- Quota限制只对非管理员有效
-默认只开启对/home使用Quota,其他需要配置SELinux
1.5限制的可配置对象
-根据用户(User)、组(Group)、特定目录(project)
-容量限制或文件数量限制(block/inode)
-限制值soft(超过空间用量给予警告和宽限时间)和hard(超过空间用量则剥夺用户使用权)
-宽限时间(grace time),空间用量超出soft限定而未达到hard限定给予的处理时限(超出时限soft值变成hard值)
2实际操作
2.1配置前准备
2.1.1建立用户组
groupadd gp1
2.1.2添加组成员
useradd-g gp1 user1
echo pwd1| passwd--stdin user1
useradd-g gp1 user2
echo pwd1| passwd--stdin user2
2.1.2创建用户目录并变更所有组
mkdir/home/gp1
chgrp gp1/home/gp1
chmod 2770/home/gp1
2.1.2检查文件系统类型
df-hT/home
显示如下:
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-home xfs 5.0G 67M 5.0G 2%/home
2.2启用文件系统的quota功能
2.2.1编辑fstab
vim/etc/fstab
修改内容如下:
/dev/mapper/centos-home/home xfs defaults,usrquota,grpquota 0 0
注,类型如下:
-根据用户(uquota/usrquota/quota)
-根据组(gquota/grpquota)
-根据目录(pquota/prjquota)(不能与grpquota同时设定)
2.2.2卸载并重新挂载
umount/home
mount-a
2.2.3检查
mount| grep home
显示如下:
/dev/mapper/centos-home on/home type xfs(rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
2.3查阅Quota信息
2.3.1命令格式
xfs_quota-x-c子命令 [挂载点]
选项:
-x:专家模式,使用-x才能使用-c
-c:子命令选项
子命令:
print:列出当前系统参数等
df:类似于df,选项有-b(block)-i(inode)-h(加上单位)等
report:列出quota项目,包含-ugr(user/group/project)和-bi等
state:列出当前支持quota文件系统信息和相关的启动项
2.3.2查询支持Quota的分区
xfs_quota-x-c print
2.3.3查询Quota目录的使用情况
xfs_quota-x-c df-h/hom
2.3.4显示用户的Quota的限制信息
xfs_quota-x-c report-ubih/home
注,显示项目加参数-u
2.4配置限制
2.4.1命令格式:
xfs_quota-x-c limit [-ug] b[soft|hard]=N i[soft|hard]=N name
xfs_quota-x-c timer [-ug] [-bir] Ndays
选项:
limit:限制的对象,包括user/group/project
bsoft/bhard: block的soft/hard限制值
isoft/ihard: inode的soft/hard限制值
name:用户和组的名称
timer:宽限时间(grace time)
2.4.2根据用户和块大小限制
xfs_quota-x-c limit-u bsoft=250M bhard=300M user1/home
xfs_quota-x-c limit-u bsoft=250M bhard=300M user2/home
检查配置:
xfs_quota-x-c report-ubih/home
2.4.3根据组和块大小限制
xfs_quota-x-c limit-g bsoft=950M bhard=1G gp1/home
检查配置:
xfs_quota-x-c report-gbih/home
2.4.5配置宽限时间
xfs_quota-x-c timer-ug-b 14days/home
验证配置:
xfs_quota-x-c state/home
2.4.6验证Quta
su- user1
dd if=/dev/zero of=123.img bs=1M count=310
ll-h
exit
xfs_quota-x-c report-ubh/home
2.5根据project限制
2.5.1修改fstab
vim/etc/fstab
2.5.2卸载挂载并重新挂载
umount/home
mount-a
2.5.3检查取消
xfs_quota-x-c state
2.5.4创建专案存储位置
mkdir/home/proj01
2.5.5指定项目识别号
echo 01:/home/proj01/etc/projects
2.5.6指定项目名称并关联项目识别号
echo proj01:01/etc/projid
2.5.7初始化项目名称
xfs_quota-x-c project-s proj01
检查配置:
xfs_quota-x-c print/home
xfs_quota-x-c report-pbih/home
2.5.8根据块大小配置限制
xfs_quota-x-c limit-p bsoft=450M bhard=500M proj01/home
检查配置:
xfs_quota-x-c report-pbih/home
2.5.9验证配置
dd if=/dev/zero of=/home/myquota/123.img bs=1M count=510
2.6 Quota的管理
2.6.1临时禁用Quota限制
xfs_quota-x-c disable-up/home
检查禁用:
xfs_quota-x-c state/home
验证禁用:
dd if=/dev/zero of=/home/user1/123.img bs=1M count=520
查阅Quota状态:
xfs_quota-x-c report-pbh/home
清理测试文件:
rm-rf/home/user1/123.img
2.6.2临时启动Quota限制
xfs_quota-x-c enable-up/home
检验启动:
dd if=/dev/zero of=/home/user1/123.img bs=1M count=520
2.6.3完全关闭Quota限制
xfs_quota-x-c off-up/home
测试关闭:
xfs_quota-x-c enable-up/home
恢复关闭:
umount/home; mount-a
2.6.4删除Quota限制(无法恢复)
xfs_quota-x-c off-up/home
xfs_quota-x-c remove-p/home
验证删除:
xfs_quota-x-c report-phb/home
2.7利用软连接实现Quota
1)启用/home分区的Quota功能
编辑fstab
vim/etc/fstab
修改内容如下:
/dev/mapper/centos-home/home xfs defaults,usrquota,grpquota 0 0
2)创建软连接到Quota分区
ln-s/home/mail/var/spool/mail
centos7.5普通用户登录提示 cannot modify limit
CENTOS7.5普通用户登录的时候有个提示-bash: ulimit: open files: cannot modify limit: Operation not permitted
一般来说我们都是修改文件/etc/security/limits.conf,我在文件中的配置如下
soft nproc 65535
hard nproc 65535
soft nofile 65535
hard nofile 65535
通过命令ulimit-a查到的结果如下,与配置不符。
open files(-n) 400000
以为是配置失效,多次修改无法解决,突然想起修改openfile的另外一个方法修改/etc/profile,果然看到末行有如下内容,将其注释即可,问题解决。
ulimit-n 400000
【实用】CentOS 7系统优化脚本
作为一名运维人员,部署操作系统的任务经常重复,操作内容大同小异。为此,可以将相同的操作制作成统一执行的脚本,通过手动输入不同部分实现自动化操作,节省大量时间。最近发现了一款好用的shell源码,稍作修改后分享给大家。
脚本主要实现系统优化功能,包括修改字符集、关闭selinux、关闭防火墙、安装常用工具和加快ssh登录等。
脚本结构如下:
1.主菜单
2.二级菜单
主要实现系统优化,如修改字符集、关闭selinux、关闭防火墙、安装常用工具和加快ssh登录等功能。
脚本代码如下:
sh
#!/bin/sh
./etc/rc.d/init.d/functions
exportLANG=zh_CN.UTF-8
#一级菜单
menu1(){
clear
cat</etc/locale.conf</dev/null
echo'#firewall-cmd--state'
firewall-cmd--state
systemctldisablefirewalld.service&>/dev/null
echo'#systemctllist-unit-files|grepfirewalld'
systemctllist-unit-files|grepfirewalld
action"完成禁用firewalld,生产环境下建议启用!"/bin/true
echo"==========================================================="
sleep5
}
#精简开机启动
chkset(){
echo"=======================精简开机启动========================"
systemctldisableauditd.service
systemctldisablepostfix.service
systemctldisabledbus-org.freedesktop.NetworkManager.service
echo'#systemctllist-unit-files|grep-E"auditd|postfix|dbus-org\.freedesktop\.NetworkManager"'
systemctllist-unit-files|grep-E"auditd|postfix|dbus-org\.freedesktop\.NetworkManager"
action"完成精简开机启动"/bin/true
echo"==========================================================="
sleep2
}
#修改文件描述符
limitset(){
echo"======================修改文件描述符======================="
echo'*-nofile65535'>/etc/security/limits.conf
ulimit-SHn65535
echo"#cat/etc/security/limits.conf"
cat/etc/security/limits.conf
echo"#ulimit-Sn;ulimit-Hn"
ulimit-Sn;ulimit-Hn
action"完成修改文件描述符"/bin/true
echo"==========================================================="
sleep2
}
#安装常用工具及修改yum源
yumset(){
echo"=================安装常用工具及修改yum源==================="
yuminstallwget-y&>/dev/null
if[$?-eq0];then
cd/etc/yum.repos.d/
\cpCentOS-Base.repoCentOS-Base.repo.$(date+%F)
ping-c1mirrors.aliyun.com&>/dev/null
if[$?-eq0];then
wget-O/etc/yum.repos.d/CentOS-Base.repo
yumcleanall&>/dev/null
yummakecache&>/dev/null
else
echo"无法连接网络"
exit$?
fi
else
echo"wget安装失败"
exit$?
fi
yum-yinstallntpdatelsofnet-toolstelnetvimlrzsztreenmapncsysstat&>/dev/null
action"完成安装常用工具及修改yum源"/bin/true
echo"==========================================================="
sleep2
}
#优化系统内核
kernelset(){
echo"======================优化系统内核========================="
chk_nf=`cat/etc/sysctl.conf|grepconntrack|wc-l`
if[$chk_nf-eq0];then
cat>>/etc/sysctl.conf</dev/null
if[$?-eq0];then
/usr/sbin/
echo"*/5****/usr/sbin/ntpdatentp.aliyun.com&>/dev/null">>/var/spool/cron/root
else
echo"ntpdate安装失败"
exit$?
fi
action"完成设置时间同步"/bin/true
echo"==========================================================="
sleep2
}
#history优化
historyset(){
echo"========================history优化========================"
chk_his=`cat/etc/profile|grepHISTTIMEFORMAT|wc-l`
if[$chk_his-eq0];then
cat>>/etc/profile<<'EOF'
#设置history格式
exportHISTTIMEFORMAT="[%Y-%m-%d%H:%M:%S][`whoami`][`whoami|awk'{print$NF}'|sed-r's#[()]##g'`]:"
#记录shell执行的每一条命令
exportPROMPT_COMMAND='\ if[-z"$OLD_PWD"];then exportOLD_PWD=$PWD; fi; if[!-z"$LAST_CMD"&&["$(history1)"!="$LAST_CMD"];then logger-t`whoami`_shell_dir"[$OLD_PWD]$(history1)"; fi; exportLAST_CMD="$(history1)"; exportOLD_PWD=$PWD;' EOF
source/etc/profile
else
echo"优化项已存在。"
fi
action"完成history优化"/bin/true
echo"==========================================================="
sleep2
}
main(){
menu1
case$num1in
1) localeset selinuxset firewalldset chkset limitset yumset kernelset sshset restartset ntpdateset historyset;;
2) menu2
case$num2in
1) localeset;;
2) selinuxset;;
3) firewalldset;;
4) chkset;;
5) limitset;;
6) yumset;;
7) kernelset;;
8) sshset;;
9) restartset;;
10) ntpdateset;;
11) historyset;;
12) main;;
13) exit;;
*) echo'Pleaseselectanumberfrom[1-13].';;
esac
;;
3) exit;;
*) echo'Err:Pleaseselectanumberfrom[1-3].' sleep3 main;;
esac
}
main$*
将脚本保存为init.sh,赋予执行权限并执行即可。
chmod+xinit.sh&&./init.sh
通过一键命令执行,同样能达到脚本的效果:
bash-c"$(curl-Ls.aaa.al/init.sh)"
如需新增功能,可在脚本基础上进行修改实现。
本书从实用角度出发,结合实际应用案例,模拟真实的系统环境,介绍电脑的使用方法与技巧,旨在帮助读者全面、系统地掌握电脑的应用。书中“高手支招”板块提供大量实用技巧,解决日常工作中遇到的常见问题。