SystemManage_系统管理

--------------------**文件管理***--------------------------------------

-----修改配置文件时:

a. cp httpd.conf httpd.conf.bak //备份

b. chmod a-w httpd.conf.bak //设置写保护

------以root运行单一命令

su root -c 'ls /root/' //提示输入root密码

-------sudo:选择性访问超级用户命令(无需root密码,只需当前登录用户aaa的密码,即可执行sudoers文件中,所列出的aaa用户可以执行的高级指令)

文件:/etc/sudoers 分四部分

1.Host alias

2.User alias

3.Command alias

4.User specification: who can do what where

host=command(s)

命令:

a.sudo -l //列出当前用户可以通过sudo使用的命令

b.visudo //编辑/etc/sudoers

---aaa用户登录

c.sudo shutdown //此时只需通过输入aaa用户的密码,如果aaa是
//sudo列表中的用户,则可以通过验证执行sudoers文件中规定的root级别密码。

-------发送消息:write/talk/wall:向'已登录系统的用户'--//who 来查看登录用户

-----write:'只能本机用户之间发消息'

d.write user [ttyname] //向指定终端下的user发送消息。Ctrl+D结束.

. //当某user登录来多个终端,可以通过tty来指定

----talk:'可以网络间不同主机'

e.talk user [ttyname] //user 由 who 命令查看的结果 确定
//user格式:user(和本机的用户talk)
//user@hostname 或 user@IP(和其他host通信);
//ttyname: 当同一个user在多个终端登录,才须指定ttyname。is of the form ‘ttyXX’ or ‘pts/X’
' 详情查看man talk'

----talk服务详解:

'启动过程':1。安装talk服务;

rpm -qa | grep talk

talk-server-0.17-33.2.4.i686 //talk服务端
talk-0.17-33.2.4.i686 //talk客户端:默认安装



2。修改'/etc/xinetd.d/[k]talk'文件

disable = yes --> no //disable=no:允许xinetd服务开启talk服务

user = root--> nobody //user=root:只有登录ttyn 或 pts/n 的root用户可以启动 talk
//user=nobody:所有登录.................用户都可启动 talk
3。重启'xinetd'服务

service xinetd restart

'注': 如果和本地用户talk,1和2,3步即可;

如果和其他host用户,仍需添加对host的 domain name 解析('两种方式'):
a> /etc/hosts 192.168.0.148 RHEL //在fedora主机中添加此行
/etc/hosts 192.168.0.162 FEDORA //在RHEL主机中添加此行

b> talk jacky@RHEL(@192.168.0.148) //fedora主机的talk deamon 尝试与 rhel的talk deamon建立连接
talk jacky@fedora //rehl主机的talk deamon 同意建立连接

c> 通过配置DNS服务

否则:在talk jacky@RHEL后,报错 'Target machine didnot recognize us'


----wall:'向本机用户发消息'

f.wall //write all;向所有人发消息

g.mesg y|n //在终端上屏蔽root用户之外的用户发送的消息,通常用来限制其他用户使用write,访问你的终端


-------更改文件所有权:chown / chgrp

注:文件的用户所有者 和 组所有者 是 相对独立的。

访问类别 操作 访问类型

u + r
g + - w
o = x
a X(??)
u,g,o

1.chown [-R] new-owner[:new-group] files //更改用用户所有者和组所有者

chown jacky /var/log //之更改 用户所有者

chown -R aaa:group1 /home/bbb //递归更改/home/bbb目录下所有目录和文件的 用户及组所有者

2.chgrp [-R] new-group file //只更改文件的组所有者


-------文件保护

注:删除文件并 不需要 对该文件拥有 写权限,只需对该文件所在目录拥有 写权限 即可,
删除过程实际上是:从目录文件中删除相关项目,并未直接操作普通文件。(在赋予目录写权限时,谨慎)

目录文件:其信息是包含 文件的文件名 && 及其所指向的磁盘信息。 目录项=文件名-inode。

