您的位置:首页 > 运维架构 > Linux

菜鸟学Linux-常用命令小记

2013-06-18 06:41 225 查看
ftp://192.168.0.1/biji

第一天基本命令

ls -l install.log

-:rw-r--r--: 1: root: root: 39410: 11-03 13:44: install.log

文件类型:权限:硬连接数:拥有者:所属组:文件大小:最有一次修改日期和时间:文件名

- 普通文件

d 目录文件

l 链接文件

b 块设备文件

c 字符文件

p 管道文件

s 套节字文件

ls -l 长格式 -a 显示所有文件 -A 除了.和..所有文件 -h 按最大单位显示

-d 显示目录信息 -t 按时间排序 -r 反向排序 -R 递归显示

帮助命令

1.ls --help

2.type ls

alias la='ls -la'

unalias la

3.info ls

4.man

man 1 用户命令

man 2 系统调用

man 3 库调用

man 4 特殊文明

man 5 配置文件

man 6 游戏

man 7 杂项

man 8 系统命令

man 9 内核参考

man -f passwd 查询关键字在那些章节有相应说明

makewhatis 维护man

man -k passwd 模糊查询

/目录下的目录作用

/bin /usr/bin/ /usr/local/bin/ 普通用户可以执行的命令

/sbin/ /usr/sbin/ /usr/local/sbin/ 管理员可以执行的命令

/root 管理员家目录

/home 普通用户家目录

/etc 配置文件目录

/var 服务器数据目录

/usr 应用程序目录

/tmp 临时文件目录

/dev 块设备目录

/boot 引导程序目录(内核 启动文件 引导程序)

/lib /usr/lib /usr/local/lib 存放库文件和系统的模块

/sys 伪文件系统

/proc 伪文件系统(内核参数 进程在内存的信息)

/mnt 其他挂载点

/opt 第三方软件目录

/net autofs

/misc autofs

/tftpboot tftp服务器的主目录

/selinux selinux安全组件使用

/media 媒介目录

/srv 服务器数据目录

cd / .. ../.. . ~或者直接回车 -返回上次工作的目录

查看文件内容的命令

1.more install.log

2.less install.log

3.head -n 50 install.log

head -50 install.log

4.tail -n 50 install.log

tail -50 install.log

tail -f /var/log/messages 动态监控日志文件

service gpm restart 重新启动鼠标

5.cat install.log

cat -n install.log 所有行号

cat -b install.log 只显示有效行行号

| 管道符

cat -n install.log | less

创建文件

touch aa.txt

touch cc.txt tt.txt

touch abc{1..10}.txt

touch a{b,c}{1..5}.txt

创建目录

mkdir abcd

mkdir abcde abcdf

mkdir abc{1..10}

mkdir a{b,c}{1..5}

mkdir -p xx/pp

mkdir -p -v xx/pp -p 递归 -v 显示创建过程

拷贝剪切

拷贝

cp /root/install.log /tmp/

cp /root/install.log /tmp/test 拷贝过程中重命名

cp -r /root/Desktop/ /tmp/

剪切

mv

mv /root/tt.txt /tmp/

mv /root/tt.txt /tmp/aa.txt

mv /root/abcd/ /tmp

mv ac3.txt uuu.txt

删除

rmdir 删除空目录

rm aa.txt

rm -f uuu.txt

rm -rf xx/

rm -rf *

rm -rf *.txt

rm -rf ab*

练习:

1.创建以下目录结构(一条命令完成)(并且验证)

/aa

/ \

ba/ bc/

/ \ \

ca/ cb/ cc/

mkdir -p /aa/ba/c{a,b} /aa/bc/cc

tree /aa

ls -R /aa

2.由ba目录进入ca目录有几种方法,分别是什么

3. 拷贝/etc/man.config到ca/ 拷贝过程中将man.config改名为test.txt

mkdir -p /aa/ba/c{a,b} /aa/bc/cc

