您的位置:首页 > 理论基础 > 计算机网络

Linux 网络配置的二十个有用的命令

2013-08-25 19:41 309 查看

对中级 Linux 用户非常有用的 20 个命令

也许你已经发现第一篇文章非常的有用,这篇文章是继对初级Linux用户非常有用的20个命令 的一个延伸。 第一篇文章的目的是为新手准备的而这篇文章则是为了Linux的中高级用户。在这里你将学会如何进行自定义搜索,知道正在进行的进程和停掉进程,如何使用Linux的强势功能和如何在系统内编译C,C++和JAVA程序。

21. 命令: Find

搜索指定目录下的文件,从开始于父目录,然后搜索子目录。

-name&lsquo]root@tecmint:~# find -iname *.SH ( find -iname *.Sh /  find -iname *.sH)

./Desktop/load.sh
./Desktop/test.sh
./Desktop/shutdown.sh
./Binary/firefox/run-mozilla.sh
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh
./Downloads/kdewebdev-3.5.8/admin/cvs.sh
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh
./Downloads/wheezy-nv-install.sh

35 Find Command Examples in Linux

22. 命令: grep

grep‘命令搜索指定文件中包含给定字符串或者单词的行。举例搜索‘/etc/passwd‘文件中的‘tecmint'
root@tecmint:~# grep tecmint /etc/passwd

tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash

使用’-i'选项将忽略大小写。

使用&rsquo]root@tecmint:~# grep -r "127.0.0.1" /etc/

/etc/vlc/lua/http/.hosts:127.0.0.1
/etc/speech-dispatcher/modules/ivona.conf:#IvonaServerHost "127.0.0.1"
/etc/mysql/my.cnf:bind-address		= 127.0.0.1
/etc/apache2/mods-available/status.conf:    Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/ldap.conf:    Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/info.conf:    Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/proxy_balancer.conf:#    Allow from 127.0.0.1 ::1
/etc/security/access.conf:#+ : root : 127.0.0.1
/etc/dhcp/dhclient.conf:#prepend domain-name-servers 127.0.0.1;
/etc/dhcp/dhclient.conf:#  option domain-name-servers 127.0.0.1;
/etc/init/network-interface.conf:	ifconfig lo 127.0.0.1 up || true
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# http.nonProxyHosts=localhost|127.0.0.1
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# ftp.nonProxyHosts=localhost|127.0.0.1
/etc/hosts:127.0.0.1	localhost

注意:您还可以使用以下选项:

-w 搜索单词 (egrep -w ‘word1|word2‘ /path/to/file).

-c 用于统计满足要求的行 (i.e., total number of times the pattern matched) (grep -c ‘word‘ /path/to/file).

–color 彩色输出 (grep –color server /etc/passwd).

23. 命令: man

man‘是系统帮助页。Man提供命令所有选项及用法的在线文档。几乎所有的命令都有它们的帮助页,例如:

  [-d]  [-D]  [--warnings[=warnings]]  [-R  encoding]  [-L  locale]  [-m  system[,...]]  [-M  path]  [-S list] [-e extension] [-i|-I]
[--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager] [-r prompt] [-7] [-E encoding] [--no-hyphenation] [--no-justification]  [-p
string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
man -k [apropos options] regexp ...
man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
man -f [whatis options] page ...
man -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p string] [-t] [-T[device]]
[-H[browser]] [-X[dpi]] [-Z] file ...
man -w|-W [-C file] [-d] [-D] page ...
man -c [-C file] [-d] [-D] page ...
man [-hV]
上面是man命令的系统帮助页,类似的有cat和ls的帮助页。

注意:系统帮助页是为了命令的使用和学习而设计的。

24. 命令: ps

ps命令给出正在运行的某个进程的状态,每个进程有特定的id成为PID。 [code]root@tecmint:~# ps PID TTY TIME CMD 4170 pts/1 00:00:00 bash 9628 pts/1 00:00:00 ps

使用&lsquo]root@tecmint:~# ps -A