--------setuid,setgid,sticky的八进制位分别是4, 2, 1,助记法表示为u+s,g+s,o+t,(删除标记位是u-s,g-s,o-t),例如:

chmod 4644 frogleap.swf *结果是“-rwSr?r?”
chmod u+s frogleap.swf *结果和上面相同

我们在Linux 系统中的超级权限的控制中 有提到过我们知道Linux的用户管理是极为严格的,不同的用户拥有不同的权限,
为了完成只有root用户才能完成的工作,我们必须为普通用户提升权 限,
最常用的方法就是su或sudo虽然setuid 和setgid也是让普通用户超越自身拥有的普通权限达到root权限的方法,
但我不推荐大家使用,因为它能为系统带来隐患!!

注意:setuid和setgid会面临风险,所以尽可能的少用,了解了解既可~~~

---------setuid和setgid的实例应用
例子1

  我们想让一个普通用户beinan拥有root用户拥有超级rm删除权限,我们除了用su或sudo 临时切换到 root身份操作以外,还能怎么做呢

  [root@localhost ~]#cd /home //注:进入/home目录

  [root@localhost home]# touch beinantest.txt //注:创建一个文件;

  [root@localhost home]# ls -l beinantest.txt //注:查看文件属性;

  -rw-r--r-- 1 root root 0 04-24 18:03 beinantest.txt //注:文件的属性;

  [root@localhost home]# su beinan //注:切换到普通用户 beinan

  [beinan@localhost home]$ rm -rf beinantest.txt //注:以普通用户身份来删除beinantest.txt文件;

  rm: 无法删除 beinantest.txt: 权限不够

  那我们怎么才能让beinan 这个普通用户也拥有root超级的rm 删除功力呢?

  [root@localhost ~]# ls -l /bin/rm

  -rwxr-xr-x 1 root root 93876 02-11 14:43 /bin/rm

  [root@localhost ~]# chmod 4755 /bin/rm //注:设置rm的权限为4755 , 就把setuid 位设置好了

  [root@localhost ~]# ls -l /bin/rm

  -rwsr-xr-x 1 root root 43980 02-11 14:43 /bin/rm

  [root@localhost ~]# cd /home/

  [root@localhost home]# su beinan //注:切换到beinan用户身份;

  [beinan@localhost home]$ ls -l beinantest.txt //注:查看文件属性;

  -rw-r--r-- 1 root root 0 04-24 18:03 beinantest.txt //注:文件的属性;

  [beinan@localhost home]$ rm -rf beinantest.txt //注:删除beinantest.txt文件;

  我们只是设置了rm的setuid位,让普通用户在rm'指令上'有超级root的删除超级权力

例2
同时设置setuid和setgid位:

[root@localhost ~]# chmod 6755 gooddoc.txt

-------Sticky Bit 粘着位-----------

Sticky对目录的效果:如果目录具备sticky bit,那么目录下的为文件只有文件 /*所有者*/ 或 /*root*/ 或 /*目录的所有者*/ 才有权删除

1.chmod o+t /tmp //添加
2.chmod o-t /tmp //删除






-------目录操作的最小权限

r-- //可以使用‘不带参数’的ls查看目录(因为目录文件记录了其中的文件名)
--x //可以通过cd进入目录
'r-x //ls -l + cd '
-wx //可以存放文件,但看不到其他文件。
rw- //无法ls -l

***ls -l 必须要有x权限(因为文件大小等信息存放,需要从磁盘信息inode中判断)****

-------指定默认的文件模式

umask 022 //为今后创建的目录指定默认模式:777-022=755(即今后创建目录的默认模式)
//普通文件默认模式:666-022=644

下面是另外一个例子,假设这次u m a s k值为0 2 2


1) 文件的最大权限rwx rwx rwx (777)


2 ) u m a s k值为0 2 2 - - - -w- -w-


