linux培训笔记3

selinux:
传统权限:以人为单位,chmod,chown;对用户做授权。
selinux权限:以对象为单位,(服务,文件,人),安全性更加细,到

达文件级别;至今还没有一套很好的管理工具。

ls -Z 查看文件或目录的selinux的信息。
ps -Z
ps -ef -Z 查看进程的selinux的信息。

getenforce 查看selinux的状态。
状态:
enforcing:强制,主动进行权限审核。
permissive:被动的进行权限审核。会记录权限的信息。
disabled:关闭。

设置selinux的状态:
# vim /etc/selinux/config
SELINUX=disabled ##将enforcing更改为disabled
SELINUXTYPE=targeted

临时关闭:
# setenforce 0 临时关闭
# setenforce 1 临时打开
# getenforce
Disabled


iptables防火墙。
# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination


IPTABLES防火墙共有4个表。
:filter 数据包过滤包的表。
:nat 地址转换的表。
:mangle 数据包处理的表
:raw 制定的规则的表

在每一个表中都有相应的链:
INPUT 输入的链,数据通过网卡进入系统。
FORWARD 转发的链
OUTPUT 输出的链,数据通过网卡出系统。
PREROUTING DNAT地址转换的链。
POSTROUTING SNAT地址转换的链。

所有规则都必须设置在指定的链上。
允许172.16.4.100主机访问本地的21端口。
iptables -t filter -A INPUT -s 172.16.4.100 -p tcp --dport 21

-j ACCEPT

拒绝172.16.4.47主机访问本机的22号端口。
iptables -t filter -A INPUT -s 172.16.4.47 -p tcp --dport 22

-j DROP|REJECT


配置iptables防火墙的规则。
方法一:setup 向导式配置。
方法二:通过shell脚本配置。 sh iptables.sh
方法三:通过iptables命令配置。

# setup
# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0

state NEW tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0

state NEW tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0

state NEW tcp dpt:21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0

state NEW tcp dpt:2049
REJECT all -- 0.0.0.0/0 0.0.0.0/0

reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
REJECT all -- 0.0.0.0/0 0.0.0.0/0

reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
#

关闭iptables防火墙:
# service iptables stop #关闭iptables防火墙
iptables:清除防火墙规则: [确

定]
iptables:将链设置为政策 ACCEPT:filter nat [确

定]
iptables:正在卸载模块: [确

定]
#
# chkconfig iptables off #设置防火墙开机不自动运




NAS:网络附加存储;传统的一个文件服务器
支持nfs,cifs,iscsi协议
支持ftp,http,rsync协议

SAN:FC-SAN,FC协议。

nfs协议的配置,nfs服务器的配置。
# rpm -qa |grep nfs
nfs-utils-1.2.3-26.el6.x86_64
nfs4-acl-tools-0.3.3-6.el6.x86_64
nfs-utils-lib-1.1.5-4.el6.x86_64

nfs服务器的配置文件。
# vim /etc/exports
共享目录 允许访问此共享目录的主机或网段(选项) 主机(选项)
eg:(以空格做为分隔符)
/tmp 192.168.10.100(rw,sync) 192.168.10.0/24(ro)

选项:
ro 只读
rw 读写
sync 同步
async 异步
root_squash 将远程客户端的root用户降级为匿名用户
no_root_squash 不将远程客户端root用户降级
all_squash 将远程客户端的普通用户降级为匿名用户
no_all_squash 不将远程客户端普通用户降级。


实例:将/var/ftp目录共享,允许172.16.4.0/24网段以只读的方式访问,允许172.16.4.48主机可读写,root权限不降级。


# cat /etc/exports
/var/ftp 172.16.4.0/24(ro) 172.16.4.48(rw,sync,no_root_squash)
# service rpcbind restart
停止 rpcbind: [确定]
正在启动 rpcbind: [确定]
# service nfs restart
关闭 NFS 守护进程: [确定]
关闭 NFS mountd: [确定]
关闭 NFS quotas: [确定]

关掉 NFS 配额: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
# cat /etc/exports

# showmount -e 172.16.4.49
Export list for 172.16.4.49:
/var/ftp 172.16.4.48,172.16.4.0/24

