Linux基本命令 文末附赠一份word版本链接
Ctrl+u
删除整行命令
;一行输入多条命令时,命令间用其格开。
\
一个命令占多行时,用其继行
>
为命令的输出信息重定向
<
为命令的输入信息重定向
|
命令管道
&
后台运行命令
alt+F1alt+F2alt+F3alt+F4alt+F5 alt+F6
虚终端切换
manman
命令名查看命令的使用说明.
*
匹配任意顺序的一个或多个字符Jo*y
?
匹配任意单个字符?.h
[ ]
匹配一组封闭字符或范围:doc[a-e].txt
注意:在UNIX里是大小写敏感
ls
查看文件和目录,参数:-l -a-R
pwd
显示当前工作的绝对路径
cd
目录导航
file
确定文件类型
stat
查看文件或目录的访问统计
cp
拷贝文件参数:-r -f
mv
移动或更名目录和文件
rm
删除文件,参数:-i –f -r
touch
创建空文件或更新已存在的文件或目录的时间戳
cat
显示文件内容,合并文件,追加文件
catfile
catfile1 file 2> newfile
cat file1 file2>>file3
more
分屏显示文件内容, 回车键:一行一行往下翻;空格键:一屏一屏往下翻;q:退出
ln
链接文件(可理解为同样的文件有两个文件名) ln –s [source] [target]; 软链接
mkdir
创建目录mkdir dir1 dir2 dir3
rmdir
删除空目录
rm –r
删除目录及其文件
head
显示一个文件头几行.缺省10行.head –50 file |more
tail
显示一个文件尾几行.缺省10行.
sort
排序sort a.txtsort-o sorted.txt a.txt
sort -o sorted.txt a.txt b.txt c.txt
-r
:倒序排列
wc
可计算文件的行数,字符数和单词数,参数: -l-c -w
grepgrep [-viw] pattern file(s)
在屏幕上显示包含pattern的每一行.
-v
显示模式的反向内容
-i
忽略大小写的区别
-w
要求规定模式必须是一个完整的单词
which
确定一个命令(程序)的具体路径
locate
显示包括输入字符的所有文件和目录
find
查找文件
find [path] [-type fdl] [-name pattern] [-atime [+ -] name of days] [-exec command { } \ ;] 例如:
find . –type d
find. –type f –name mess*
find . –type f –name mess* -exec grep –l ‘abc’ {} \;
find /home –type f -empty
whatis
whatis keyword 显示对keyword的说明
pico
全屏文本编辑器 pico filename
du
显示目录使用情况du |sort –r -n
compresscompress
[-v] file(s)压缩成 .Z 扩展文件
-v
显示被压缩的文件的减少比例uncompressuncompress [-v]
unzip
解压缩带.zip文件
gunzip
gunzip [-v] file(s)解压缩.gz.z.Z.tgz
gzipgzip
[-rv]可得到最佳的压缩率和价值,但速度十分慢得.gz
tartar
[c] [x] [v] [z] [f filename] file or dir
tar cvf source.tar *.c
tar xvf backup.tar
tar xzf backup.tgz或tar xzf backup.tar.gz
rpmRedHeadPackage Manager
rpm –i precious-software-1.0.i386.rmp安装软件包
rpm –ivh详细输出安装信息
rpm-v升级
rpm-qa列出系统中安装得全部有关的包
rpm-q sendmail查sendmail属于哪个包
rmp –e sendmail卸载sendmail软件包
编辑状态:命令态[一般命令,行命令]、输入态 状态转换:命令态---->输入态--(ESC)-->命令态
h
, backspace左移一个字符
j
, return下移一行
k
上移一行
l
, spaceBar右移一个字符
H
移至屏幕顶端
L
移至屏幕底端
Ctrl+F
向前移动一页
Ctrl+B
向后移动一页
a
在当前位置右边插入
A
在当前行尾插入
i
在当前位置插入
I
在当前行首插入
o
在下一行插入
O
在上一行插入
x
删除当前位置字符
X
删除当前位置左边的字符
dd
删除当前行
yy
拷贝当前行
p
粘贴到下一行
P
粘贴到上一行
G
移到最后一行
1G
移到第一行
xG
移到第x行
/{regular expression}
查找字串
? {regular expression}
反向查找字串
n
查找下一个
N
查找前一个
:g
替换字串:g/findstring//s/replacestring/g
ZZ
存盘并退出vi
:w
存盘:w {filename}
:q
退出
:r
插入文件:r {filename}
view
只读打开.$view {filename}
例: 用vi字处理程序编写文件test.txt
vi test.txt
=== 键 I 插入文字
Hi, my name is abc.
how are you?
bye!
=== ESC 键 :wq 存盘退出
查看内容: more test.txt
继续修改 文件test.txt
dd
dd if=input file [conv=conversion type] of=outpu file [obs=output block size]
其中conv=lcase or ucase 例如:
dd if=/tmp/uppercase.txt conv=lcase of=/tmp/lowercase.txt
dd if=/some/boot.img conv=lcase of=/dev/fd0 obs=16k
df
显示磁盘容量dk –k
fdformat
在软盘设备上进行低级格式化
mkfs
创建文件系统
mkfs [-t fstype] [-cv] device-or-mount-point [blocks]
-c
在创建文件系统之前,需要mkfs检查坏道
-v
产生详细输出
mkfs –t ext2 /dev/hda3
edquota
quota
quotaon
quotaoff
swapoff –a
swapon
umount –a [-t fstype]
fdisk
mount安装文件系统
mount –a [-t fstype] [-o options] device directory
-a 全部
options: ro(只读)rw(读写)
mount –t ext2 –o ro /dev/hda6 /sacured
mount –a –t ext2
bash$ more /etc/fstab
/dev/hda3 / ext2 defaults 1 1
/dev/hda1 /dos vfat defaults 0 0
/mnt/cdrom /mnt/cdrom supermount fs=iso9660,dev=/dev/cdrom 0 0
/mnt/floppy /mnt/floppy supermount fs=vfat,dev=/dev/fd0 0 0
none /proc proc defaults 0 0
none /dev/pts devpts gid=5,mode=620 0 0
/dev/hda5 swap swap defaults 0 0
文件系统类型:
ext2 ------ linux文件系统
ext3 ------ linux文件系统
swap ------ linux交换区文件系统
vfat ------ linux文件系统
iso9600 ------ 光盘文件系统
安装卸载文件系统
mount [-t 文件系统类型] 设备文件 安装点
umount 安装点
例:使用光盘:
mount /dev/cdrom /mnt/cdrom
停止使用光盘:
umount /mnt/cdrom
例:使用软盘:
mount /dev/fd0 /mnt/floppy
停止使用软盘:
umount /mnt/floppy
文件权限管理
缺省文件权限的设定:
umask 022
666 - 022 = 644 (对应位相减)
读权限 = 4 写权限 = 2 执行权 = 1
644 - (读写执行 读执行 执行)
umask 026
666 - 026 = 640
对目录的权限设定
777 - 026 = 751 (读写执行 读执行 执行)
chown 用户[:用户组] 文件名
chmod 权限 文件
磁盘空间查看
df [-k]
du [-sk]
检查文件系统
fsck [-f -y] [设备文件]
磁盘分区管理 fdisk /dev/hda
dmesg
打印引导时内核显示的状态信息
free
显示存储器使用统计信息
shutdown
关闭系统,重新引导或停机
shutdown [-r][-h][-c][-k][-t seconds] time [messages]
-f
强制执行
-r
重启
-h
停机
-c
取消shutdown操作
shutdown –r nowshutdown –r 12:55shutdown –r +5
立即重启12点55分重启5分钟后重启
shutdown –h now
uname
显示系统信息
uname [-m][-v][-n][-r][-s][-a]
uptime
显示系统已经运行多长时间
su
改变用户
date
查看或修改时间
date [MMDDhhmm[[CC]YY][.ss]]
cal
查看日立
ps
进程管理
例如:
[root@linux /root]# ps -ef|grep ls
root 1399 1280 0 14:41 tty2 00:00:00 grep ls
[root@linux /boot]# ps
PID TTY TIME CMD
1278 tty2 00:00:00 login
1280 tty2 00:00:00 bash
1345 tty2 00:00:00 ps
[root@linux /boot]# ps -ef|grep sendmail
UID PID PPID C STIME TTY TIME CMD
root 811 1 0 09:48 ? 00:00:00 sendmail: accepting connections
root 1342 1280 0 14:26 tty2 00:00:00 grep sendmail
[root@linux /boot]# ps aux|grep sendmail
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 811 0.0 0.8 2128 1124 ? S 09:48 0:00 sendmail: acceptiroot 1344 0.0 0.4 2232 516 tty2 S 14:26 0:00 grep sendmail
kill -HUP 811
kill -9 811
(1)
用户名一般为2~8个字符,且第一个必须为字符
UID0~499为系统用
组用户必有主组,并可加入最多16个辅组
/etc/default/useradd创建默认的用户设置
/etc/skel典型的点配置文件拷贝到家目录
(2)
用户管理数据库
/etc/passwd存放用户信息
/etc/group 存放组信息
/etc/shadow存放密码信息
[root@linux /boot]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:
adm:x:3:4:adm:/var/adm:
gopher:x:13:30:gopher:/usr/lib/gopher-data:
ftp:x:14:50:FTP User:/home/ftp:
nobody:x:99:99:Nobody:/:
zrm:x:500:0:Zou Renming:/home/zrm:/bin/bash
用户管理数据库格式为:
每行为一个用户的记录, 以":"来区分不同列
列1为: 用户名
列2为: 密码
列3为: 用户ID
列4为: 用户组ID
列5为: 用户信息
列6为: 用户根目录
列7为: 用户SHELL
注意:对/etc/passwd文件:
(1) 超级用户的uid:gid=0:0
(2) 密码用x代替,加密后的口令字存在/etc/shadow文件中
若修改其中某一列值(如:用户SHELL),出现什么现象?
zrm:x:500:0:Zou Renming:/home/zrm
[root@linux /boot]# more /etc/group
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
用户组管理数据库格式为:
每行为一个用户组的记录, 以":"来区分不同列
列1为: 用户组名
列2为: 密码
列3为: 用户组ID
列4为: 用户组成员列表
[root@linux /boot]# more /etc/shadow
root:$1$K2pg8GSO$WNqt8YRFJOWwPFxum5SA//:11434:0:99999:7:-1:-1:134540356
bin:*:11423:0:99999:7:::
ftp:*:11423:0:99999:7:::
nobody:*:11423:0:99999:7:::
zrm:$1$EBqAq.jp$uvxYBdXWyomQYSGR7kL4H.:11423:0:99999:7:-1:-1:134540356
比较文件/etc/passwd和/etc/shadow的存取权限。
(3)
添加删除用户
adduser [-g group -p "password"] name//adduser=useradd
userdel [-r] name
例如:
adduser user123//所创建的uid=pid
adduser -g nobody -p "" user124
useradd newuser -d /www/newuser//只创建最终的目录而不是
全部的路径
userdel user123
userdel -r user123//把用户的家目录一起删除
tail /etc/passwd
ls -l /home
(4)
添加删除用户组
groupadd [-g gid [-o]] [-r] [-f] group
groupdel group
例如:
groupadd stu
groupadd –g gid
groupdel stu
从文件添加一组用户
建文件users,加用户记录,格式如/etc/passwd
auser1:test::503::/home/auser1:/bin/bash
auser2:test::503::/home/auser2:/bin/bash
执行:# newusers users
文件users 内容为:
admin:password::0::/home/admin:/bin/bash
usera_01:password::100::/home/usera_01:/bin/bash
(5) chsh laofd改变shell
(6) 改home目录useradd –d new-home-dir username
把原home内容移到新目录useradd –d –m new-home-dir username
(7) 修改用户的uidusermod –u uid username
此时在用户家目录里用户所拥有的所有文件和目录将自动反映uid的变化,而在家目录之外则必须手工修改.
(8) 修改帐户截至日期usermod –e MM/DD/YY username
(9) 修改存在的组groupmod –n newgroup currentgroup
(10) 设置使用用户磁盘配额----------不讲
lilo引导启动头
LINUX启动顺序:启动LINUX内核->安装文件系统->按启动方式启动服务进程
Linux启动时,运行一个叫做init的程序,然后由它来启动后面的任务,包括多用户环境、网络等。
/etc/inittab文件是init程序寻找的主要文件。/etc/inittab文件里指定运行级别。那么,到底什么是运行级呢?简单的说,运行级就是操作系统当前正在运行的功能级别。这个级别从1到6 ,具有不同的功能。最先运行的服务是那些放在/etc/rc.d 目录下的文件。
大多数的Linux发行版本中,启动脚本位于/etc/rc.d/init.d。这些脚本被ln命令来连接到 /etc/rc.d/rcn.d目录。(这里的n 就是运行级0-6) 例如/etc/rc.d/rc2.d下面的S10network就是连接到/etc/rc.d/init.d下的network脚本的。
目录中script文件的命名规则为:[S|K][0|9][0|0][name]
手工控制启动方式:init 数字
修改lilo引导启动头
lilo引导启动头
lilo:dos
lilo:linux (或空回车)
[root@linux /boot]# more /etc/lilo.conf
boot=/dev/hda3
map=/boot/map
install=/boot/boot.b
message=/boot/cosixmsg
prompt
timeout=50
default=linux
image=/boot/vmlinuz-2.2.16-3
label=linux
vga=0x301
read-only
root=/dev/hda3
other=/dev/hda1
label=dos
LINUX启动顺序:启动LINUX内核->安装文件系统
->执行init程序:按启动方式启动服务进程
/boot/vmlinuz
启动方式:单用户、多用户
不同的运行级定义如下:(可以参考Red Hat Linux里面的/etc/inittab)
# 缺省的运行级,RHS用到的级别如下:
# 0 - 停机(千万不要把initdefault设置为0 )
# 1 - 单用户模式
# 2 - 多用户,但是没有NFS
# 3 - 完全多用户模式
# 4 - 没有用到
# 5 - X11
# 6 - 重新启动(千万不要把initdefault设置为6 )
# 对各个运行级的详细解释:
0 为停机,机器关闭。
1 为单用户模式,就像Win9x下的安全模式类似。
2 为多用户模式,但是没有NFS支持。
3 为完整的多用户模式,是标准的运行级。
4 一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。
5 就是X11,进到X Window系统了。
6 为重启,运行init 6机器就会重启。
more /etc/inittab
# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 – unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
# Things to run in every runlevel.
ud::once:/sbin/update
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
# When our UPS tells us power has failed, assume we have a few minutes
# of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5
# xdm is now a separate service
x:5:respawn:/etc/X11/prefdm -nodaemon
/etc/rc.d
drwxr-xr-x 2 root root 4096 04 12 00:11 init.d
-rwxr-xr-x 1 root root 2889 11 9 1999 rc
-rwxr-xr-x 1 root root 1569 05 7 2000 rc.local
-r-xr-x--- 1 news news 2964 07 25 2000 rc.news
-rwxr-xr-x 1 root root 14394 08 2 2000 rc.sysinit
drwxr-xr-x 2 root root 4096 04 12 00:11 rc0.d
drwxr-xr-x 2 root root 4096 04 12 00:11 rc1.d
drwxr-xr-x 2 root root 4096 04 12 00:11 rc2.d
drwxr-xr-x 2 root root 4096 04 12 00:11 rc3.d
drwxr-xr-x 2 root root 4096 04 12 00:11 rc4.d
drwxr-xr-x 2 root root 4096 04 12 00:11 rc5.d
drwxr-xr-x 2 root root 4096 04 12 00:11 rc6.d
[root@linux /boot]# ls /etc/rc.d/init.d
anacron functions inet killall network rstatd smb ypserv
apmd gpm innd kudzu nfs rusersd snmpd
arpwatch halt ipchains linuxconf nfslock rwalld syslog
atd httpd irda lpd pcmcia rwhod xfs
crond hzinput kdcrotate named portmap sendmail ypbind
dhcpd identd keytable netfs random single yppasswdd
使用rc.local文件
最后运行的SHELL程序
查看: more /etc/rc.d/init.d/rc.local
手工控制启动方式:init 数字
0 - halt (Do NOT set initdefault to this)
1 - Single user mode
2 - Multiuser, without NFS (The same as 3, if you do not have networking)
3 - Full multiuser mode
6 - reboot (Do NOT set initdefault to this)
自定义服务启动方法
自编SHELL程序,放入相应引导目录, 或
放入rc.local程序中, 如: 加入
mail -s "Boot now" < /dev/null
修改lilo引导启动头
修改/etc/lilo.conf
lilo - 重新安装引导头
1、 at
at 时间20:408:40 p.m.8:40 p.m. feb23 10 a.m.+5 days
12:30 p.m. tomorrowmidnightnoon
atq
atrm 工作号
如果想限制at工具的使用,可以创建一个/etc/at.allow文件
如果只想拒绝一小部分用户,可以创建/etc/at.deny文件
例如:
[root@dhcplinux root]# at now
warning: commands will be executed using (in order) a) $SHELL b) login shell c)
/bin/sh
at> ls
at> <EOT>
job 4 at 2003-09-25 15:25
[root@dhcplinux root]# atq
[root@dhcplinux root]# at 20:40
warning: commands will be executed using (in order) a) $SHELL b) login shell c)
/bin/sh
at> du -a > /tmp/du.out
at> <EOT>
job 5 at 2003-09-25 20:40
[root@dhcplinux root]# atq
5 2003-09-25 20:40 a root
[root@dhcplinux root]# atrm 5
[root@dhcplinux root]# atq
[root@dhcplinux root]#
2 cron
crontab [-u 用户名] –e//编辑作业文件
分 小时 天 月 星期几 <命令>//格式
0,30 * * * * ls /root/out.a//可以定义逗号分割的列表
30 1 * * * tar cvf /dev/fd0 /root/*
crontab [-u 用户名] –l//显示某个用户的作业
crontab [-u 用户名] –r//删除作业
/var/spool/cron//任务规划文件放置位置
例:
[laofd@dhcplinux laofd]$ vi cron//创建任务文件
[laofd@dhcplinux laofd]$ ls
aa ExtremeWare622QuickRef.pdf ExtremeWareUser622.zip test
cron ExtremeWareCLIRef622.zip lao
[laofd@dhcplinux laofd]$ more cron
30 2 * * * ls > ls.out
[laofd@dhcplinux laofd]$
[laofd@dhcplinux laofd]$ crontab cron//安装laofd的cron作业
[laofd@dhcplinux laofd]$ crontab –l//查看laofd的cron作业
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (cron installed on Thu Sep 25 15:55:28 2003)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
30 2 * * * ls > ls.out
[laofd@dhcplinux laofd]$ su -//转到root用户
Password:
[root@dhcplinux root]# crontab –l查看root的cron作业
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (root installed on Thu Sep 25 15:49:41 2003)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.20015 installed on Fri Aug 2 17:04:12 2002)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/mrtg /mrtg/mrtg3600.cfg
5/5 * * * * /usr/bin/mrtg /mrtg/mrtg2511.cfg
[root@dhcplinux root]# crontab -u laofd –l//在root 下查看laofd的cron作业
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (cron installed on Thu Sep 25 15:55:28 2003)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
30 2 * * * ls > ls.out
[root@dhcplinux root]# cd /var/spool
[root@dhcplinux spool]# ls
anacron cron lpd mqueue rwho squid vbox
at fax mail news samba up2date
[root@dhcplinux spool]# cd cron
[root@dhcplinux cron]# ls//在此目录下会自动生成laofd文件
laofd root root.lao
[root@dhcplinux cron]# ls -l
total 12
-rw------- 1 root laofd 201 Sep 25 15:55 laofd
-rw------- 1 root root 490 Sep 25 15:49 root
[root@dhcplinux cron]# crontab -u laofd –r//删除laofd的cron作业
[root@dhcplinux cron]# ls
root
(1). 日志有两种类型:
1. 服务器程序写定制的日志文件,如 Sendmail,Apache,named
2. 使用syslog的实用程序,这个日志工具是由syslogd守护进程提供的
(2). 日志存放目录:/var/log
[root@linux log]# ls -l /var/log
%ld 总用量 632
-rw-r--r-- 1 root root 73863 05 4 09:48 boot.log
-rw------- 1 root root 5125 05 4 15:20 cron
-rw-r--r-- 1 root root 3951 05 4 09:48 dmesg
-rw-r--r-- 1 root root 0 04 12 00:00 htmlaccess.log
drwxr-xr-x 2 root root 4096 04 22 18:06 httpd
-rw-r--r-- 1 root root 148044 05 4 14:53 lastlog
-rw------- 1 root root 5545 05 4 15:03 maillog
-rw------- 1 root root 297779 05 4 15:20 messages
-rw-r--r-- 1 root root 6358 04 26 13:42 netconf.log
drwxrwxr-x 3 news news 4096 04 11 23:58 news
drwx------ 2 root root 4096 07 25 2000 samba
-rw------- 1 root root 2098 05 4 14:53 secure
-rw-r--r-- 1 root root 616 05 4 15:03 sendmail.st
-rw------- 1 root root 0 04 11 23:56 spooler
drwxr-xr-x 2 uucp uucp 4096 04 12 00:01 uucp
-rw-rw-r-- 1 root utmp 177792 05 4 14:53 wtmp
-rw------- 1 root root 0 04 12 00:01 xferlog
(3). Syslogd的配置文件: /etc/syslog.conf
syslog配置行的结构如下:
facility. priority destination
facility可以是以下关键字的某个或*:
auth、Authpriv、cron、daemon、kern、lpr、mail、news、syslog、
user、uucp、local0~local7
priority可以是以下关键字的某个或*:
debug、info、notice、warning、err、crit、alert、emerg、none
=========================================================
[root@linux log]# more /etc/syslog.conf
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;news.none;authpriv.none /var/log/messages
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* /var/log/maillog
# Everybody gets emergency messages, plus log them on another
# machine.
*.emerg *
# Save mail and news errors of level err and higher in a
# special file.
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
......
====================
rm /var/log/messages
cp /dev/null /var/log/messages
kill -HUP `cat /var/run/syslog.pid`
0 1 * * sun /usr/local/bin/newlogs
(4) 使用tail监视log
tail –f /var/log/messages
12、shell(外壳) 用户登录环境,命令解释环境
[zrm@linux /etc]$ more /etc/shells
/bin/bash
/bin/sh
/bin/ash
/bin/bsh
/bin/tcsh
/bin/csh
set -- 查看当前用户的环境变量
AA=aaaa
echo $AA
export AA
[zrm@linux zrm]$ AA=abc
[zrm@linux zrm]$ echo $AA
abc
[zrm@linux zrm]$ sh
bash$ echo $AA
bash$ exit
[zrm@linux zrm]$ AA=abc;export AA
[zrm@linux zrm]$ sh
bash$ echo $AA
abc
.bash_profile (用户根目录下) bash用户执行命令的记录文件
注意:与history比较
PATH=$PATH:/bin:/usr/bin:/usr/sbin:.
TERM=ansi
export PATH TERM
/etc/profile (定义所有用户的环境变量)
PATH
umask 022
重定向
标准输入(0) 标准输出(1) 标准错误输出(2)
标准输入重定向 <
mail zrm < abc.txt
标准输入重定向 > >>
cat >abc.txt
ls >> abc.txt (覆盖)
more abc.txt
标准错误重定向 2>
cmd 2> err.txt
cat er.txt
find / -name a* -print > out.txt 2>&1
more out.txt
管道 |
ls -l |more
ls -l | wc -l
通配符
* -- 0至多个字符
? -- 1个字符
[a,b,c,1-5,e-f] -- 任配一个字符
{01,02,03} 文件或目录名扩展
如:
ls a*
ls abc?.txt
ls abc[1-5].txt
mkdir abc{01,02,03}
mkdir ab/{aa,aa/bb{01,02,03}}
[zrm@linux zrm]$ ls a*
abc.txt abc1.txt abc3.txt
[zrm@linux zrm]$ ls abc?.txt
abc1.txt abc3.txt
[zrm@linux zrm]$ ls abc[1-5].txt
abc1.txt abc3.txt
umask -- 决定生成新文件的权限
umask 022 (缺省权限设置)
touch aaa1
mkdir dir1
ls -l
umask 002
touch aaa2
mkdir dir2
ls -l
alias 命令别名
alias l="ls -la"
l
作业控制
后台作业 &
shell编程
test命令 - 测试某些条件是否成立。如果条件成立,则返回“真”,即返回“0”,否则为“假”
非0
test “hello”=”aaa”; echo $?
Test $aaa 测试变量aaa是否存在。
Test命令的另一种表示方法[ ]
[ “hello”=”world”]
[ $aaa]
test命令参数:
-f 文件是否存在 test –f abc.txt
-z 检测字符串是否为空串 test –z $name
shell控制结构
if命令
if <条件>; then <命令>;fi
if <条件>; then <命令>;else <命令>; fi
例举:/etc/rc.d/rc3.d/S80sendmail
=== S80sendmail shell程序
#!/bin/sh 调用shell
# #--说明语句
# sendmail This shell script takes care of starting and stopping
# sendmail.
#
# chkconfig: 2345 80 30
# description: Sendmail is a Mail Transport
Agent, which is the program \
# that moves mail from one machine to another.
# processname: sendmail
# config: /etc/sendmail.cf
# pidfile: /var/run/sendmail.pid
# Source function library.
. /etc/rc.d/init.d/functions *** 执行另一shell程序
# Source networking configuration.
. /etc/sysconfig/network
# Source sendmail configureation.
if [ -f /etc/sysconfig/sendmail ] ; then -- if 分枝语句
. /etc/sysconfig/sendmail
else
DAEMON=yes
QUEUE=1h
fi
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0 -- 前一条件为真,执行后一命令
[ -f /usr/sbin/sendmail ] || exit 0 -- 前一条件为假,执行后一命令
RETVAL=0
# See how we were called.
case "$1" in-- 开关分枝语句
start)
# Start daemons.
echo -n "Starting sendmail: "
/usr/bin/newaliases > /dev/null 2>&1
for i in virtusertable access domaintable mailertable ; do
if [ -f /etc/mail/$i ] ; then
makemap hash /etc/mail/$i < /etc/mail/$i
fi
done
daemon /usr/sbin/sendmail $([ "$DAEMON" = yes ] && echo -bd) \
$([ -n "$QUEUE" ] && echo -q$QUEUE)
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sendmail
;;
stop)
# Stop daemons.
echo -n "Shutting down sendmail: "
killproc sendmail
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sendmail
;;
restart|reload)
$0 stop
$0 start
RETVAL=$?
;;
status)
status sendmail
RETVAL=$?
;;
*)
echo "Usage: sendmail {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
==== end ====
练习shell编程
vi mytest.sh
=== 进入VI 键I
me=`whoami`
if [ $me = root ] ; then
echo "you are root user!"
else
echo "hi, $me, You aren't root user!"
fi
=== ESC :wq 退出
执行shell:
sh mytest.sh
换另一用户,再次执行
基本的网络配置文件:(这些配置文件可以通过配置命令来实现,也可以直接编辑实现)
(1) /etc/hosts当前主机的IP地址和主机名
格式: IP_address canonical_hostname aliases
例如:127.0.0.1 localhost.localdomain localhost
202.205.80.165 ipgw1 ipgw1.cau.edu.cn
注明: 一旦配置完机器的网络配置文件,应该重新启动网络以使修改生效。使用下面的命令来重新启动网络:/etc/rc.d/init.d/network restart
(2) /etc/sysconfig/network
文件示例:
NETWORKING=yes//表示想要允许连网支持
HOSTNAME=dhcplinux//
GATEWAY=202.205.93.129 //此处必须配网关
(3)/etc/sysconfig/network-scripts/ifcfg-ethN网卡的配置信息
(第一块网卡N=0,第二块网卡N=1)
文件示例:
a: DEVICE=eth0//网卡名
ONBOOT=yes//启动时是否激活该卡
BOOTPROTO=static//none, bootp, dhcp
IPADDR=202.205.93.131
NETMASK=255.255.255.128
NETWORK=202.205.93.128
BROADCAST=202.205.93.255
ONBOOT=yes
b: DEVICE=eth0
IPADDR=208.164.186.1
NETMASK=255.255.255.0
NETWORK=208.164.186.0
BROADCAST=208.164.186.255
ONBOOT=yes
BOOTPROTO=none
USERCTL=no //是否允许非root用户控制该设备
注明:一旦配置了(2)和(3),需重新引导网络,最简单的方法是运行:
#/etc/sysconfig/network-scripts/ifup eth0
(4) /etc/resolv.conf指定域名服务器
文件示例:
searchcau.edu.cn
nameserver 202.205.80.132
nameserver202.112.170.10
其中searchcau.edu.cn表示当提供了一个不包括完全域名的主机名时,在该主机名后加cau.edu.cn
(5) /etc/nsswitch.conf指定名字解释
文件示例:
# An example Name Service Switch config file. This file should be
# sorted with the most-used services at the beginning.
passwd: files nisplus nis
shadow: files nisplus nis
group: files nisplus nis
#hosts: db files nisplus nis dns
hosts: files nisplus nis dns//此行必须在后面加上dns
(6) /etc/host.conf指定如何解释主机名
文件示例:
order hosts,bind//指定主机名查询次序为先查/etc/hosts后查DNS
multi on
ospoof on
“order bind,hosts”指定主机名查询顺序,这里规定先使用DNS来解析域名,然后再查询“/etc/hosts”文件(也可以相反)。
“multi on”指定是否“/etc/hosts”文件中指定的主机可以有多个地址,拥有多个IP地址的主机一般称为多穴主机。
“nospoof on”指不允许对该服务器进行IP地址欺骗。IP欺骗是一种攻击系统安全的手段,通过把IP地址伪装成别的计算机,来取得其它计算机的信任。
(7) /etc/inetd.confinetd超级服务器的配置文件
inetd,也叫作“超级服务器”,就是监视一些网络请求的守护进程,其根据网络请求来调用相应的服务进程来处理连接请求。inetd.conf则是inetd的配置文件。inetd.conf文件告诉inetd监听哪些网络端口,为每个端口启动哪个服务。在任何的网络环境中使用Linux系统,第一件要做的事就是了解一下服务器到底要提供哪些服务。不需要的那些服务应该被禁止掉,最好卸载掉,这样黑客就少了一些攻击系统的机会。查看“/etc/inetd.conf”文件,了解一下inetd提供哪些服务。用加上注释的方法(在一行的开头加上#号),禁止任何不需要的服务,再给inetd进程发一个SIGHUP信号。
(8) /etc/xinetd.d/inetd增强版
a.手工改设置
[root@dhcplinux root]# cd /etc/xinetd.d
[root@dhcplinux xinetd.d]# ls
chargen daytime-udp finger rlogin sgi_fam tftp wu-ftpd
chargen-udp echo ntalk rsh talk time
daytime echo-udp rexec rsync telnet time-udp
[root@dhcplinux xinetd.d]# more telnet
# default: on
# description: The telnet server serves telnet sessions; it uses \
# unencrypted username/password pairs for authentication.
service telnet
{
disable = no
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
}
b. 重启xinetd服务:/etc/rc.d/init.d/xinetd restart
c. chkconfig
chkconfig --list [name]
chkconfig --add name
chkconfig --del name
chkconfig [--level levels] name <on|off|reset>
chkconfig [--level levels] name
(9) /etc/services 服务及其端口号
(10) /etc/hosts.allow或hosts.deny TCP wrapper提供一种更为灵活和有效的方法来实现对服务请求用户的限制
基本格式: 程序名列表:主机名/IP地址列表
程序名列表指定一个或者多个提供相应服务的程序的名字,名字之间用逗号或者空格分割,可以在inetd.conf文件里查看提供相应服务的程序名:如上面的文件示例中,telent所在行的最后一项就是所需的程序名:in.telnetd。
主机名/IP地址列表指定允许或者禁止使用该服务的一个或者多个主机的标识,主机名之间用逗号或空格分隔。程序名和主机地址都可以使用通配符,实现方便的指定多项服务和多个主机。
Linux提供了下面灵活的方式指定进程或者主机列表:
1.一个以"."起始的域名串,如 .amms.ac.cn 那么www.amms.ac.cn就和这一项匹配
2.以"."结尾的IP串如 202.37.152. 那么IP地址包括202.37.152. 的主机都与这一项匹配。
3.格式为n.n.n.n/m.m.m.m表示网络/掩码,如果请求服务的主机的IP地址与掩码的位与的结果等于n.n.n.n 那么该主机与该项匹配。
4.ALL表示匹配所有可能性
5.EXPECT表示除去后面所定义的主机。如:list_1 EXCEPT list_2 表示list_1主机列表中除去List_2所列出的主机
6.LOCAL表示匹配所有主机名中不包含"."的主机
上面的几种方式只是Linux提供的方式中的几种,但是对于我们的一般应用来说是足够了。我们通过举几个例子来说明这个问题:
例一:我们只希望允许同一个局域网的机器使用服务器的ftp功能,而禁止广域网上面的ftp服务请求,本地局域网由 202.39.154.、202.39.153.和202.39.152. 三个网段组成。
在hosts.deny文件中,我们定义禁止所有机器请求所有服务:
ALL:ALL
在hosts.allow文件中,我们定义只允许局域网访问ftp功能:
in.ftpd -l –a: 202.39.154 202.39.153. 202.39.152.
基本的网络管理命令
(1) ifconfig
[root@linux /root]# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:00:B4:C5:EA:90
inet addr:202.112.162.5 Bcast:202.112.162.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:3105 dropped:0 overruns:0 carrier:6210
collisions:52785 txqueuelen:100
Interrupt:11 Base address:0xa000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:3924 Metric:1
RX packets:2087 errors:0 dropped:0 overruns:0 frame:0
TX packets:2087 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
=================
手工设置IP: ifconfig eth0 192.168.1.1 netmask 255.255.255.0
[root@linux /root]# ifconfig eth0 down
[root@linux /root]# ifconfig eth0 202.112.162.20 up
[root@linux /root]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:00:B4:C5:EA:90
inet addr:202.112.162.20 Bcast:202.112.162.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:3141 dropped:0 overruns:0 carrier:6282
collisions:53397 txqueuelen:100
Interrupt:11 Base address:0xa000
=================
(2) ping
[root@linux /root]# ping 202.112.162.5
PING 202.112.162.5 (202.112.162.5) from 202.112.162.5 : 56(84) bytes of data.
64 bytes from linux.cau.edu.cn (202.112.162.5): icmp_seq=0 ttl=255 time=0.1 ms
64 bytes from linux.cau.edu.cn (202.112.162.5): icmp_seq=1 ttl=255 time=0.0 ms
64 bytes from linux.cau.edu.cn (202.112.162.5): icmp_seq=2 ttl=255 time=0.0 ms
64 bytes from linux.cau.edu.cn (202.112.162.5): icmp_seq=3 ttl=255 time=0.0 ms
--- 202.112.162.5 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.0/0.0/0.1 ms
(3) netstatnetstat –rnnetstat –lnetstat -a
[root@dhcplinux etc]# netstat –rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
202.205.93.128 0.0.0.0 255.255.255.128 U 40 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 40 0 0 lo
0.0.0.0 202.205.93.129 0.0.0.0 UG 40 0 0 eth0
You have new mail in /var/spool/mail/root
[root@dhcplinux etc]#
[root@dhcplinux root]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 nmlinux:32768 *:* LISTEN
tcp 0 0 *:32769 *:* LISTEN
tcp 0 0 *:32770 *:* LISTEN
tcp 0 0 *:999 *:* LISTEN
tcp 0 0 *:netbios-ssn *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:http *:* LISTEN
tcp 0 0 *:10000 *:* LISTEN
tcp 0 0 *:wnn4_Cn *:* LISTEN
tcp 0 0 *:ftp *:* LISTEN
[root@dhcplinux root]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 nmlinux:32768 *:* LISTEN
tcp 0 0 *:32769 *:* LISTEN
tcp 0 0 *:32770 *:* LISTEN
tcp 0 0 *:999 *:* LISTEN
tcp 0 0 *:netbios-ssn *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:http *:* LISTEN
tcp 0 0 *:10000 *:* LISTEN
tcp 0 0 *:wnn4_Cn *:* LISTEN
tcp 0 0 *:ftp *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:telnet *:* LISTEN
tcp 0 0 nmlinux:smtp *:* LISTEN
tcp 0 0 *:https *:* LISTEN
udp 0 0 *:nfs *:*
udp 0 0 *:32770 *:*
udp 0 0 *:32771 *:*
udp 0 0 *:32773 *:*
udp 0 0 202.205.93.1:netbios-ns *:*
udp 0 0 *:netbios-ns *:*
udp 0 0 202.205.93.:netbios-dgm *:*
udp 0 0 *:netbios-dgm *:*
udp 0 0 *:10000 *:*
udp 0 0 *:bootps *:*
udp 0 0 *:tftp *:*
udp 0 0 *:996 *:*
udp 0 0 *:sunrpc *:*
udp 0 0 *:753 *:*
raw 0 0 *:icmp *:* 7
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 1272 /tmp/.font-unix/fs7100
unix 2 [ ACC ] STREAM LISTENING 1210 /tmp/cd_sockV4
unix 2 [ ACC ] STREAM LISTENING 1154 /dev/gpmctl
[root@dhcplinux root]#
(4) route add |delete
添加路由:
route add -net 20.200.200.0 netmask 255.255.255.0 eth0
route add default gw 20.200.200.1 eth0
删除路由:
route delete -net 20.200.200.0 netmask 255.255.255.0
route delete default gw 20.200.200.1 eth0
附赠一份百度云word版本链接: 链接:https://pan.baidu.com/s/1k2Dgoh7ILzUitUd2i_w9AA 密码:wqlv