cp ../etc/man.config ../aa/ba/ca/test.txt

4.剪切test.txt文件 cc/目录

mv /aa/ba/ca/test.txt /aa/bc/cc/

5.在两次拷贝同一文件到同一个目录下时,不出现覆盖提示

\cp /root/install.log /

/bin/cp install.log /

unalias cp

cp /root/install.log /

vim文本编辑器

由命令模式进入输入模式

a 当前字符后输入

A 当前行行尾输入

i 当前字符前输入

I 当前字符前输入

o 当前行的下一行输入

O 当前行的上一行输入

s 删除当前字符后输入

S 删除当前行后输入

home键 行首 end键 行尾

命令模式

u 撤销一步操作

ctrl+r 重做

复制一行 yy p粘贴(光标所在行的下一行)

复制50行 50yy

剪切一行 dd p粘贴

剪切50行 50dd

删除一行 dd

删除50行 50dd

^行首 $行尾

删除当前字符到行首 d^ y^

删除当前字符到行尾 d$ y$

w 下一个单词的首字符

yw 复制一个单词 dw剪切一个单词

G 尾行 gg 首行 572G

dgg 删除当前行到首行

dG 删除当前行到尾行

末行模式

:w q wq w! q! wq!

:w /tmp/newfile 另存

:2,4w /tmp/newfile2

:e /root/install.log 读入文件

:r /tmp/newfile2 追加读入

:e! 重新读入当前文件

查找

/ 从上向下 ?从下向上 n N 下一个

替换

:%s/ab/$$/gc %所有行 g 全局 s 交换 c交互式替换

:3,6s/ab/$$/ 3-6行替换

练习:

1.替换掉所有开头的a 换成b

%s/a/b/

2.去掉整个文件中所有的制表符(\t)

%s/\t//g

3.去掉2-6行开头的空格(\s)

2,6s/^ //g

2,6s/^\s//g

4.去掉整个文件中所有的空格

%s/\s//g

%s/ //g

%s/ *//g

5.将每行换行符号替换为空格(\n)

%s/\n/ /g

去掉空行

g/^$/d

g/^\s*$/d

调出行号

set nu

set nonu

set autoindent

set noautoindent

文件加密

X

vim配置文件/etc/vimrc

多文件操作

vim aa.txt cc.txt tt.txt

:args :next :prev :last :first ctrl+6

同一屏内打开多个文件

vimdiff aa.txt cc.txt tt.txt

ctrl+w+w

vim -O aa.txt cc.txt tt.txt

vim -o aa.txt cc.txt tt.txt 竖向排列

vim的帮助文档

/usr/share/vim/vim70/tutor/tutor.zh.euc

iconv -f UTF8 -t GB18030 tutor.zh.euc -o test.txt

-f 文件源字符集 -t 输出字符集 -o 输出文件

用户管理

uid 0 root (管理员) gid 0(管理员组)

uid 1-499 (系统用户) gid 1-499(系统组)

uid 500-60000(普通用户) gid 500-60000(普通用户组)

useradd abc1

用户信息文件/etc/passwd

abc1:x:503:503:PT:/home/abc1:/bin/bash

用户名:密码占位符:uid:gid:描述信息:家目录:shell

useradd -u 1000 -g 503 -c hello -d /mnt/abc2 -s /bin/bash abc2

-u uid -g gid -c 描述 -d 家目录 -s shell

groupadd uplooking

组信息文件/etc/group

abc2:x:503:

组名:密码:gid:如有信息用户名

groupadd -g 2000 uplooking

练习:

添加组uplooking gid为3000

添加用户 tom uid为1200 属于uplooking 家目录在/mnt下 shell为bash

groupadd -g 3000 uplooking

useradd -u 2000 -g uplooking -c oooo -d /mnt/tom -s /bin/bash tom

密码

passwd

存放密码信息的文件/etc/shadow