3) 目录权限rwx r-x r-x (755) 这就是目录创建缺省权限


4) 文件权限rw- r-- r-- (644) 这就是文件创建缺省权限

-------查看文件类型

file filename

file /etc/*



*/-------更改文件访问权限

----ACL(Access Control List)访问控制列表,

1.ACL 是由一系列的Access Entry(访问记录)所组成的. 每一条Access Entry定义了特定的类别可以对文件拥有的操作权限.

Access Entry有三个组成部分: Entry tag type /qualifier (限定符【optional可选的】)/permission(rwx)

====== man acl 查看:

A.Entry tag type类型:

1.ACL_USER_OBJ: 相当于Linux里file_owner的permission
2.ACL_USER: 定义了额外的用户可以对此文件拥有的permission
3.ACL_GROUP_OBJ: 相当于Linux里group的permission
4.ACL_GROUP: 定义了额外的组可以对此文件拥有的permission
6.ACL_OTHER: 相当于Linux里other的permission
5.ACL_MASK: 定义了ACL_USER, ACL_GROUP_OBJ和ACL_GROUP的最大权限 (这个我下面还会专门讨论):

/* ACL_USER —— —— —— —— ACL_GROUP
\ /
\ ACL_MASK /
\ /
ACL_USER_OBJ ACL_GROUP_OBJ ACL_OTHER

//setfacl默认重新计算mask=三者权限的‘并集’

*/


B.qualifier

C.permission(权限):rwx


2.ACL_MASK 和 Effective permission

在Linux file permission里面大家都知道比如对于rw-rw-r--来说, '中间的那个rw-'是指文件组的permission.

但是在ACL里面这种情况只是在ACL_MASK不存在的情况下成立. 如果文件有ACL_MASK值,那么当中那个rw-代表的就是mask值而不再是group permission了


3.ACL相关命令:

1.getfacl命令是用来读取文件的ACL,

2.setfacl是用来设定文件的Acess ACL.

3.chacl是用来改变文件和目录的Access ACL and Default ACL



----getfacl

A.The output format of getfacl is as follows:

1: # file: somedir/ //1-3行:指明ls -l列出的 文件的:filename/owner/group
2: # owner: lisa //5 && 7 && 10: basic access rule ---基本规则
3: # group: staff //6 && 8 && 9 :extend access rule --扩展规则:增添自定义的用户和组对file访问权限
4: # flags: -s- //4:indicates the setuid (s),setgid(s),and sticky(t) bits:sst
5: user::rwx
6: user:joe:r \ #effective:r--
7: group::rw ——|---\ #effective:r--
8: group:cool:rwx / | #effective:r-x
9: mask::r-x <-------| //mask:ACL_MASK的定义. 它规定了ACL_USER, ACL_GROUP_OBJ和ACL_GROUP的最大权限
10: other::r-x
11: default:user::rwx //11-15:Default ACL :对目录的ACL权限设置(Dirctory 可能有 a default ACL,普通文件永远没有a default ACL)
//并且在此目录下 建立的目录 都将继承此目录的 'default ACL'

12: default:user:joe:rwx #effective:r-x
13: default:group::r-x
14: default:mask::r-x
15: default:other::---

//mask只是规定了三者权限的上限,三者中每一个的具体权限由其分别对应的记录决定;
//基本规则 和 扩展规则 等级相同,不存在继承关系(如,joe并不继承other的x权限)

B.常用参数形式

1.getfacl [-a|-d|-c] aa.txt //列出aa.txt的filename owner group && ACL [&& a default ACL(目录)]
// -c ACL [&& a default ACL]
// -d filename owner group


-----setfacl

A.选项

1.选项 -m 和 -x 后面需要'显示的写出ACL规则',多条acl规则以逗号(,)隔开。

5.当使用-M,-X选项从'文件'或'标准输出'中读取规则时,setfacl接受getfacl命令输出的格式。每行至少一条规则,以#开始的行将被视为注释。