在172.16.4.43主机上操作。
# mount 172.16.4.49:/var/ftp /data
# cd /data/
# ls
10.27.txt 10.29.txt pub webmin-1.680.tar.gz
10.28.txt proftpd-1.3.5.tar.gz putty.exe winscp554setup.exe
# cp *.txt /root/
# ls /root/
10.27.txt foremann puppett
10.28.txt foreman-new
rubygem-multi_json-1.7.7-2.fc20.noarch.rpm
10.29.txt install.log rubygem-rabl-0.8.0-2.fc20.noarch.rpm
anaconda-ks.cfg install.log.syslog ruby-libs-2.0.0.247-15.fc20.x86_64.rpm
# cp /root/install.log* .
cp: cannot create regular file `./install.log': Read-only file system
cp: cannot create regular file `./install.log.syslog': Read-only file system


在172.16.4.48主机上操作。
# mount 172.16.4.49:/var/ftp /data
# cd /data
# ls
10.27.txt 10.29.txt proftpd-1.3.5.tar.gz putty.exe webmin-1.680.tar.gz
10.28.txt 777 pub test winscp554setup.exe
# cd test/
# ls
# touch aa
#


nfs服务器
# cat /etc/exports
/var/ftp 172.16.4.0/24(ro) 172.16.4.48(rw,sync,no_root_squash)
/tmp *(rw)
# service rpcbind restart
# service nfs restart

nfs客户端:
# mount /data
# mount 172.16.4.49:/tmp /data
# df -Ph


cifs服务器:
1、windows和Linux之间的共享。
2、linux和linux之间的共享。

安装cifs服务器(samba服务器)
# yum install samba.x86_64 -y
# rpm -qa |grep samba
samba-common-3.5.10-125.el6.x86_64
samba-client-3.5.10-125.el6.x86_64
samba-3.5.10-125.el6.x86_64
samba-winbind-clients-3.5.10-125.el6.x86_64

samba服务器的配置文件
/etc/samba/smb.conf

配置匿名共享。
# vim /etc/samba/smb.conf

101 security = share (将原来的user更改为share)

[public]
comment = Public Stuff
path = /var/ftp
public = yes
writable = yes


用户级的共享。
/data 允许work1组和abc用户访问,work1组中的用户只读,abc可读写。

# mkdir /data
# chmod 777 /data
# ls -ld /data
drwxrwxrwx 2 root root 4096 10月 29 17:05 /data
# groupadd work1

# useradd user1
# useradd -s /sbin/nologin user2

# cat /etc/group|grep work1
work1:x:0:user1,user2
# useradd -s /sbin/nologin abc
# passwd abc
更改用户 abc 的密码 。
新的 密码:
无效的密码: 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
# usermod -s /sbin/nologin user1
# passwd user1
更改用户 user1 的密码 。
新的 密码:
无效的密码: 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
#
# vim /etc/samba/smb.conf
102 passdb backend = smbpasswd
103 smb passwd file =/etc/samba/smbpasswd
104
297 [doc]
298 path = /data
299 public = no
300 write list = abc
301 read list = +work1
302 valid users = +work1,abc

# service smb restart
# smbpasswd -a user1
# smbpasswd -a user2
# smbpasswd -a abc

在windows上测试。


postfix邮件服务器:(实现25端口,smtp协议) pop3 110

安装postfix邮件服务器
# rpm -qa |grep postfix
postfix-2.6.6-2.2.el6_1.x86_64
# cd /etc/postfix/
# ls
access generic main.cf relocated virtual
canonical header_checks master.cf transport
#
# vim /etc/hosts
172.16.4.49 mail.abc.com abc.com

# vim /etc/postfix/main.cf
75 myhostname = mail.abc.com
83 mydomain = abc.com
99 myorigin = $mydomain
113 inet_interfaces = all (打开all)
116 #inet_interfaces = localhost (关闭localhost)
164 mydestination = $mydomain, $myhostname, localhost.$mydomain, localhost
(添加$mydomain)
264 mynetworks = 172.16.4.0/24, 127.0.0.0/8

# service postfix restart
# netstat -atnup|grep :25