abc2:$1$cYSCxWGT$s9pWcN0lYAIYmd5q6/ilV1:15654:0:99999:7:::

第一列:用户名

第二列:密码

第三列:最后一次修改密码的时间

第四列:密码最小时间

第五列:密码的最大时间

第六列:密码过期前警告时间

第七列:密码过期后帐号过期时间

第八列:帐号有效期

第九列:保留列

修改组信息

groupmod

groupmod -g 2500 tt2 改gid

groupmod -n ttnew tt 改组名称

修改用户信息

usermod -u -g -c -d -s 用户名

usermod -f 密码过期后帐号过期时间 -e 帐号有效期

usermod -l newname oldname

usermod -L 锁定帐户

usermod -U 解锁帐号

修改密码信息

passwd

passwd -n 最小时间 -x 最大时间 -w 警告时间 -i 密码过期后帐号过期时间

passwd -l 锁定用户密码

passwd -u 解锁密码

passwd -S 查看密码状态

练习:

创建两个用户jack rose 分别设定为密码123

锁定jack帐号

锁定rose密码

分别终端登录 查看信息是否一致,能否判断出现什么问题?

查看/etc/shadow文件中该两个用户 密码段 有什么不同?

删除组

groupdel 组名

删除用户

userdel -r abc -r连同家目录一起删除

关于用户的配置文件

useradd eric

/etc/default/useradd 用户默认值文件

/var/spool/mail/ 用户邮件信息目录

home=/home

shell=/bin/bash

CREATE_MAIL_SPOOL=yes

/etc/login.defs 保存密码默认信息

useradd -G kf,yw wg

usermod -a -G boss abc1

usermod -G yw,kf abc1

useradd -u 0 -o admin

手工管理用户

groupadd redhat

vim /etc/groupadd

redhat:x:5000:

useradd eric

vim /etc/passwd

eric:x:5000:5000:PTuser:/home/eric:/bin/bash

mkdir /home/eric

cp /etc/skel/.bash* /home/eric/

passwd eric

vim /etc/shadow

eric::15654:0:99999:7:::

grub-md5-crypt

终端登录测试密码 创建文件aa.txt

多用户导入

1.touch user.txt

aa:x:5500:5500::/home/aa:/bin/bash

bb:x:5600:5600::/home/bb:/bin/bash

newusers < user.txt

2.touch passwd.txt

aa:123

bb:123

chpasswd < passwd.txt

家目录下文件作用

.bash_history 记录该用户执行的历史命令

.bash_logout 用户退出时执行的命令

.bashrc nologin-shell级别环境配置文件

.bash_profile login-shell登录级别环境配置文件

/etc/bashrc

/etc/profile

加载顺序

/etc/profile

~/.bash_profile

~/.bashrc

/etc/bashrc

权限管理

-rw-r--r-- 1 eric uplooking 39410 11-03 13:44 install.log

rw- | r-- | r--

user
group other

eric uplooking 其他人

u g o

root(0)------>uid=eric----->group(uplooking)---->other

文件

r----------cat head tail more

w----------vim > >>

x----------./ 执行

目录

r----------ls r-x查询详细信息

w----------touch rm -wx才能创建文件

x----------cd


tt.txt rwxrwxrwx root root

tom rm -rf tt.txt

/test/ rwxr-x--- root root

/test/tt/ rwxrwxrwx

/test/tt/aa.txt rwxrwxrwx

tom rm -rf aa.txt

chmod u+r file/dir

chmod u+r,g-w file/dir

chmod u+r,g-r,o+rw file/dir

chmod ug+rw,o-rw file/dir

chmod ugo+rwx file/dir

chmod a+rwx file/dir

chmod u=r file/dir

chmod u=rw,g=rx file/dir

chmod u=r,g=rx,o=rwx file/dir

chmod ug=rw,o=tx file/dir

chmod ugo=rwx file/dir