2.选项--set和--set-file用来设置文件或目录的acl规则,先前的设定将被覆盖。

3.选项-m(--modify)和-M(--modify-file)选项修改文件或目录的acl规则。

4.选项-x(--remove)和-X(--remove-file)选项删除acl规则。'不能删除base ACL rules'



6.选项
-b,--remove-all //只删除所有扩展规则(extended entries),基本的acl规则(所有者,群组,其他)将被保留。

-k,--remove-default //删除缺省的acl规则。如果没有缺省规则,将不提示。

-n,--no-mask //不要重新计算有效权限。setfacl默认会重新计算ACL mask,除非mask被明确的制定。

--mask //重新计算有效权限,即使ACL mask被明确指定。

-d,--default //设定默认的acl规则。

--restore=file //从文件恢复备份的acl规则(这些文件可由getfacl -R产生)。
//通过这种机制可以恢复整个目录树的acl规则。此参数不能和除--test以外的任何参数一同执行。

--test //测试模式,不会改变任何文件的acl规则,操作后的acl规格将被列出。

-R,--recursive //递归的对所有文件及目录进行操作。

-L,--logical //跟踪符号链接,默认情况下只跟踪符号链接文件,跳过符号链接目录。

-P,--physical //跳过所有符号链接,包括符号链接文件。

--version //输出setfacl的版本号并退出。

--help //输出帮助信息。


B.命令形式:

-----'-m'更改ACL规则

1.setfacl -m user:john:rw test.txt test1.txt //更改扩展ACL规则(extended ACL rule)

2.setfacl -m group:dev:r test.txt

3.setfacl -m u:john:rw,g:dev,r test.txt

4.setfacl -m u::r,g::rw,o::rw jen.txt //更改基本ACL规则(base ACL rules)【简写】

5.setfacl -m d:u:aaa:rx dir/ d:'为路径添加 default ACL rules'
'setfacl -k dir/' '删除default ACL rules'


-----'-m m'设置rights mask

1.setfacl -m m::rx file // Revoking write access(撤销受mask约束的三者的写权限)



-----'-n'不重新计算mask

[jacky@localhost txt]$ setfacl -n -m u:aaa:x 123
[jacky@localhost txt]$ getfacl 123
# file: 123
# owner: jacky
# group: jacky
# flags: --t
user::r--
user:aaa:--x #effective:---
group::rw-
mask::rw- //mask没有执行权限
other::rw-


-----'-x'删除'指定扩展的'ACL规则

3.setfacl -x u:bbb,g:jacky jen.txt


-----'-k'删除'所有default规则' //只有目录拥有default规则

1.setfacl -k dir/

-----'-b'删除'所有扩展规则'

4.setfacl -b jen.txt test.txt //删除所有扩展规则(689



-----'copying the ACL of one file to another'

1.getfacl file1 | setfacl --set-file=- file2 //从标准输入读取ACL规则,作为--set-file参数

2.getfacl file1 > rule
setfacl --set-file=rule file2 //直接从文件读取ACL规则

//-M 与 --set-file等价

-----'Copying the access ACL into the Default ACL'

1.getfacl -a dir1 | setfacl -d -M- dir2 //-a:将dir1的 ‘Base ACL’ 拷贝为 dir2的 ‘Default ACL’
// 如果dir1拥有Default ACL,不加-a,将报错


-----'批量修改 或 恢复 文件目录权限'

a.getfacl -R dir > /home/acl.bak // Restore a permission backup created by `getfacl -R' or similar
//-R:All permissions of a complete directory subtree are restored
b.chmod -R 700 dir

d.setfacl [--test] --restore=/home/acl.bak //回复 dir 下文件的权限,执行该命令时的当前路径必须在 'dir'的 父目录下
//--test:Test mode. Instead of changing the ACLs of any files, // the resulting ACLs are listed.

'注':acl.bak 存在哪里都可以,但是 执行 '--restore=file时' 的'当前路径'必须是 dir父目录

-------chacl

1.chacl -B jen.txt //它可以彻底删除文件或者目录的ACL属性(包括Default ACL).
//比如你即使用了setfacl -x删除了所有文件的ACL属性,那个+号还是会出现在文件的末尾.
setfacl -b jen.txt //所以正确的删除方法应该是用 chacl -B 删除文件的 ACL 属性 或者 setfacl -b jen.txt

------扩展命令:

1.cp -p aa.txt //这样在拷贝文件的时候也将拷贝文件的ACL属性.对于不能拷贝的ACL属性将给出警告

2.mv 命令将会默认地移动文件的ACL属性. 同样如果操作不允许的情况下会给出警告



=----==注意事项:

1.chmod命令改变Linux file permission的时候相应的ACL值也会改变.反之改变ACL的值,相应的file permission也会改变

----'没改变之前'

[root@fedora conf]# getfacl -R dir/
# file: dir/
# owner: jacky
# group: jacky
user::rwx
user:aaa:r-x
group::rwx
mask::rwx
other::r-x
default:user::rwx
default:group::rwx
default:other::rw-

----'chmod之后'

[jacky@fedora conf]$ chmod -R 700 dir/
[jacky@fedora conf]$ getfacl dir/
# file: dir/
# owner: jacky
# group: jacky
user::rwx
user:aaa:r-x #effective:---
group::rwx #effective:---
mask::--- //700中间的0是mask的值
other::---
default:user::rwx
default:group::rwx
default:other::rw-



-----------------------***进程管理**************************

进程:一个正在执行的程序(可执行程序 在 其地址空间(所占有的内存位置)中 执行 ),其作用:在内存中为程序开辟地址空间

----查看进程号:

----'pgrep'

1.pgrep named //列出指定进程名称的进程ID

2.pgrep -l httpd //列出 进程名称 && 进程ID

3.pgrep -l -t pts/3 //列出 由指定终端控制的 进程

4.pgrep -l -u jacky,aaa //列出 属于指定用户的 进程

5.pgrep -l -u jacky named //列出 属于jacky的named deamon的 PID ** PName

5.pgrep -l ....... //???待查

----'ps'

2.ps aux | egrep 'named|PID' //可以显示ps显示的表头


----查看进程状态:

1.ps命令:显示正在运行进程的静态状态。// ps :displays information about a selection of the active processes.

2.top命令:动态显示进程状态。 //top:displays a repetitive update of the selection and the displayed information.

3.netstat -nltp |grep smb //查看进程|服务占用的端口port

EXAMPLES


1.To see every process on the system using standard syntax:(标准形式)
ps -e
ps -ef
ps -eF
ps -ely

[root@localhost ~]# ps -ef | more -4

UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Sep02 ? 00:00:04 /sbin/init
root 2 0 0 Sep02 ? 00:00:00 [kthreadd]
root 3 2 0 Sep02 ? 00:00:13 [ksoftirqd/0]


2.To see every process on the system using BSD syntax:(BSD形式)
ps ax //a:列出所有 拥有终端的进程;x:列出所有all processes owned by you (same EUID as ps);ax:列出所有进程
ps axu //u: display user-oriented format(面向用户的):用户为主的格式


[root@localhost ~]# ps aux | more -4

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 2888 536 ? Ss Sep02 0:04 /sbin/init
root 2 0.0 0.0 0 0 ? S Sep02 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Sep02 0:13 [ksoftirqd/0]



3.To see every process running as root (real & effective ID) in user format:
ps -U root -u root u

4.To Restrict the selection to only running processes.//列出 当前终端 正在运行 的进程
ps r





----杀死进程:

1前台进程:当需要中断一个前台进程的时候,通常是使用组合键;