PID TTY TIME CMD
1 ? 00:00:01 init
2 ? 00:00:00 kthreadd
3 ? 00:00:01 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
7 ? 00:00:00 kworker/u:0H
8 ? 00:00:00 migration/0
9 ? 00:00:00 rcu_bh
....

注意:当你要知道有哪些进程在运行或者需要知道想杀死的进程PID时ps命令很管用。你可以把它与‘[b]grep‘合用来查询指定的输出结果,例如:
[/b]

root@tecmint:~# ps -A | grep -i apache2

1285 ?        00:00:00 apache2

搜索‘apache2'进程,找到PID并杀掉它.例如:在本例中‘apache2'进程的PID是1285..

root@tecmint:~# pkill apache2

注意:kill需要PID作为参数,pkill可以选择应用的方式,比如指定进程的所有者等.


26. 命令: whereis

whereis的作用是用来定位命令的二进制文件\资源\或者帮助页.举例来说,获得ls和kill命令的二进制文件/资源以及帮助页:

root@tecmint:~# whereis ls

ls: /bin/ls /usr/share/man/man1/ls.1.gz

root@tecmint:~# whereis kill

kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz

注意:当需要知道二进制文件保存位置时有用.

27. 命令: service

&lsquo]root@tecmint:~# service apache2 start

* Starting web server apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
httpd (pid 1285) already running [ OK ][/code]

重启apache2 server:

停止apache2 server:

root@tecmint:~# service apache2 stop

* Stopping web server apache2                                                                                                                                 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
... waiting                                                           		[ OK ]

注意:要想使用service命令,进程的脚本必须放在&lsquo]root@tecmint:~# alias l='ls -l'

试试它是否能用:

root@tecmint:~# unalias l

再试试:

alias cd='ls -l' (set alias of ls -l to cd)
alias su='pwd' (set alias of pwd to su)
....
(You can create your own)
....

想想多么有趣,现在如果你的朋友敲入&lsquo]root@tecmint:~# df

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 47929224 7811908 37675948 18% /
none 4 0 4 0% /sys/fs/cgroup
udev 1005916 4 1005912 1% /dev
tmpfs 202824 816 202008 1% /run
none 5120 0 5120 0% /run/lock
none 1014120 628 1013492 1% /run/shm
none 102400 44 102356 1% /run/user
/dev/sda5 184307 79852 94727 46% /boot
/dev/sda7 95989516 61104 91045676 1% /data
/dev/sda8 91953192 57032 87218528 1% /personal[/code]
‘[b]df
’命令的更多例子请参阅 12 df Command Examples in Linux.

30. 命令: du

估计文件的空间占用。 逐层统计文件(例如以递归方式)并输出摘要。

10 du (Disk Usage) Commands.

31. 命令: rm

'rm' 标准移除命令。 rm 可以用来删除文件和目录。

删除目录

root@tecmint:~# rm PassportApplicationForm_Main_English_V1.0