chmod a=rwx file/dir

chmod -R 777 test/

r------4 w------2 x------1

rwxrwxrwx-777

561 r-xrw---x -wx-w---x 321

234 -w--wxr--

/ 755

/etc 755

/bin 755

/sbin 755

/var 755


/tmp 1777

/etc/shadow 400

/etc/passwd 644

root管理员创建文件默认权限 644

root管理员创建目录默认权限 755

普通用户创建文件的默认权限 664

普通用户创建目录的默认权限 775

/root 750

/home/普通用户家目录 700

chown tom file/dir

chgrp uplooking file/dir

chown tom.uplooking file/dir

chown tom:uplooking file/dir

chown .uplooking file/dir

chown -R tom.uplooking file/dir

练习:

1.在tmp目录下创建目录test 在test目录下创建文件aa.txt 创建用户zorro

要求:

zorro用户可以删除aa.txt 但没是不能读写

mkdir /tmp/test/ root root rwxr-xr-x

touch /tmp/test/aa.txt root root rw-r--r--

chmod o-r /tmp/test/aa.txt

chmod o+w /tmp/test

2.在tmp目录下创建目录test 在test目录下创建文件aa.txt 创建用户zorro eric

要求:

zorro用户可以读写aa.txt文件 不能删除

eric用户不可以读写aa.txt 但是可以删除

/tmp/test/ eric root rwxr-xr-x

/tmp/test/aa.txt zorro root rw-r-----

zorro

eric

chown zorro /tmp/test/aa.txt

chmod o-r /tmp/test/aa.txt

chown eric /tmp/test/

3.在/root/下创建abc.txt文件 创建3个用户zorro,shrek,seker 要求如下:

seker可以读写abc.txt文件 但是不能删除

zorro只读abc.txt文件 不能删除

shrek 不可以读写abc.txt文件 但是可以删除

/root/ root root rwxrwxr-x

/root/abc.txt seker zorro rw-r-----

seker

zorro

shrek ----> root

chmod 640 abc.txt

chown seker.zorro abc.txt

chmod 775 /root/

usermod -a -G root shrek

shrek shrek shrek,hello,root

高级权限

suid权限

suid只能加到user

chmod u+s 文件

suid只能作用到文件上(二进制可执行文件)

作用:任何用户在执行拥有suid权限的命令时,都已该命令拥有者的身份执行

练习:

要求:在不改变/etc/shadow权限的前题下,使用tom能使用cat查看到shadow内容

which cat

chmod u+s /bin/cat

sgid

只能添加到group

chmod g+s dir/

只能作用到目录上

作用:任何用户在拥有sgid权限的目录下创建文件(目录)时,创建的文件与该目录同组

如果创建目录,则目录继承sgid权限.

练习:

在/tmp目录下创建test目录, 该目录权限777,创建用户tom

tom在/tmp/test目录下创建文件aa.txt 查看aa.txt信息

在test目录上添加sgid权限

tom在/tmp/test目录下创建文件tt.txt 查看tt.txt信息

tom在/tmp/test目录下创建创建目录dir 查看目录dir的信息

sticky(t)冒险位,粘贴位

只能添加到other

chmod o+t dir/

只能作用到目录上

作用:任何用户在含有t权限的目录下创建的文件,只能拥有者删除,其他人无权删除(除了root)

练习:

在/tmp目录下创建test目录, 该目录权限777,创建用户tom 创建用户jerry

使用tom 在test目录下创建文件tom.txt

使用jerry 在test目录下创建文件jerry.txt

尝试使用tom删除jerry.txt 使用jerry删除tom.txt

是否能删除?

使用tom 在test目录下创建文件tom.txt

使用jerry 在test目录下创建文件jerry.txt

在test目录上添加t权限

尝试使用tom删除jerry.txt 使用jerry删除tom.txt

是否能删除?

使用管理员root能否删除?