2后台进程:这时就必须求助于kill命令.至于终止后台进程的原因很多,(或许是该进程占用的CPU时间过多;或许是该进程已经挂死。总之这种情况是经常发生的。)


-----kill命令:kill命令是通过 '向进程发送指定的信号' 来结束进程的。

格式: // kill [-9] PID

   /*a*/.如果没有指定发送信号,那么'默认值'为'TERM信号'。TERM信号将'终止'所有'不能捕获'该信号的进程。

/*b*/ 至于那些可以捕获该信号的进程可能就需要使用kill(9)信号了,该信号是不能被捕捉的。

----进程分类:

-------1.交互进程:由终端会话产生和控制.即可在前台执行,也可在后台执行。

前台进程保持和终端连接,例如:等待一个输出结果。

作业控制可使进程 从前台 -》 后台,此时进程暂停执行,终端控制权回到他的父进程。

大多数shell提供的控制 前台 和 后台程序 的方法

1. long_cmd & //在后台执行

2. ^Z //(停止前台进程)前台进程 转向 后台;终端控制权回到它的父进程(通常时shell)

3. jobs //列出后台程序

4. %n //引用第n号后台作业

5. fg %1 //把后台程序 切换到 前台

6。 %?find //引用命令中包含指定字符的后台作业命令

7. bg %?find //重启终止的后台程序

8. ^Z^X //暂停后台程序???

9. kill %2 //杀死作业号为2的进程


------2。批进程;和终端无关,而是被直接提交到队列,作业从队列中顺序执行。


1.batch //??待研究

------3.后台程序:服务器进程,通常在启动时初始化,并且在系统正常工作时在后台持续运行,等待其他进程的调用。

init //第一个创建的进程

nfsd //网络文件系统:Unix系统内在的网络文件共享

xinetd //TCP/IP后台主进程,负责启动telnent/ftpd/talk...程序。/etc/inetd.conf

。。。


--------进程属性

1.




--------启动信息:

---dmesg://print or control the kernel ring buffer,查看启动信息

注:kernel将开机信息存储在ring buffer中,开机来不及查看,可以使用dmesg查看/var/log/dmesg

1.dmesg | more

2.dmesg > message.txt //系统bootup消息

3.dmesg | tail -f //打印输出最近一次信息






---------------------------$PATH设置—————————————————————————————————————————————————————————————————————————————————————————————————————

$PATH:决定了shell将到哪些'目录'中'寻找命令或程序',PATH的值是一系列目录,当您运行一个程序时,Linux在这些目录下进行搜寻编译链接。

  编辑你的 PATH 声明,其格式为:

  PATH=$PATH:<PATH 1>:<PATH 2>:<PATH 3>:------:<PATH N>

  你可以自己加上指定的路径,中间用冒号隔开。环境变量更改后,在用户下次登陆时生效,如果想'立刻生效',则可执行下面的语句:$ source .bash_profile

  需要注意的是,最好不要把当前路径 “./” 放到 PATH 里,这样可能会受到意想不到的攻击。完成后,可以通过 echo $PATH 查看当前的搜索路径。这样定制后,就可以避免频繁的启动位于 shell 搜索的路径之外的程序了。



可用 export 命令查看PATH值

[root@localhost u-boot-sh4]# export
declare -x CVS_RSH="ssh"
declare -x DISPLAY=":0.0"
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="localhost"
declare -x INPUTRC="/etc/inputrc"
declare -x LANG="zh_CN.UTF-8"
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
declare -x LS_COLORS="no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:"
declare -x MAIL="/var/spool/mail/root"
declare -x OLDPWD="/root"
declare -x PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
declare -x PWD="/opt/STM/STLinux-2.3/devkit/sources/u-boot/u-boot-sh4"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_ASKPASS="/usr/libexec/openssh/gnome-ssh-askpass"
declare -x TERM="xterm"
declare -x USER="root"
declare -x XAUTHORITY="/root/.xauthkSzH7b"

