1 / 64

Linux illustration

Linux illustration. content. why we use linux Basic concept for linux newbie Useful commands in linux System configuration howto How to work efficiently Programming under linux Remote operation howto About our work. Why we use linux. Linux is more efficient Linux is more stable

elise
Download Presentation

Linux illustration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux illustration

  2. content • why we use linux • Basic concept for linux newbie • Useful commands in linux • System configuration howto • How to work efficiently • Programming under linux • Remote operation howto • About our work

  3. Why we use linux • Linux is more efficient • Linux is more stable • For compatibility • Linux is free • Linux is more flexible

  4. 新手上路之基本概念 • 预备知识 • 文件系统 • 进程 • Linux环境

  5. 预备知识 • 登陆到linux • 从linux注销 • 控制台和视窗环境 • 重起活着关闭linux • Linux中最有用的命令

  6. 登陆到linux • 字符界面登陆,蓝色字符表示输入 Red Hat Linux release 9 (Shrike) Kernel 2.4.20-mosix on an i686 login: dhh Password:这里输入密码,没有回显 Last login: Tue Apr 6 18:05:38 from 10.14.122.253 You have new mail. [dhh@d6 dhh]$ 最后一行是命令提示符号,表示进入了shell.

  7. 登陆到linux • 图形界面登陆 这种登陆界面和windows的登陆界面类似。所以这里就不详细介绍了。 至于用那种模式登陆取决于文件 /etc/inittab. 在这个文件中有一行包含字符:“id:3:initdefault:”,其中的数字3指定了登陆的模式为字符界面的。如果把3改成5就是图形界面登陆模式。具体可以参照man inittab。

  8. 从linux注销 • [dhh@d6 dhh]$ exit • [dhh@d6 dhh]$ Ctrl-d 输入exit按回车或者直接按Ctrl和d的复合键就可以从linux中退出。如果在图形界面的情况下面有菜单可以选择退出或者关机。

  9. 控制台和视窗环境 • [dhh@d6 dhh]$ 当你在字符模式下面登陆到linux的时候,你处于控制台(你处于一个正在运行的shell中),你可以通过Ctrl和Fn键来切换控制台,这里的n指的是数字,譬如说F1,F2等等。一般来说,linux默认有6个控制台可以切换。如果你愿意的话,你可以把它添加到上百个。上面的字符就是linux的shell的提示符号的一种。 • 进入视窗环境 [dhh@d6 dhh]$ startx startx是进入视窗环境的一个命令

  10. 重起或者关闭linux • shutdown • init • halt • reboot 这里的有些命令默认只能由root来执行。比较常用的是shutdown,halt和reboot

  11. Linux中最有用的命令 • man – 按格式打印出linux的在线文档 命令举例: man man man ls 按 q来退出man。 下面就是man g77的一段: G77(1) GNU G77(1) NAME g77 - GNU project Fortran 77 compiler SYNOPSIS

  12. 文件系统 文件系统的结构是树状的 [dhh@d6 dhh]$ tree bin bin |-- flitter |-- rpm2targz | |-- README | |-- rpm2targz | `-- rpmoffset.c `-- tmp 1 directory, 6 files [dhh@d6 dhh]$ 和windows不一样的是,linux只有一个根(root) 或者说/

  13. 文件系统 • mount – 挂载一个文件系统 [dhh@d6 dhh]$ cat /etc/fstab LABEL=/ / ext3 defaults 1 1 none /dev/pts devpts gid=5,mode=620 0 0 LABEL=/home /home ext3 defaults 1 2 none /proc proc defaults 0 0 none /dev/shm tmpfs defaults 0 0 LABEL=/usr /usr ext3 defaults 1 2 LABEL=/var /var ext3 defaults 1 2 /dev/hda5 swap swap defaults 0 0 /dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0 none /mfs mfs dfsa=1 0 0 [dhh@d6 dhh]$ Fstab里面写的是系统启动的时候挂载的分区列表。 尝试用命令 fdisk –l来察看磁盘的分区结构.

  14. 文件系统 • ls – 列出文件或者目录 命令举例: ls -lh *.html ls -ltr • cp – 拷贝文件或者目录 命令举例: cp file1.c ~/tmp/file2.c • mv – 移动或者重命名文件 命令举例: mv ising.c ising.c.old mv ising.c ~/backup/

  15. 文件系统 • rm – 移除/删除文件 命令举例: rm -rf ~/bin 命令 rmdir 只删除空目录 • pwd – 打印工作目录(当前目录) • cd – 改变当前目录到其他目录 命令举例: cd .. cd /home cd – • chgrp – 改变文件或者目录的属组 命令举例: chgrp users filename 这样就把filename这个文件改成属于users这个组。

  16. 文件系统 • chown – 改变文件的属主 命令举例: chown -R dhh.users bin/ • chmod – 改变文件的存取权限 举例: 当你用ls –l来察看文件的时候,会看到类似情况:  -rwxr-x--x 表示文件是一个普通文件。文件的主人可以读(r),写(w) 和执行(x)文件。在同一个组的用户可以读可执行文件,而其他的用户只有执行文件的权限。 试试 chmod 600 filename ; chmod +x filename 看看文件的权限在命令前后有什么变化? 试试 chmod –x filename,然后去执行这个文件?

  17. 文件系统 • 控制台下面察看文件的命令 more – 分屏显示文件,翻页用f(forward),b(backward)或者空格,可以按回车单向逐行翻动显示内容。 cat – 连接文件并且在标准输出输出文件内容,普通用法和more类似,但是不会分屏显示,而是一下子全部输出。 命令举例: cat file cat file1 file2 less – 分屏显示文件,可以用方向键逐行翻动文件 你可以按q来退出more或者less的察看文件的状态。按键q可以使得你能在linux的很多暂停状态下面退出来,譬如在运行命令top的时候。

  18. 进程 [dhh@z3 dhh]$ date;who Tue Apr 6 15:55:57 CST 2004 dhh pts/0 Apr 6 15:0 (10.14.122.253) [dhh@z3 dhh]$ 当你运行程序的时候,那就是说,linux里面有进程在运行。进程是linux运行的程序单位。 你可以给正在运行的进程发送信号。如果你在shell的前台运行程序,你可以按Ctrl-c来向当前进程发送SIGINT这样的信号。默认的对信号SIGINT的处理方式是终止进程。 问题: date && who和上面的data;who有什么区别?

  19. 进程 举例: [dhh@z3 dhh]$ ./ising Ctrl-c [dhh@z3 dhh]$ 进程ising也许需要运行10天才能正常结束。这里的Ctrl-c中断了进程的继续运行。 每个运行中的进程有唯一的一个整数和它对应,我们称呼 这个整数为进程id,往往用pid作为缩写。 你可以用命令ps或者top来找到进程的进程id。 尝试用Ctrl-c来终止一个进程?

  20. 进程 • top – 显示最占用cpu/内存等等的程序 • ps – 报告进程状态的命令 命令举例: ps -u dhh ps –aux 尝试在ps的输出项中找到pid项。 • nohup – 运行一个程序,忽略挂起信号,同时使得输出为非终端。往往用在需要长期运算的程序。 • nice – 运行一个进程优先级别经过调整的程序。 • at – 调整或设定计划任务(单次执行) 命令举例:at now+5min < file

  21. 进程 • 在后台运行进程 [dhh@z3 dhh]$ nohup nice ./ising > /dev/null & [1] 4931 [dhh@z3 dhh]$ jobs [1]+ Running nohup nice ./ising >/dev/null & [dhh@z3 dhh]$ fg %1 nohup nice ./ising >/dev/null Ctrl-c [dhh@z3 dhh]$ jobs [dhh@z3 dhh]$

  22. 进程 • 在后台运行进程 [dhh@z3 dhh]$ ./ising Ctrl-z [1]+ Stopped ./ising [dhh@z3 dhh]$ bg [1]+ ./ising & [dhh@z3 dhh]$ 这里的Ctrl-z向进程发送了信号SIGSTP。命令 bg把进程放到了后台,相反的命令是 fg. 命令jobs列出了被放到后台的可以由shell控制的进程列表。

  23. 进程 • kill – 终止/杀死进程 事实上,kill只是向进程发送信号。默认的发送给进程的信号是SIGTERM。这个信号往往会让进程体面的终止它自己。 用法举例: [dhh@z3 dhh]$ ps -u dhh PID TTY TIME CMD 4635 pts/0 00:00:00 bash 5026 pts/0 00:00:00 ps [dhh@z3 dhh]$ kill –s SIGSTP 4635 如果你想让操作系统,而不是进程来终止进程,你可以这样运行命令:kill -9 4635。这个命令并不是发送信号。没有9这样的信号。

  24. 进程 关于信号的一个有趣的练习 [dhh@z3 dhh]$ more trap.sh #!/bin/sh trap 'echo sigint catched' 2 trap 'echo oh! My god! someone want kill me!' 15 read in echo "$in is inputed" [dhh@z3 dhh]$ 运行命令chmod +x trap.sh. 运行这个脚本,按 Ctrl-c或者尝试 kill 来杀死进程,看看发生了什么?

  25. Linux环境 • 新手上路-怎样执行一个程序? 通过绝对路径来执行命令: /sbin/ifconfig 通过一个相对路径来执行命令: cd /sbin ./ifconfig

  26. Linux环境 • Shell是怎样找到命令的? [dhh@z3 dhh]$ echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/dhh/bin [dhh@z3 dhh]$ 这里的 PATH是一个系统的环境变量。

  27. Linux环境 • 管道和I/O重定向 who | wc -l “|” 这里就是一个管道。通过管道的连接命令“who”的输出就作为了命令“wc -l”的输入。 ls -l > ls.out “>” 是 I/O 重定向指示符。可以察看文件ls.out来获得输出内容。 比较who | sort 和who > sort之间的区别。

  28. Linux环境 • 管道和I/O重定向 在一般的环境下面,每个进程在启动的时候有三个文件描述符和它关联,其中之一是对应标准输入(3),其中之一对应标准输出(1),另外一个用来打印诊断和错误信息,那就是标准错误 (2)。 f77 –o ising ising.f90 2>&1 | more f77 -o ising ising.f 2 > err.log

  29. Linux中有用的命令 • grep –打印匹配的内容 用法举例: [dhh@d6 dhh]$ who econo3 tty1 Apr 6 23:27 dhh pts/1 Apr 7 01:41 (10.14.122.253) econo3 pts/0 Apr 6 23:31 (:0.0) [dhh@d6 dhh]$ who | grep dhh dhh pts/1 Apr 7 01:41 (10.14.122.253) [dhh@d6 dhh]$ who | grep -v dhh econo3 tty1 Apr 6 23:27 econo3 pts/0 Apr 6 23:31 (:0.0) [dhh@d6 dhh]$ who | grep -v "eco*" dhh pts/1 Apr 7 01:41 (10.14.122.253) [dhh@d6 dhh]$

  30. Linux中有用的命令 • find – 在指定的目录中找查找文件 用法举例: find . -name “*.mp3” -exec rm {} \; • locate – 在一个updatedb产生的数据库里面查找文件 • who – 显示谁在线 • which – 显示被使用的命令的全路径 注:可能多个相同命名的程序,可以找到当前被执行的是哪个。 • whereis – 定位一个命令的可执行文件,源码和手册的路径 用法举例: • [dhh@d6 dhh]$ whereis ls • ls: /bin/ls /usr/share/man/man1/ls.1.gz • [dhh@d6 dhh]$

  31. Linux中有用的命令 • su – 运行一个具有不同用户id和组id的shell,或者说切换用户。 用法举例: su - dhh • tar – GNU版本的打包工具 用法举例: tar jxvf file.tar.bz2 tar zxvf file.tar.gz tar jcvf file.tar.bz2 ./file/ tar zcf file.gz ./file/* • gzip – 压缩或者解压缩文件(zip) • bzip2 – 一个块压缩/解压缩工具(bzip2)

  32. Linux中有用的命令 • passwd – 更新/修改用户的密码 • adduser – 新建用户 用法举例: adduser dhh -g users • file – 判断文件类型的命令 • tail – 输出文件的最后部分 • head – 输出文件的最前面部分 • wc – 打印出文件的大小,单词个数或者行数。 用法举例: wc -l result.dat 用现有的命令怎么打印出文件的第一行?

  33. Linux中有用的命令 • sed和 awk 用法举例:more file | sed s/abc/def/g cat file | sed –n 5p sed s&<[^>]*>&&g file awk ‘{print $5}’ file 我确实看到过有一本很厚的书仅仅用来讲 sed和 awk的用法。命令sed和 awk的威力是远远超出你的想像能力的。

  34. 系统配置howto • ftp服务 /etc/init.d/vsftpd start/stop/restart • telnet 服务 (不安全) 修改 /etc/xinetd.d/telnet, 修改 disable = yes为 disable = no. 然后运行 /etc/init.d/xinetd restart. • ssh 服务 (推荐) /etc/init.d/sshd start/stop/restart 在 ssh中传输的数据是加密过的,所以它比较安全

  35. 系统配置howto • ftp客户端 在linux下面: gftp, lftp, ncftp, ftp 在windows下面: leapftp等等 • telnet 客户端 Linux下面:gnome-terminal->telnet Windows下面 telnet, putty,fterm (推荐) • ssh client Linux下面 ssh, scp, sftp Windows下面 sshwinclient, putty,fterm 其中 sshwinclient支持 sftp文件传输

  36. 系统配置howto • lftp的一个例子 [dhh@hhding dhh]$ lftp 10.14.122.166 -u dhh Password: lftp dhh@10.14.122.166:~> ls drwxrwxr-x 3 505 100 4096 Mar 30 11:29 bin drwxr-xr-x 7 505 100 4096 Apr 04 21:19 dl drwxrwxr-x 6 505 100 4096 Apr 03 07:31 doc drwxr-xr-x 2 505 100 4096 Apr 06 19:25 src drwxr-xr-x 3 505 100 4096 Apr 08 16:30 tmp drwxr-xr-x 4 505 100 4096 Apr 06 11:27 work lftp dhh@10.14.122.166:~> bye [dhh@hhding dhh]$

  37. 系统配置howto lftp里面的一些命令 get – 从服务器取得单个文件 mget – 从服务器取得多个文件(支持通配符) put – 上传文件到服务器 mput – 上传多个文件到服务器 binary – 设置传输模式为二进制(这个命令可以简写为bin) lcd – 改变上传的本地目录 ! – 在不退出ftp状态的情况下切换到一个shell环境中 open – 连接指定端口的远程主机 user –指定用户名 bye – 断开连接 几乎所有的基于字符界面的ftp客户端都支持以上命令。 mirror && mirror –R queue – 把一个任务放入队列 我们可以象在shell中那样按 Ctrl-z 把任务放到后台运行。 lftp支持 TAB 自动补全,我们将会在稍后介绍。

  38. 系统配置howto • 和ssh有关的几个命令 ssh -l dhh 10.14.122.166 ssh -l -X dhh@10.14.122.166 最后的那个命令使得X forward生效。 Public key (公共密匙?)验证 第一步: ssh-keygen -t dsa 第二步: scp ~/.ssh/id_dsa.pub target_host:~/.ssh/authorized_keys 这里的target_host应该替换成为远程主机的实际的机器名或者ip。

  39. 系统配置howto • 软件安装howto 在linux世界里面,往往存在两大类安装方式的软件,其中一种是源代码安装方式,另外一种是二进制的文件安装方式。所以我们应该用不同的方式处理这两种软件。

  40. 系统配置howto • 源代码方式 一般来说每个安装软件都有安装文档,他们们可能是 README或者 INSTALL,里面一般都有详细的安装过程介绍。最普通的安装软件方式,也是比较通用的安装软件的步骤是: ./configure make make install 以上命令应该在源代码所在目录运行。

  41. 系统配置howto • 二进制软件 我们常用的redhat公司发布的linux,常见的的二进制软件包是rpm包。 我们可以在Xwindows的gnome窗口环境下通过双击rpm包来安装rpm包。如果在命令行的情况下面,可以通过运行命令 rpm -ivh file.rpm来安装软件,可以通过运行 rpm -e file来卸载软件。 具体信息可以通过察看 rpm的手册来获得。

  42. 系统配置howto • ifup && ifdown – 激活或者禁用网络设备 • ifconfig – 配置网络设备 应用举例: /sbin/ifconfig eth0 10.14.122.167 /sbin/ifconfig eth0 什么文件控制了网络配置信息? 看看文件 /etc/sysconfig/network-scripts/ifcfg-eth0的内容或者看在下面的目录中包含的文件: /etc/sysconfig/networking/profiles/default/

  43. 系统配置howto • uname – 打印出系统基本信息 • free – 显示被使用和空闲的内存 • df - 显示文件系统的磁盘使用情况 • du – 估计文件占用磁盘空间情况 用法举例: du -sh * • vmstat – 显示虚拟内存统计情况

  44. 系统配置howto • 存取外部设备 我们怎样存取软驱,光驱或者u盘? /dev/cdrom /mnt/cdrom udf,iso9660 noauto,owner,kudzu,ro 0 0 /dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0 前面两行是从文件/etc/fstab拷贝过来的, 他们指定了挂载光驱和软驱的选项,通过这些选项,每个用户都可以存取这两个设备。 挂载U盘的命令: mount -t vfat /dev/sda1 /the/mount/point/ mount的几个常用用法 挂载软驱 mount -t ext3 /dev/fd0 /mnt/floppy 挂载光驱 mount -t iso9660 -o loop /dev/cdrom /mnt/cdrom 挂载iso文件 mount -t iso9660 -o loop /dir/file.iso /target/directory

  45. 高效工作的技巧 • Shell脚本 • 无法归类的技巧 • 定制你的工作环境

  46. Shell脚本 • 假设在当前目录下面有10000个压缩文件,假设文件类似 0001.tar.bz2,0002.tar.bz2,你怎样才能把所有的这些文件解压缩?输入下面的命令: [dhh@d6 dhh]$ for fn in *.tar.bz2 > do tar jxf $fn > done [dhh@d6 dhh]$ • Ok, 你会发现所有的文件都被解压缩了。这只不过是shell的一个小技巧而已。如果你愿意的话,你可以把这些命令写到文件里面,那么这个文件就叫做shell脚本了。

  47. Shell 脚本 • 我的一个简单的shell脚本 [dhh@d6 bin]$ more gg #!/bin/sh infile=$1 /usr/bin/gcc -ggdb -o ${infile%.c} $infile -lm [dhh@d6 src]$ ls ising.c ising.c [dhh@d6 src]$ gg ising.c ising.c: In function `main': ising.c:27: warning: return type of `main' is not `int' [dhh@d6 src]$ 这样你就不用输入很长的gcc –ggdb –o ising ising.c –lm了

  48. 无法归类的技巧 • 关于 TAB 这里有一个文件名很长的文件:“who am I, I am dhh, do you know me, who are you”, oh, my god! 这确实是一个文件名!我想察看文件内容,那么我应该输入所有的这些文字吗? [dhh@d6 test]$ ls current ising_Metropolis_current.c who am I, I am dhh, do you know me , who are you [dhh@d6 test]$ more who\ am\ I\,\ I\ am\ dhh\,\ do\ you\ know\ me , who\ are\ you 我所输入的是:more w<TAB>,然后就可以了。Is it great?

  49. 无法归类的技巧 • alias 我在~/.bashrc里面的部分alias: alias cx='chmod u+x' alias c=clear alias vv='konqueror &' alias mm='mozilla &' alias 88='lftp 10.13.22.88 -p 52088‘ 不错, 命令 alias是命令的缩写,如果你运用这个技巧的话,可以节省很多时间。如果你不再需要某些alias,你可以用命令 unalias来使得这些alias失效。

  50. 无法归类的技巧 • alias的几个例子 • 在fortran的编程中,我们经常做一些烦琐的事情。举例 rm输出文件, 编译fortran文件, 而且这种过程要重复非常多次。让我们采取一些技巧来简化这些问题。 • 假设我们需要编译的文件是test.f, the binary file test will output 2 files named ising.dat and sum.dat. • Ok, 输入以下命令: alias r=“rm ising.dat sum.dat”;alias f=“f77 –o test test.f”; alias e=“./test &”;alias p=“r;f”;这样,你就可以节省很多时间了。

More Related