Linux 基础

命令说明

$ type cmd # 获取命令类型
$ which cmd # 命令的位置
$ help cmd / cmd --help / man cmd # 获取命令帮助
$ whatis cmd # 命令描述信息

Linux – filesystem path notation

Notation

Desc

/

the root directory

.

the current working directory

..

the parent directory (i.e. one directory up)

~

home directory

~wasadmin

wasadmin's home directory

-

return to the last visited directory

Linux - user & group

 

文件

文件内容示例

含义

用户

/etc/passwd

root:x:0:0:root:/root:/bin/bash

mysql:x:502:502::/home/mysql:/bin/bash

用户名:密码:UID:GID:用户全名:home目录:shell

1-499: 系统用户; 500-:普通用户

用户组

/etc/group

root:x:0:

mail:x:12:mail,postfix

组名:用户组密码:GID:用户组内的用户名

用户密码

/etc/shadow

-

-

用户组密码

/etc/gshadow

-

-

sudoer

/etc/sudoers

root    ALL=(ALL)       ALL

%wheel ALL=NOPASSWD: ALL

提权配置文件

Linux - file permission model

lrwx-xr-x

说明:l为目录、文件、软硬连接信息,示例为软连接

rwx为当前用户权限,这里表示有读写执行权限

xr为用户组权限

x为其他人的权限

Linux – file link

为解决文件的共享使用,引入了两种链接:硬链接 (hard link) 与软链接(又称符号链接,即 soft link 或 symbolic link)。

硬链接有以下特性(或限制),所以软连接比硬链接应用广泛:

l 文件有相同的 inode 及 data block;

l 只能对已存在的文件进行创建;

l 不能交叉文件系统进行硬链接的创建;

l 不能对目录进行创建,只可对文件创建;

l 删除一个硬链接文件不影响其它相同 inode 号的文件。

软连实际用法:

l 用作 web 集群的 www 目录,软连接至nas目录

l 用作解释性脚本语言应用程序的多版本发布

l 用作应用服务器集群 log 目录

l …

示例:

ln -s /data01/logs /tctHome/tomcat_xx/logs
把tomcat_xx下的的logs目录指向data01的logs目录

Linux install software

rpm:
查已安装:rpm -qa | grep java
安装:rpm -ivh xxx.rpm
卸载:rpm -e xxx

Yum:
查找:yum list xxx
安装:yum install xxx
更新:yum update xxx
卸载:yum remove xxx

Build from src
wget http//somewhere/something.tar.gz
tar xvf something.tar.gz
cd something
./configure --prefix ~/mysoftware
Make && make install

Linux cmd keyboard tricks

Linux cmd 需要大量敲键盘,如何更快、更高效的敲?

移动光标
往左:左箭头;最左:CTRL+A
往后:右箭头;最右:CTR + E

自动补齐:TAB / ESC+. 上次命令的参数

查找历史:
上1条:上箭头; 下1条:下箭头;