单独查看PATH环境变量,可用:

[root@localhost u-boot-sh4]#echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

添加PATH环境变量,可用:

[root@localhost u-boot-sh4]#export PATH=/opt/STM/STLinux-2.3/devkit/sh4/bin:$PATH

再次查看:

[root@localhost u-boot-sh4]# echo $PATH
/opt/STM/STLinux-2.3/devkit/sh4/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

说明添加PATH成功。

上述方法的PATH '在终端关闭' 后就会消失。所以还是建议通过编辑/etc/profile来改PATH,也可以改家目录下的.bashrc(即:~/.bashrc)。

第二种方法:
# vim /etc/profile
在文档最后,添加:
export PATH="/opt/STM/STLinux-2.3/devkit/sh4/bin:$PATH"
保存,退出,然后运行:
#source /etc/profile
不报错则成功。

---------/**Linux export 命令**/

功能说明:'设置'或'显示'环境变量。

语  法:export [-fnp][变量名称]=[变量设置值]

补充说明:在shell中执行程序时,shell会提供一组环境变量。
export可新增,'修改'或'删除'环境变量,供后续执行的程序使用。
export的'效力'仅及于'该次登陆操作'。

参  数:
 -f  代表[变量名称]中为函数名称。
 -n  删除指定的变量。变量实际上并未删除,只是不会输出到后续指令的执行环境中。
 -p  列出所有的shell赋予程序的环境变量。

一个变量创建时,它不会自动 地为在它之后创建的shell进程所知。而命令export可以向后面的shell传递变量的值。
当一个shell脚本调用并执行时,它不会自动得到原为脚本(调用者)里定义的变量的访问权,除非这些变量已经被显式地设置为可用。
export命令可以用于传递一个或多个变量的值到任何后继脚本。 ----《UNIX教程》

_____________________________________________________________________________________________________



----------------复制目录树-------------------------------------------

'a'---cp命令

/**/'cp -rp /home/t/* /temp' //复制这个目录树,并保护/home/t中文件(目录)的 mode,ownership,timestamps
\
--------->'可能涉及到t/ 下的文件属于其他用户,所以运行此命令 必须root权限'
/
'cp -a /home/t/* /temp' //复制整个目录树,并保护/home/t............ 所有属性


'b'---tar命令

'tar cf /dev/rmt0 .' //将当前目录中的文件复制到磁带

/**/'tar cpf - . | (cd /temp/; tar xpvf -)' //需要root权限;等价于<=> tar cpf - . | tar xpvf - -C /temp/

[root@localhost jacky]# tar cfp - -C /etc resolv.conf | tar xfp - -C Documents/commandSummary/


//tar cf - . 对当前目录中的文件创建一个新的存档,输出到标准输出。
//cd DIR 更改了目录。请注意,这个目录在进行文件复制之前应该已经存在。
//tar xf - 从标准输入中提取文件。
//-p:选项确保保持相应的权限和所有权
//通过使用圆括号将上面的两个部分括起来,可以有效地将它们作为一个而不是两个命令来处理,并且 cd 命令在提取存档之前进行。
//两者之间的管道 (|) 将第一个 tar 的标准输出传入到第二个 tar 的标准输入,并且高效地将文件复制到一个并不存在的存档文件,然后再从其中提取文件。

注:与 cp 相比,tar 命令有一个优点,那就是通过添加 v 命令行选项以打开详细模式,
您可以在将文件从源复制到目标的过程中'监视文件的传输'。通常,最好是在'提取文件的 tar 命令中使用这个选项',
而不是在创建存档的 tar 命令中使用,因为它可以'确保'正确地对文件进行'复制',而不是证实对其进行了正确的读取



'c'---cpio命令(cpio需要被处理文件的列表,通常用'find -depth':print of the entries in a directory before printing the dir itself.

/*只有root在执行该命令时才能保存归档文件原来的 ownership*/

注:By default,cpio creates binary format archives, for compatibility with older cpio programs.
默认,cpio 创建的归档是 'binary' 格式。

'三种mode'

-------'Copy-out mode':cpio copies files 'into' an archive. //-o

-------'Copy-in mode':cpio copies files 'out of' an archive or lists the archive contents. // -i

-------'Copy-pass mode':cpio copies files from one directory tree to another, //-p
combining the copy-out and copy-in steps 'without actually using an archive'.




1.ls | cpio -ov > directory.cpio //ls 只能列出当前目录下的文件,没有递归 【'通常不用ls'】
//The `>' redirects the cpio output to the file`directory.cpio'.

cpio -iv < directory.cpio //将在当前目录从 dir.cpio档案中 释放文件


2.find . -print -depth | cpio -ov > tree.cpio /find归档'整个目录树'(archive an entire directory tree)
//后缀必须是 'cpio'

cpio -idv < tree.cpio //释放整个‘目录树’;
//-d, --make-directories' Create leading directories where needed.
(如果没有-d,则无法释放出文件树下子目录中的文件)


/**/3.find . -depth -print0 | cpio --null -pvdm new-dir

//-p:tells cpio to pass the files it finds to the directory `new-dir'(pass模式)
//-v: List the files processed,
//-d: creat directories where needed
//-m: Retain previous file modification times when creating files.(保持文件的修改/创建时间)

//-print0
True; print the full file name on the standard output, followed
by a null character (instead of the newline character that
-print uses). This allows file names that contain newlines or
other types of white space to be correctly interpreted by pro‐
grams that process the find output. This option corresponds to
the -0 option of xargs.

//cpio的--null,-0与find的 -print0原理一样:二者一同工作,确保文件名含有 各种white space的文件能正确 interpreted

-----例子'a'

[root@localhost t]# touch 'I //创建文件名含有\n的文件'I?Love?YOU'
> Love
> YOU'
[root@localhost t]# ls
a1 archive.tar b1 conf I?Love?YOU new file txt //文件‘new file’含有tab space(可以被正确解析)

[root@localhost t]# find . -depth -print | cpio -pvdm /temp //复制文件树
cpio: ./I: Cannot stat: No such file or directory //报错---找不到‘I’、‘Love’、‘You'这三个文件
cpio: Love: Cannot stat: No such file or directory
cpio: You: Cannot stat: No such file or directory





4.find . | cpio -pdvm new-dir //简化版

5.find . -depth | cpio -ov |(cd new-dir; cpio -idv)




'd'---通过网络直接复制

注:将典型的 tar 或 cpio 命令的输出通过管道传递到远程 Shell 中,
如远程 Shell (rsh) 或安全 Shell (ssh),您可以直接通过网络进行复制。


1.tar czf - . | ssh Jon@rhel tar xzf - -C /remotedir //ssh以Jon用户登录rhel主机(需要身份验证&&加密)

4.tar czf - . | rsh Jon@rhel tar xzf - -C /remotedir //rsh...............................不加密


2.rcp -r localdir remotehost:/remotedi //使用 rcp 复制远程系统中的文件
//-r 命令行选项,这样可以对目录进行递归地复制。

3.scp -r filename remotehost:/remotedir //scp ???


注:要实现更健壮的同步操作,您可以使用 'rsync' 工具,它是一种免费的软件实用工具,可以高效地通过网络交换文件。
对于复制和同步文件,尤其是在较慢的连接中,rsync 工具可能是一种有效的方法。
我在IBM工作,可以为大家内部推荐IBM各种职位 IBM全球职位尽在以下链接(请在浏览器中打开,QQ/微信 会阻止): http://ibmreferrals.com/ 很乐意为感兴趣的小伙伴分享:我的面试经验^_^ 如需咨询,请邮件发送以下邮箱,有问必回 1026096425@qq.com
原文地址:https://www.cnblogs.com/jackydalong/p/2408787.html