# mail user1@abc.com
Subject: test mail
this is test mail..
.
EOT
# cd /var/spool/mail/
# ls
abc admin jake root rpc test user user1 user2
# ll
总用量 12
-rw-rw---- 1 abc mail 0 10月 29 17:06 abc
-rw-rw----. 1 root mail 0 10月 27 22:12 admin
-rw-rw---- 1 jake mail 0 10月 28 22:09 jake
-rw-------. 1 root mail 6215 10月 29 16:01 root
-rw-rw----. 1 rpc mail 0 10月 27 18:17 rpc
-rw-rw----. 1 test mail 0 10月 27 22:52 test
-rw-rw----. 1 user mail 0 10月 27 22:05 user
-rw-rw----. 1 user1 mail 543 10月 29 17:52 user1
-rw-rw----. 1 user2 mail 0 10月 27 22:37 user2
# cat user1
From root@abc.com Wed Oct 29 17:52:43 2014
Return-Path: <root@abc.com>
X-Original-To: user1@abc.com
Delivered-To: user1@abc.com
Received: by mail.abc.com (Postfix, from userid 0)
id 89286C2C1F; Wed, 29 Oct 2014 17:52:43 +0800 (CST)
Date: Wed, 29 Oct 2014 17:52:43 +0800
To: user1@abc.com
Subject: test mail
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20141029095243.89286C2C1F@mail.abc.com>
From: root@abc.com (root)

this is test mail..


pop3服务器的安装和配置。
# yum install dovecot -y
# /etc/init.d/dovecot restart
停止 Dovecot Imap: [失败]
正在启动 Dovecot Imap: [确定]
# netstat -atnup|grep -i dov
tcp 0 0 0.0.0.0:110 0.0.0.0:*
LISTEN 9110/dovecot
tcp 0 0 0.0.0.0:143 0.0.0.0:*
LISTEN 9110/dovecot
tcp 0 0 0.0.0.0:993 0.0.0.0:*
LISTEN 9110/dovecot
tcp 0 0 0.0.0.0:995 0.0.0.0:*
LISTEN 9110/dovecot
tcp 0 0 :::110 :::*
LISTEN 9110/dovecot
tcp 0 0 :::143 :::*
LISTEN 9110/dovecot
tcp 0 0 :::993 :::*
LISTEN 9110/dovecot
tcp 0 0 :::995 :::*
LISTEN 9110/dovecot
#


检查综合性能:
top -->z--b--k--r--q

cpu性能:
sar 1 1 (cpu,diskio)
mpstat -P ALL (查看CPU各核的信息)。
cat /proc/cpuinfo
vmstat 1 1

内存性能:
top
vmstat
free -m
cat /proc/meminfo (真正使用的内存=cache+buffer)

io性能:
sar -d 1 100 每隔1秒检查一次,共检查100次
iostat 1
vmstat 1

进程:
ps -aux
ps -ef
pstree
kill [-9] <pid>
killall <进程名称>

开机时间:
uptime
cat /proc/uptime

操作系统的版本
# lsb_release -a (推荐)
LSB Version:
:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 6.3 (Santiago)
Release: 6.3
Codename: Santiago
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.3 (Santiago)
# cat /etc/issue
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel \r on an \m

# cat /etc/issue.net
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel \r on an \m
#


密码不正确或丢失:
grub密码 root密码 处理办法
nok ok 进入/boot/grub/grub.conf文件,直接修改。
ok nok 通过单用户登录,修改密码。
nok nok
通过光盘,进入救援模式,删除grub.conf文件中的password行,再通过单用户模式登录更改root密码。

通过光盘进入到救援模式,一路回车--进入bash---chroot /mnt/sysimage---cd
/boot/grub---vim grub.conf(删除password行),保存退出,重启系统。
系统进入grub菜单,按e编辑--选择第二行(带kernel的行),输入空格1|single|S--回车---按b引导---passwd
root更改root密码---exit重启引导系统。


MBR故障处理:
1、模拟故障
# dd if=/dev/zero of=/dev/sda bs=46 count=1
记录了1+0 的读入
记录了1+0 的写出
46字节(46 B)已复制,0.000196035 秒,235 kB/秒
# reboot

2、解决故障;
通过光盘进入到救援模式,一路回车--进入bash---chroot
/mnt/sysimage--grub-install /dev/sda--重启。


文件被误删除。
1、通过rlinux工具。找回误删的信息。
2、ext4undelete命令,恢复误删的信息。


# chattr +i install.sh
# lsattr install.sh
----i--------e- install.sh
# rm install.sh
rm:是否删除普通文件 "install.sh"?y
rm: 无法删除"install.sh": 不允许的操作
# whoami
root
# chattr -i install.sh

# lsattr install.sh
-------------e- install.sh
# rm install.sh
rm:是否删除普通文件 "install.sh"?y


# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
# cat 1.sh
#!/bin/bash
date
# chmod +x 1.sh
# ll 1.sh
-rwxr-xr-x 1 root root 17 10月 29 21:26 1.sh
# sh 1.sh
2014年 10月 29日 星期三 21:26:57 CST
# bash 1.sh
2014年 10月 29日 星期三 21:27:04 CST
# source 1.sh
2014年 10月 29日 星期三 21:27:07 CST
# ./1.sh
2014年 10月 29日 星期三 21:27:12 CST