使用tom 在test目录下创建文件tom.txt

使用jerry 在test目录下创建文件jerry.txt

添加用户zorro 将test目录拥有者改为zorro

使用zorro能否删除tom.txt jerry.txt?

源文件有无x权限区别

chmod +x aa.txt

[root@localhost tmp]# ll aa.txt

-rwsrwsrwt 1 root root 0 11-11 14:20 aa.txt

[root@localhost tmp]# chmod -x aa.txt

[root@localhost tmp]# ll aa.txt

-rwSrwSrwT 1 root root 0 11-11 14:20 aa.tx

suid-------4

sgid-------2

sticky-----1

1777

2777

4777

3777

进程管理

ps -ef

ps aux

top

前后台切换

xclock -update 1 &

xclock -update 1

ctrl + z

jobs

bg 编号

fg 编号

kill %编号 杀掉

杀进程

kill -15 pid

kill -9 pid

pkill 进程名称

killall 进程名称

xkill

优先级调整(-20~19)

nice -n -20 xclock -update 1

renice 10 -p 6020(pid)

高级命令

cat -n /etc/passwd |head -20 |tail -2 | rev | tac

wc -l行数 -w单词数 -c字符数

sort -n 按照整个数字排序 -u 去掉重复行 -r 反向排序

uniq 去掉连续的重复行 -u 显示不重复的行 -d 只显示重复行 -c统计重复次数

grep root /etc/passwd

grep ^root /etc/passwd

grep halt$ /etc/passwd

grep -v halt$ /etc/passwd 取反

cut

cut -d: -f 1 /etc/passwd

cut -d: -f 1,6 /etc/passwd

cut -d: -f 1-3 /etc/passwd

cut -d: -f 1-3,6 /etc/passwd

cut -c 1,3 /etc/passwd | head -2

cut -c 1-6 /etc/passwd | head -2

练习:

1.计算出你的系统中有多少个可以登录到系统的用户

cat /etc/passwd | grep -c bash

cat /etc/passwd | grep /bin/bash | wc -l

cat /etc/passwd | grep /bin/bash | cut -d: -f7 |uniq -c

cat -n /etc/passwd | grep /bin/bash | cut -d: -f1,7 |cat -n

2.ifconfig eth0 | awk -F':| +' '/Bcast/{print $4}'

192.168.1.1

ifconfig eth0 | head -2 | tail -1 | cut -d: -f 2 | cut -d' ' -f1

ifconfig eth0 | grep Bcast | cut -d: -f 2 | cut -d' ' -f 1

3. stat aa.txt

File: “aa.txt”

Size: 34 Blocks: 8 IO Block: 4096 一般文件

Device: 802h/2050d Inode: 48922685 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2012-11-17 12:06:11.000000000 +0800

Modify: 2012-11-17 12:06:11.000000000 +0800

Change: 2012-11-17 12:06:11.000000000 +0800

只显示时间

12:06:11

12:06:11

12:06:1

stat aa.txt | tail -3 | cut -d' ' -f3 | cut -d. -f 1

shell基础

!! !$ !ser

重定向符

> >>

> 标准正确输出,如果文件存在则覆盖,如果文件不存则创建

>> 标准正确输出,如果文件存在则追加,如果不存在则创建

2> 标准错误输出,如果文件存在则覆盖,如果文件不存在则创建

2>> 标准错误输出,如果文件存在则追加,如果不存在则创建

0标准正确输入

&>

循环脚本

#!/bin/bash

#添加用户的脚本

for i in {1..10}

do

echo "create user$i now!!!!!please wait!!!!"

sleep 1

useradd user$i

echo "Set user$i password now!!!!!"

echo 123 | passwd --stdin user$i &> /dev/null

echo "The create user$i success!!!!!"

done

判断脚本

#!/bin/bash

#判断文件是否存在

if [ -f /tmp/aa.txt ]

then