$history or $history | grep keyword
!foo 执行以 foo 开头的历史命令
!?foo 执行包含 foo 的历史命令
查找:
CTRL + R
$(reverse-i-search)`': keyword
ENTER 执行 or CTRL + J 复制粘贴

清屏:CTRL+L or $clear

结束命令:CTRL+C / kill / killall

文件/目录通配符

*

any number of characters

?

any single character

[0-9]

any digit

{a..z}

alphabet

1{13..22}

numbers 113 through 122

如:mkdir book-{2010..2016}-{00..12}

命令一行写不完,换行:\

Linux i/o redirection

STDOUT
$ls -l /usr/bin > ls-output.txt #重定向 stdout
$> ls-output.txt #清空文件
$ls -l /usr/bin >> ls-output.txt #重定向 stdout 并 append

STDERR
$ls -l /bin/usr 2> ls-error.txt #重定向 stderr

STDOUT & STDERR
$ls -l /bin/usr > ls-output.txt 2>&1 #重定向stdin和stderr
$ls -l /bin/usr &> ls-output.txt

$ls -l /bin/usr 2> /dev/null #丢弃stderr

STDIN
$cat < file
$cat access_log_2016_12* > 2016_12_all_in_one.log

Linux Pipelines & filters:  cmd1 | cmd2 

示例:

ls -lt /var/log | head | grep root

cat access_log_2016_07* |awk '{print $7}'|sort|uniq -c|sort -nr|head -n 10 > top10

备注:上面第二个示例表示取我们项目业务数据中2016年7月访问量最高的前10条日志

管道参数说明:

cat—Concatenate files.
sort—Sort lines of text.
uniq—Report or omit repeated lines.
wc—Print newline, word, and byte counts for each file.
grep—Print lines matching a pattern.
head—Output the first part of a file.
tail—Output the last part of a file.
tee—Read from standard input and write to standard output and files.

Linux cmd

分类

常用命令

帮助

man

目录和文件处理

pwd cd mkdir ls ll pwd cp mv touch ls rm rmdir scp rsync

文本处理

cat more less head tail echo vi

系统管理

top ps free kill jobs bg fg iostat lsof crontab

文件系统

du df mount/umount

权限处理

chmod chown chgrp

文件压缩解压

tar gzip/gunzip zip/unzip

用户/组管理

useradd usermod userdel groupadd groupmod groupdel

网络相关

ifconfig telnet nslookup ping

Linux cronjobs / at

定时任务,依赖于任务调度守护进程crond
contab -e #编辑
crontab -l #列表
crontab -r #删除

格式:分时日月周
* : Any possible value
/ : every
, : or
- : range

示例:

30 21 * * * /opt/lampp/apache/bin/apachectl restart
上面的例子表示每晚的21:30重启apache。

45 4 1,10,22 * * /opt/lampp/apache/bin/apachectl restart
上面的例子表示每月1、10、22日的4 : 45重启apache。

10 1 * * 6,0 /opt/lampp/apache/bin/apachectl restart
上面的例子表示每周六、周日的1 : 10重启apache。

0,30 18-23 * * * /opt/lampp/apache/bin/apachectl restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。

0 23 * * 6 /opt/lampp/apache/bin/apachectl restart
上面的例子表示每星期六的11 : 00 pm重启apache。

* */1 * * * /opt/lampp/apache/bin/apachectl restart
每一小时重启apache

几个重要相关文件:
cron 计划信息存储在:/var/spool/cron/{用户}
cron 日志存储在:/var/log/cron

crond 服务:
service crond {start|stop|status|restart|…}
命令stdout & stderr 要重定向

Linux common Limitation

TCP 最大端口号: 65535 (RFC 793 源/目标端口由16位表示,65535 = 2 ^ 16 - 1);
监听端口号小于 1024 的进程须由 OS root 账号启动(也有方法绕开,但并不建议这么做);
命令的参数数量有限制:getconf ARG_LIMIT; ~~ Argument list too long
OS 的应用账户可用的文件描述符和进程数均有限制 ~~ too many open files

可使用 ulimit 命令修改(当前session)或者用 root 账号修改  /etc/security/limits.conf file 以及/etc/security/limits.d/ *.conf

Linux System Resources

Processes
Use the ps command to list processes
ps aux will list all processes (grep it for your username)

CPU
Use the top command to run an interactive system monitor (hit q to exit it)

Memory
The free command shows overall memory usageps can give per-process details (ps -o 'user pid vsize rss %mem cmd')

Open files
lsof lists open files on the system (at least by processes that you have permission to investigate)

Linux network cmd

怎么测试远程端口的防火墙策略是否OK?
telnet ip port

怎么测试GSLB的策略是否OK?
nslookup domain dns

如何检测端口是否在监听?
netstat -anlp | grep 80

ifconfig
tcpdump

参考资料:http://talk.linuxtoy.org/using-cli/#1

上一篇:BZOJ 3412: [Usaco2009 Dec]Music Notes乐谱(离线处理)


下一篇:开篇-QT完全手册