# . /root/1.sh ###直接在此终端执行,不产生中断。
2014年 10月 29日 星期三 21:27:21 CST


输入输出重定向
> 正确输出重定向
2> 错误输出重定向
2>&1 正确和错误重定向
# ls > 1.txt
# cat 1.txt
1.sh
1.txt
anaconda-ks.cfg
Desktop
install.log.bak
install.log.syslog
sar.txt
# ls 2> 1.txt
1.sh 1.txt anaconda-ks.cfg Desktop install.log.bak install.log.syslog
sar.txt
# cat 1.txt
# ls > 1.txt 2>&1 > /dev/null
# cat 1.txt
# ls > 1.txt 2>&1
# cat 1.txt
1.sh
1.txt
anaconda-ks.cfg
Desktop
install.log.bak
install.log.syslog
sar.txt
# lsadsfadsf > 1.txt 2>&1
# cat 1.txt
-bash: lsadsfadsf: command not found

管道: | 前一个命令的输出作为后一个命令的输入。
ls -l |grep ^- 只显示文件。

前后台
# pwd;date;time
/root
2014年 10月 29日 星期三 21:35:43 CST

real 0m0.000s
user 0m0.000s
sys 0m0.000s
# ls &
[1] 3683
# 1.sh 1.txt anaconda-ks.cfg Desktop install.log.bak
install.log.syslog sar.txt

[1]+ Done ls --color=auto
# fg
-bash: fg: current: no such job
# vim &
[1] 3684
# fg
vim

[1]+ Stopped vim
# jobs
[1]+ Stopped vim
#


shell的变量
1、环境变量,set,env,export
2、预定义变量,用户不能直接修改。 $0,$1,$2..$$,$?,$#
3、自定义变量(局部和全局)
# var=hello
# echo $var
hello
# bash
# echo $var

# exit
exit
# export var1=hello
# echo $var1
hello
# bash
# echo $var1
hello

正则表达式:
是一个表达式,由一个或多个元字符组成的表达式。


元字符:
不能再分隔的字符。

元字符:
* 匹配0个及0个以上。 ab*(a,a1,a2,ab,abc,abccd)
? 匹配0个或1个。 ab?(a,ab,abb,abc)
+ 匹配1个或1个以上。 ab+(ab,abc,abb)
. 匹配任意一个字符 a.(a1,aa,az,a#,a@)

[0-9] 匹配任意一个数字 1,2,3,4,5,6,7,8,9,0
[^0-9] 匹配非数字的任意一个字符 a,A,#,*,Z...
^[0-9] 匹配任意一个数字开头的 1a,0b,9c
[0-9a-zA-Z] 匹配任意一个数字,小写字母,大写字母
[abc] 匹配a或b或c


^a 匹配a开头
a$ 匹配a结尾

*==={0,}
?==={0,1}
+==={1,}

a\{2,9\} 匹配2个~9个a字符。


流程控制语句:
条件
if 条件;then
xxx
else
xxx
fi


# vim if.sh
#!/bin/bash
echo "pls input yes or no"
read yn
if [ "$yn" = "yes" ];then
echo "input yes"
else
echo "input no"
fi


# sh if.sh
pls input yes or no
yes
input yes
# sh if.sh
pls input yes or no
no
input no


case语句:
case "变量" in
值1)
xxx
;;
值2)
xxx
;;
*)
xxx
;;
esac


# vim case.sh
#!/bin/bash
echo "pls input yes or no"
read yn
case "$yn" in
[yY][eE][sS]|[yY])
echo "input yes"
;;
[nN][oO]|[Nn])
echo "input no"
;;
*)
echo "pls input yes or no"
;;
esac

# chmod +x case.sh
# sh case.sh
pls input yes or no
Yes
input yes
# sh case.sh
pls input yes or no
N
input no
# sh case.sh
pls input yes or no
Yees
pls input yes or no


for语句:
方法一:
for((i=0;i<=100;i++))
do
xxxx
done

方法二:
for 变量 in 值1 值2 ...
do
xxxx
done

# vim for.sh
# cat for.sh
#!/bin/bash
sum=0
for ((i=0;i<=100;i++))
do
let sum=sum+i
done
echo $sum

sum1=0
for i in `seq 1 100`
do
let sum1=sum1+i
done
echo $sum1
# chmod +x for.sh

# sh for.sh
5050
5050
#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值