echo "the file is cunzai!!!!!display now!!!1"

sleep 1

ls -l /tmp/aa.txt

else

echo "the file is now cunzai!!!create it now !!!! "

sleep 1

touch /tmp/aa.txt

fi

网络

mii-tool

ifconfig

ifconfig eth0

ifconfig eth0 192.168.1.100/24

setup

配置文件

/etc/sysconfig/network-scripts/ifcfg-eth*

网卡别名

ifconfig eth0:0 10.10.10.1

ifconfig eth0 up

ifconfig eth0 down

ifup eth0

ifdown eth0

; 前边命令无论是否执行成功,后边命令都执行

&& 前边命令执行成功,后边命令才执行

arping 192.168.1.33

arp

route -n

route add default gw 192.168.1.1

route del default gw 192.168.1.1

traceroute

hostname 查询主机名

hostname zhb.com

vim /etc/sysconfig/network 永久修改

HOSTNAME=zhb.com

注意:主机名和ip地址的对应关系

/etc/hosts

打开路由转发功能

cat /proc/sys/net/ipv4/ip_forward

echo 1 > /proc/sys/net/ipv4/ip_forward

cat /proc/sys/net/ipv4/ip_forward

压缩,打包,安装软件

gzip bzip2

dd if=/dev/zero of=data bs=100M count=2 ***200M的文件

gzip data

gunzip date.gz

bzip2 data

bunzip2 data.bz2

tar

tar -cvf boot.tar /boot

tar -tvf boot.tar

tar -rvf boot.tar data

tar -xvf boot.tar

tar -cvf /home/boot.tar /boot/

tar -xvf boot.tar -C /home/

tar -zcvf boot.tar.gz /boot

tar -ztvf boot.tar.gz

tar -zxvf boot.tar.gz

tar -jcvf boot.tar.bz2 /boot

tar -jtvf boot.tar.bz2

tar -jxvf boot.tar.bz2

tar -zcvf /home/boot.tar.gz /boot/

tar -zxvf boot.tar -C /home/

mount(挂载)

mount -t iso9660 /dev/cdrom /mnt

mount

umount /dev/cdrom

umount /mnt

-l 强制卸载

***镜像

cat /dev/cdrom >> /rhel5u8.iso

dd if=/dev/cdrom of=/rhel5u8.iso

挂载镜像

mount -t iso9660 /rhel5u8.iso /mnt -o loop

软件安装

rpm -ivh httpd-2.2.3-63.el5.i386.rpm 安装

rpm -q httpd 查询是否安装

rpm -qa | grep httpd 查询所有安装含有httpd关键字的软件包

rpm -ql httpd 查询软件包的安装路径

rpm -qf httpd.conf 查询该文件是那个软件包安装的

rpm -qf `which mount` 同上``优先执行

rpm -qi httpd 查询httpd软件包的信息

rpm -e httpd 卸载软件包

未安装的软件包

rpm -qpi httpd-2.2.3-63.el5.i386.rpm未安装信息

rpm -qpl httpd-2.2.3-63.el5.i386.rpm安装后的路径

rpm -e apr-devel --nodeps 不检测依赖关系卸载

rpm -ivh httpd-2.2.3-63.el5.i386.rpm --force 强制安装

yum库搭建与使用

1.mount /dev/cdrom /mnt

2.vim /etc/yum.conf

gpgcheck=0

3.cd /etc/yum.repos.d/

touch yum.repo

vim yum.repo

[yum]

name=yum server

baseurl=file:///mnt/Server

enabled=1

gpgcheck=0

yum list

yum install httpd-devel

yum remove httpd-devel

yum reinstall httpd-devel

yum clean all

rpm --import RPM-GPG-KEY-redhat-release 导入key

创建yum库

建库命令

rpm -ivh createrepo-0.4.11-3.el5.noarch.rpm

/yum 里边全是软件包

createrepo /yum
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: