linux 初始化系统配置(centos6)

来源:互联网 发布:java获取文件类型 编辑:程序博客网 时间:2024/05/14 16:06

作者:zhouxingfu520

转自:http://www.iteye.com/topic/1125235


     linux 初始化系统配置(centos6)  在配置服务器之前一般都需要根据自己的需要对系统进行初始化的一些工作,我自己在配置服务器之前都会做些初始化的配置 这里主要包括5个设置:

 

1.  限制某些用户使用su命令

2.  限制只能wheel组能使用sudo命令

3.  演示让普通用户使用root命令(自己玩的使用)

4.  配置服务 关闭一些你不需要的服务

5.  更新yum在线库 寻找最快的软件仓库

6.  关闭SELINUX

7.  对linux的文件权限补充(对于服务器配置来说很重要的知识点)




1 控制cent用户能使用su 命令 登入root用户

 

[root@bogon ~]# useradd cent添加用户
[root@bogon ~]#passwd cent 设置密码一样

[root@bogon ~]#vi /etc/group编辑cent为wheel组

wheel:x:10:root,cent

[root@bogon ~]#vi /etc/pam.d/su编辑只能wheel组的用户能使用su命令

auth required pam_wheel.so use_uid

[root@bogon ~]#vi /etc/aliases

root:cen


------------------------------------------

2 控制只能wheel组能使用sudo命令

[root@bogon ~]#vi /etc/sudoers


root    ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
%wheel ALL=(ALL)       ALL  添加wheel组能使用sudo命令

# Same thing without a password
%wheel ALL=(ALL)       NOPASSWD: ALL

 

下面自定义一些权限控制 比如控制所有用户除了root都不能执行 关机 重启命令

Cmnd_Alias SHUTDOWN = /sbin/halt, /sbin/shutdown, \
/sbin/poweroff, /sbin/reboot, /sbin/init
# add ( commands in aliase 'SHUTDOWN' are not allowed )

cent ALL=(ALL) ALL,!SHUTDOWN  不能执行关机命令



sudo是linux下常用的允许普通用户使用超级用户权限的工具。

它的主要配置文件是sudoers,linux下通常在/etc目录下,如果是solaris,sudo都提供了一个编辑该文件的命令:visudo来对该文件进行修改。强烈推荐使用该命令修改sudoers,因为它会帮你校验文件配置是否正确,如果不正确,在保存退出时就会提示你哪段配置出错的。
言归正传,下面介绍如何配置sudoers


首先写sudoers的缺省配置:

##################################################
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification

# User alias specification

# Cmnd alias specification

# Defaults specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

# Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now
##################################################


------------------------------------------


3  最简单的配置,让普通用户support具有root的所有权限

[root@bogon ~]#visudo可以看见缺省只有一条配置:
root    ALL=(ALL) ALL
那么你就在下边再加一条配置:
support ALL=(ALL) ALL
这样,普通用户support就能够执行root权限的所有命令



以support用户登录之后,执行:
sudo su -
然后输入support用户自己的密码,就可以切换成root用户了

 让普通用户support只能在某几台服务器上,执行root能执行的某些命令
首先需要配置一些Alias,这样在下面配置权限时,会方便一些,不用写大段大段的配置。Alias主要分成4种

Host_Alias
Cmnd_Alias
User_Alias
Runas_Alias

1) 配置Host_Alias:就是主机的列表
Host_Alias      HOST_FLAG = hostname1, hostname2, hostname3
2) 配置Cmnd_Alias:就是允许执行的命令的列表
Cmnd_Alias      COMMAND_FLAG = command1, command2, command3
3) 配置User_Alias:就是具有sudo权限的用户的列表
User_Alias USER_FLAG = user1, user2, user3
4) 配置Runas_Alias:就是用户以什么身份执行(例如root,或者oracle)的列表
Runas_Alias RUNAS_FLAG = operator1, operator2, operator3
5) 配置权限
配置权限的格式如下:

USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG
如果不需要密码验证的话,则按照这样的格式来配置
USER_FLAG HOST_FLAG=(RUNAS_FLAG) NOPASSWD: COMMAND_FLAG

配置示例:
##################################################
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification
Host_Alias      EPG = 192.168.1.1, 192.168.1.2  允许这两个ip登入的主机执行root命令

# User alias specification

# Cmnd alias specification
Cmnd_Alias      SQUID = /opt/vtbin/squid_refresh, /sbin/service, /bin/rm 允许执行的root命令

# Defaults specification

# User privilege specification
root    ALL=(ALL) ALL
support EPG=(ALL) NOPASSWD: SQUID   support用户也可以执行root的命令了

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

# Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now


------------------------------------------

4 配置服务

chkconfig --list 查看系统服务
chkconfig netfs off 停止netfs服务

停止打印服务
  如果不准备提供打印服务,停止默认被设置为自动启动的打印服务。

[root@bogon ~]#/etc/rc.d/init.d/cups stop ← 停止打印服务
Stopping cups:            [ OK ]    ← 停止服务成功,出现“OK”

[root@bogon ~]#chkconfig cups off ← 禁止打印服务自动启动

[root@bogon ~]#chkconfig --list cups ← 确认打印服务自启动设置状态
cups 0:off 1:off 2:off 3:off 4:off 5:off 6:off  ← 0-6都为off的状态就OK(当前打印服务自启动被禁止中)

[10] 停止ipv6

  在CentOS默认的状态下,ipv6是被启用的状态。因为我们不使用ipv6,所以,停止ipv6,以最大限度保证安全和快速。

  首先再次确认一下ipv6功能是不是被启动的状态。


[root@sample ~]# ifconfig -a ← 列出全部网络接口信息

eth0 Link encap:Ethernet HWaddr 00:0C:29:B6:16:A3
inet addr:192.168.0.13 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb6:16a3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:84 errors:0 dropped:0 overruns:0 frame:0
TX packets:93 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10288 (10.0 KiB) TX bytes:9337 (9.1 KiB)
Interrupt:185 Base address:0x1400

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:952 (952.0 b) TX bytes:952 (952.0 b)
sit0 Link encap:IPv6-in-IPv4 ← 确认ipv6是被启动的状态
NOARP MTU:1480 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

  然后修改相应配置文件,停止ipv6。

[root@sample ~]# vi /etc/modprobe.conf ← 修改相应配置文件,添加如下行到文尾:

alias net-pf-10 off
alias ipv6 off

[root@bogon ~]#shutdown -r now ← 重新启动系统,使设置生效

  最后确认ipv6的功能已经被关闭。


[root@sample ~]# ifconfig -a ← 列出全部网络接口信息

eth0 Link encap:Ethernet HWaddr 00:0C:29:B6:16:A3
inet addr:192.168.0.13 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb6:16a3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:84 errors:0 dropped:0 overruns:0 frame:0
TX packets:93 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10288 (10.0 KiB) TX bytes:9337 (9.1 KiB)
Interrupt:185 Base address:0x1400 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:952 (952.0 b) TX bytes:952 (952.0 b)

(确认ipv6的相关信息没有被列出,说明ipv6功能已被关闭。)


CentOS Server首次安装后的关闭不需要的服务

service smartd stop
chkconfig smartd off

服务说明:
smartd检查硬盘故障,虚拟的硬盘是无法用这个服务来检查,只对实体的装置才有用。

1.停止服务

service apmd stop
service autofs stop
service bluetooth stop
service cups stop
service ip6table stop
service iptable stop
#service isdn stop
service hidd stop
service pcscd stop
service pcmcia off
#service sendmail stop
service yum-updatesd stop


2.设定不启动
chkconfig apmd off
chkconfig autofs off
chkconfig bluetooth off
chkconfig cups off
chkconfig ip6table off
chkconfig iptable off
#chkconfig idsn off
chkconfig hidd off
chkconfig pcscd off
service pcmcia off
#chkconfig sendmail off
chkconfig yum-updatesd off


服务说明:
apmd电源管理
没有UPS或是没有电池的电脑这是没用的

autofs自动挂载服务
通常server所需要挂载都是固定的,除非是特殊的目的,否则用处不大。

bluetooth蓝芽

Server应该是不太需要用蓝芽

cups列印
如果列印需求的机器才需要打开

iptable6,iptable
建议交由防火墙来管理连线, Server专注服务即可(这个工具自己需要)

isdn
如果有需要建立ISDN的环境,才需要使用

hidd
一些蓝芽的输入装置,如键盘滑鼠等。 Server应该不太会用到这些装置,有点太高档的感觉!

pcscd
给smart card用的,目前手上没有这种机器,所以都是关掉的。

pcmcia
给pcmcia用的,目前Server很少用到pcmcia,关掉。

yum-updatesd
yum自动升级程式, CentOS 5预设会安装,不过有时候自动安装会造成一些不必要的困扰,目前大部分的人建议是不要启用,尤其是已经上线的主机。


------------------------------------------

5 更新yum在线下载库链接   自动寻找最快的资源库

yum -y install yum-plugin-fastestmirror

yum -y update

配置yum第三方资源库 
主要作用是官方没有的包 会去第三方查找相关的包

[centos6.0  x86 32位]使用第三方软件仓库|使用RPMForge软件库

在centos下运行yum install flash-plugin,或者 yum install mplayer 的时候,命令无效?为什么会这样?因为centos是rhel编译过来的,去掉了所有关于版权问题的东西。因此,在没有

使用第三方软件库的情况下,很多软件无法用yum install来直接安装。

RPMForge拥有4000多种CentOS的软件包,被CentOS社区认为是最安全也是最稳定的一个软件仓库。
下面,我就来讲解下如何使用RPMForge软件仓库:

一、安装yum-priorities插件。

这个插件是用来设置yum在调用软件源时的顺序的。因为官方提供的软件源,都是比较稳定和被推荐使用的。因此,官方源的顺序要高于第三方源的顺序。如何保证这个顺序,就需要安装yum-

priorities这插件了。

[root@bogon ~]#yum install yum-priorities    #安装yum-priorities

安装完后需要设置/etc/yum.repos.d/ 目录下的.repo相关文件(如CentOS-Base.repo),在这些文件中插入顺序指令:priority=N  (N为1到99的正整数,数值越小越优先)

一般的配置是这样的:
[base], [addons], [updates], [extras] ... priority=1
[centosplus],[contrib] ... priority=2
其他第三的软件源为:priority=N  (推荐N>10)

如我的CentOS-Base.repo配置为:

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
priority=1

#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
priority=1

#packages used/produced in the build but not released
[addons]
name=CentOS-$releasever - Addons
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons
#baseurl=http://mirror.centos.org/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
priority=1

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
priority=1

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
priority=2

二、下载与安装相应rpmforge的rpm文件包

1,下载rpmforge的rpm文件包
32位系统

[root@bogon ~]# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
64位系统
[root@bogon ~]# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
如果您不清楚您的系统内核,你可以使用uname -i命令来查看您的内核信息。

2,安装DAG的PGP Key

[root@bogon ~]# rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt

3,验证rpmforge的rpm文件包
[root@bogon ~]# rpm -K rpmforge-release-0.5.2-2.el6.rf.*.rpm

4,安装rpmforge的rpm文件包

[root@bogon ~]# rpm -i rpmforge-release-0.5.2-2.el6.rf.*.rpm

5,设置/etc/yum.repos.d/rpmforge.repo文件中源的级别,就是添加priority=11这句。
如我的rpmforge.repo文件内容:

### Name: RPMforge RPM Repository for Red Hat Enterprise 6 - dag
### URL: http://rpmforge.net/
[rpmforge]
name = Red Hat Enterprise $releasever - RPMforge.net - dag
baseurl = http://apt.sw.be/redhat/el5/en/$basearch/rpmforge
mirrorlist = http://apt.sw.be/redhat/el5/en/mirrors-rpmforge
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1
priority=11

[rpmforge-extras]
name = RHEL $releasever - RPMforge.net - extras
baseurl = http://apt.sw.be/redhat/el5/en/$basearch/extras
mirrorlist = http://apt.sw.be/redhat/el5/en/mirrors-rpmforge-extras
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge-extras
enabled = 0
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1
priority=11

[rpmforge-testing]
name = RHEL $releasever - RPMforge.net - testing
baseurl = http://apt.sw.be/redhat/el5/en/$basearch/testing
mirrorlist = http://apt.sw.be/redhat/el5/en/mirrors-rpmforge-testing
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge-testing
enabled = 0
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1
priority=11


三、测试

1,测试升级

[root@bogon ~]#yum check-update

输出信息应该会有以下两行:
Loading "priorities" plugin
...
XX packages excluded due to repository priority protections

2,安装下xmms音乐播放器试试,如果安装成功了就没有问题了。 xmms音乐播放器默认不在基础库
[root@bogon ~]#yum install xmms
[root@bogon ~]# yum install xmms-mp3   #安装xmms的mp3播放插


------------------------------------------

6 如果不是很熟悉selinux

 

关闭selinux
[root@bogon ~]# vi /etc/selinux/config

其他内容全部注释 加上以下内容
SELINUX=disabled


------------------------------------------

7 文件/目录权限  很重要的知识点补充
 

[root@bogon ~]#  ls -l 中显示的内容: 

-rw-r--r--   1    root   root    42304   Sep  4 18:26 install.log



第一栏代表这个文件的类型与权限(permission)
  drwxr-xr-x  第一个字符为类型 是否为文件或者目录 -d为目录
  后三个字符为当前用户拥有的权限  再后三个字符为与创建者所在组的用户权限 再后为其他用户


    第一个属性代表这个文件是『目录、文件或连结文件』:
        当为[ d ]则是目录,例如上表的第 11 行;
        为[ - ]则是文件,例如上表的第  5 行;
        若是[ l ]则表示为连结文件(link file);
        若是[ b ]则表示为装置文件里面的可供储存的接口设备;
        若是[ c ]则表示为装置文件里面的串行端口设备,例如键盘、鼠标。


    接下来的属性中,三个为一组,且均为『rwx』的三个参数的组合。其中,[ r ]代表可读、[ w ]代表可写、[ x ]代表可执行:
        第一组为『拥有人的权限』,以第五行为例,该文件的拥有人可以读写,但不可执行;
        第二组为『同群组的权限』;
        第三组为『其它非本群组的权限』


第二栏表示有多少文件名连结到此节点(i-node):
第三栏表示这个文件(或目录)的『拥有者账号』
第四栏表示这个文件的所属群组
第五栏为这个文件的容量大小,默认单位为bytes;
第六栏为这个文件的创建日期或者是最近的修改日期:
第七栏为这个文件的名称