rm: cannot remove `PassportApplicationForm_Main_English_V1.0': Is a directory

'rm' 不能直接删除目录,需要加上相应的'-rf'参数才可以。

警告: "rm -rf" 命令是一个破坏性的命令,假如你不小心删除一个错误的目录。一旦你使用'rm -rf' 删除一个目录,在目录中所有的文件包括目录本身会被永久的删除,所以使用这个命令要非常小心。

32. 命令: echo

echo ]root@tecmint:~# echo "Tecmint.com is a very good website" Tecmint.com is a very good website

创建一小段交互式脚本
1. 在桌面上新建一个文件,命名为 ‘interactive_shell.sh‘ (记住必须带 ‘.sh‘扩展名)。
2. 复制粘贴如下脚本代码,确保和下面的一致。

root@tecmint:~# chmod 777 interactive_shell.sh

[code]root@tecmint:~# ./interactive_shell.sh

Please enter your name:
Ravi Saive
Welcome to Linux Ravi Saive

注意: &lsquo]root@tecmint:~# passwd

Changing password for tecmint.
(current) UNIX password: ********
Enter new UNIX password: ********
Retype new UNIX password: ********
Password unchanged [这里表示密码未改变,例如:新密码=旧密码]
Enter new UNIX password: #####
Retype new UNIX password:#####[/code]

34. 命令: lpr

这个命令用来在命令行上将指定的文件在指定的打印机上打印。

root@tecmint:~# cat file1.txt

Hi My name is Tecmint

file2.txt
 com
比较一下这两个文件,看看命令的输出。

root@tecmint:~# cmp file1.txt file2.txt

file1.txt file2.txt differ: byte 15, line 1

36. 命令: wget

Wget是用于非交互式(例如后台)下载文件的免费工具.支持HTTP, HTTPS, FTP协议和 HTTP 代理。

使用wget下载ffmpeg

[code]root@tecmint:~# wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2
--2013-05-22 18:54:52-- http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59
Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 [following]
--2013-05-22 18:54:54-- http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 Resolving kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)... 92.46.53.163
Connecting to kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)|92.46.53.163|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 275557 (269K) [application/octet-stream]
Saving to: &lsquo] 是一个很重要的命令,用来挂载不能自动挂载的文件系统。你需要root权限挂载设备。

在插入你的文件系统后,首先运行"lsblk"命令,识别出你的设备,然后把分配的设备名记下来。

root@tecmint:~# lsblk

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 931.5G  0 disk
├─sda1   8:1    0 923.6G  0 part /
├─sda2   8:2    0     1K  0 part
└─sda5   8:5    0   7.9G  0 part [SWAP]
sr0     11:0    1  1024M  0 rom
sdb      8:16   1   3.7G  0 disk
└─sdb1   8:17   1   3.7G  0 part

从这个输出上来看,很明显我插入的是4GB的U盘,因而“sdb1”就是要挂载上来的文件系统。以root用户操作,然后切换到/dev目录,它是所有文件系统挂载的地方。

[code]root@tecmint:~# cd /dev

创建一个任何名字的目录,但是最好和引用相关。

[code]root@tecmint:~# mkdir usb

现在将&ldquo]root@tecmint:~# mount /dev/sdb1 /dev/usb[/code]
现在你就可以从终端进入到/dev/usb或者通过X窗口系统从挂载目录访问文件。

是时候让程序猿见识见识Linux环境是多么丰富了!

38. 命令: gcc

gcc 是Linux环境下C语言的内建编译器。下面是一个简单的C程序,在桌面上保存为Hello.c (记住必须要有‘.c‘扩展名)。

编译
root@tecmint:~# gcc Hello.c

运行
注意: 编译C程序时,输出会自动保存到一个名为&ldquo]root@tecmint:~# gcc -o Hello Hello.c

这里‘-o‘将输出写到‘Hello‘文件而不是 ‘a.out‘。再运行一次。

39. 命令: g++

g++C++的内建编译器。下面是一个简单的C++程序,在桌面上保存为Add.cpp (记住必须要有&lsquo]#include <iostream> using namespace std; int main() { int a; int b; cout<<"Enter first number:\n"; cin >> a; cout <<"Enter the second number:\n"; cin>> b; cin.ignore(); int result = a + b; cout<<"Result is"<<" "<<result<<endl; cin.get(); return 0; }

编译
root@tecmint:~# ./a.out

Enter first number:
...
...

注意:编译C++程序时,输出会自动保存到一个名为&ldquo]root@tecmint:~# g++ -o Add Add.cpp

运行
class tecmint {
public static void main(String[] arguments) {
System.out.println("Tecmint ");
}
}

用javac编译tecmint.java
root@tecmint:~# javac tecmint.java[/code]
运行
root@tecmint:~# java tecmint

注意: 几乎所有的Linux发行版都带有gcc编译器, 大多数发行版都内建了g++ 和 java 编译器, 有些也可能没有. 你可以用aptyum 安装需要的包.

请留下您宝贵的意见和想看到的文章类型. 稍后, 我会打来一个很有趣主题, 关于Linux的一些鲜为人知的知识.

本文地址:http://www.oschina.net/translate/20-advanced-commands-for-middle-level-linux-users

原文地址:http://www.tecmint.com/20-advanced-commands-for-middle-level-linux-users/

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: