Linux 常用技巧 – tonyhuang

1. 如何设置环境变变量, 比如JAVA_HOME?1) be global to all the users on the Linux box:

You can modify the /etc/profile file with root privilege ( or/etc/bashrc for RMP based linux or /etc/bash.bashrcfor Debian based linux) :

export JAVA_HOME = jdk_home (ex: export JAVA_HOME = /usr/software/java/jdk1.7.0_03)

export PATH = $JAVA_HOME/bin:$PATH

Then do either of these to make it take in effect:a) source /etc/profile (or source /etc/bashrc or source /etc/bash.bashrc)b) su – rootc) Relogin

(This can not work for root user somtimes. Then you need to do previous steps in/root/.bashrc for Debian based linux or/root/.bash_profile for RPM based linux )

2) to be set only for current user:

modify the ~/.bash_profile file for RPM based Linux like Fedora or RedHat linux, or modify the~/.bashrc file for Debian based Linux like Ubuntu or Mint;

and set the env. variables there:

export JAVA_HOME = jdk_home (ex: export JAVA_HOME = /usr/software/java/jdk1.7.0_03)

export PATH = $JAVA_HOME/bin:$PATH

To bring the change u made to it in effect: do either of these -a) source ~/.bash_profilefor rpm linux( or source ~/.bashrc for debian based linux)b) su – usernamec) Relogin to the machine

3)How ro recovery .bashrc ?

echo "set -o noclobber" > ~/.bashrc

2 怎样查看和杀死进程?

查看:

1. ps aux | grep your_process_name | grep -v grep

可以先用ps aux| less 查看进程名字,再用上述命令

2.who

3. w

4. top

这四个命令的具体介绍见 :http://baike.baidu.com/view/573516.htm

杀死:

kill -9 your_process_name

3. 怎样安装、卸载,查看软件包?

sudo apt-get install XXX to install for Ddebian linux ; sudo apt-get remove xxx* to uninstall forDdebian linux

sudo yum install XXX for RMP linux; sudo yum remove xxx* to uninstall for Debian linux

—view repository

yum repolist

— view the specific package

yum list $package

–view the installed package

yum list installed $package

–uninstall package

yum remove $package

4.怎样以ROOT执行命令

用sudo xxx 或者 sudo -s xxx 或者 sudo su xxx

5.激活ubuntu root用户

sudo passwd root

6 用curl上传下载

Curl – ftp, http/https, post or get/download files

Curl可以用来上传或者下载东西,是个命令行工具,支持协议包括 ftp, http(s),包含了 FileZilla/SSH secure shell client/putty 等功能.

How to use it:

    use curl –help to see how to use it.example:

curl -x 10.161.64.10:8080 -U TonyH.Huang:TonyPassword006 -v http://valgrind.org/downloads/valgrind-3.6.1.tar.bz2 -o valgrind-3.6.1.tar.bz2

-x 代理服务器地址和端口号 -U 用户名和密码 -v 远程服务器上的文件 -o 保存在本地的文件

类似的有wget ,比如 wget http://www.google.com -o google-index.html

wget –user xxx –password xxx ftp://ip/package

7. ls 命令

ls -la

ls -lrt

d表示是目录(directory)

-表示是文件

l表示是链接link

c表示是字符设备(character)

b表示是块设备(block)

8. adduser

–ubuntu—:

adduser userid 或者 addduser –home /home/tony –shell /bin/bash userid

;然后执行 passwd userid 设定密码

— RPM linux—

adduser -m -s /bin/bash userid ;然后执行 passwd userid 设定密码

moduser -G xxx user xx 改变用户属组

9. cat

cat意即 concatenate,串联。有三个主要作用:显示文件; 创建文件;合并文件。

还有些次要作用:回响输入,

显示文件: cat filename或者: cat filename1; cat filename2

文件太大: cat file | more (按回车逐行显示) cat file | less (按翻页键, 逐页显示)

创建文件: cat >filename (输入内容,随后按^D)

合并文件: cat file1 file2 > file3(将file1,fie2内容依次输入到file3,如果file3不存在则创建若存在则覆盖) 或者: cat file1 file2 >>file3 (将file1, file2内容依次追加到file3, file3已存在则追加不存在则创建)

10 Shell命令行参数获取

$# Stores the number of command-line arguments that were passed to the shell program.

$? Stores the exit value of the last command that was executed.

$0 Stores the first word of the entered command (the name of the shell program).

$* Stores all the arguments that were entered on the command line ($1 $2 ...)."$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...).

So basically, $# is a number of arguments given when your script was executed.$* is a string containing all arguments. For example,$1 is the first argument and so on. This is useful, if you want to access a specific argument in your script.

As Brian commented, here is a simple example. If you run following command:

./command -yes -no /home/username

$# = 3$* = -yes -no /home/username$@ = array: {"-yes", "-no", "/home/username"}$0 = ./command, $1 = -yes , $2=-no, $4=/home/username.11 source (.)和sh (./)以及export

source 也即.命令,是将脚本文件里的环境变量和函数引入当前shell,无需登录就可生效。

1)有关shell中环境变量的说明

当你在当前shell中定义变量时,此变量仅在当前shell进程中有效,切换用户(会登录到新用户的shell)或者重新生成子shell时其都不可见。

比如当前用户是yarn,在shell终端中定义var1=100

echo $var1 会得到100

继续在shell终端中输入bash (本人用的是ubuntu,前面步骤用其缺省的bash shell执行命令,所以用bash来生成子shell。你的环境应输入当前你正在使用的shell名称,如csh ,ksh)来生成子shell

在子shell再运行 echo $var1 会发现输出为空

当然上面最后一条命令的执行是在子shell中

输入 ps,应该显示有2个shell

再输入exit会退出子shell回到父shell

再输入ps 可以看见剩了一个shell

再输入echo $var1可以看到输出100

由此得出结论:在终端(命令行)用bash类命令生成子shell时,父进程shell定义的变量对子进程不可见。

用类似方法可以验证:在终端(命令行)用bash类命令生成子shell时,子进程shell定义的变量对父进程也不可见。

上述方法使用最原始的方法定义环境变量,没有使用source , export ,sh 等命令

2) source(.读作点)命令

source命令只对文件起作用,其用法为 source filename. 当我修改了/etc/profile文件,我想让它立刻生效,而不用重新登录;这时就想到用source命令,如:source /etc/profile。 功能:使Shell读入指定的Shell程序文件并依次执行文件中的所有语句。source命令通常用于重新执行刚修改的初始化文件,使之立即生效。

注意 source的scope :如果是全局(或用户)的初始化文件如/etc/profile(或者~/.bashrc[ ~/.bash_profile for rpm]) source后立即生效 对所有用户(或所有进程)生效。

如果是一般文件,比如用户工作目录下的一个test.sh, source后立即生效但仅限于当前session,在子进程或切换用户后或重新登录后其变量或函数无效。

写个文件test.sh 内容为

#!/bin/bash

subVar1=101

执行 source tets.sh

再输入 echo $subVar1

可以发现结果为 101

再执行 bash进入子shell

再执行 echo $subVar1

可以发现结果为空

这说明用source后父进程中的环境变量对子进程不可见:

同样也可以验证source后子进程中的环境变量对父进程不可见。

但值得注意的是source这个命令其实只是简单地读取脚本里面的语句依次在当前shell里面执行,没有建立新的子shell。那么脚本里面所有新建、改变变量的语句都会保存在当前shell里面。这一点非常有用。比如文件A.sh里面调用B.sh ,如果用source (ex, source B.sh)那么B中的变量在对A是可见的,并在A的整个shell中可见。这就是为什么上面代码中第二行返回101

缺点就是切换用户或者重新派生子shell后变量不可见。

3) sh 命令

常见是 sh mytest.sh 等价于 ./myest.sh.

sh会重新生成子shell来执行命令,子shell中的变量对父shell不可见,父shell中的变量对子shell也不可见,两者完全独立。这也是一种非常有用的一点。比如A.sh调用 B.sh来执行一些动作,但是希望两者定义的变量即使重名也互不影响,那么用./B.ash 或者sh B.sh就非常适合。

但是值得注意的是在当前shell中对文件执行sh命令后,文件中变量在当前shell不可见,这点跟source有很大不同:

4) export命令

使用export命令对已定义的变量进行输出。 export命令将使系统在创建每一个新的shell时定义这个变量的一个拷贝。这个过程称之为变量输出。其最大的优点是输出的变量在所有派生的子进程中可被引用。

如下图所示:

用户tony在终端中定义了myvar4=404,bash出一个新shell后仍能echo出$myvar4的正确值,然后再切换到用户yarn,也能echo出$myvar4的正确值。

那么子进程export的变量能否被父进程引用呢?答案是否定的

如下图所示,在文件mytest3.sh中export了myvar3,执行后(./mytest3.sh)会产生子进程shell,但是子进程的myvar3父进程当前终端shell读到。

source 和export的结合使用:

我写一个test5.sh内容如下:

#!/bin/bash

myvar5=505

export $myvar5

然后在终端执行:

chmod 777test5.sh

source test5.sh

这时候既能在当前终端读取myvar5也能在产生的子进程中读取myvar5.

当然,如果当前终端关闭(父shell进程停止运行),那么其或者其子所定义的所有变量都消失。打开新的终端(开始新的shell进程)会发现之前终端定义的变量不能读取。

总结:

用户登录到Linux系统后,系统将启动一个用户shell。在这个shell中,可以使用shell运行命令或声明变量,也可以运行shell脚本。运行shell脚本程序时,系统将创建一个子shell。此时,系统中将有两个shell,一个是登录时系统启动的shell,另一个是系统为运行脚本程序创建的shell。当一个脚本程序运行完毕,它的脚本shell将终止,可以返回到执行该脚本之前的shell。从这种意义上来说,用户可以有许多 shell,每个shell都是由某个shell(称为父shell)派生的。

sh 文件 会产生子进程来执行脚本文件,父子进程间不共享变量。

而source 文件 相当于内联,不产生新的子进程,因此父脚本可以访问到子脚本内定义的变量。

export 变量可以将变量从父进程传给子进程。这是为什么shell不关时,及时派生子shell或者切换用户其变量仍能被访问的原因。

12 disbale ipv6

centos/redha/SUSEt:

1).修改/etc/sysconfig/network,追加:NETWORKING_IPV6=no2).修改/etc/hosts,把ipv6本地主机名解析的注释掉:#::1 localhost localhost6 localhost6.localdomain6

Debian-based (ubuntu):To check if IPv6 is enabled or disabled, from a terminal window:$ cat /proc/sys/net/ipv6/conf/all/disable_ipv60 means it’s enabled and 1 is disabled.—Add these lines to the bottom of /etc/sysctl.conf :net.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1net.ipv6.conf.lo.disable_ipv6 = 1Then run sudo sysctl -p or reboot

13 dhcp/static ip 设置

RMP-based:

static:

# vi /etc/sysconfig/network-scripts/ifcfg-eth1 (such as ifcfg-eth0, ifcfg-eth1)

DEVICE=eth1HWADDR=xx:xx:xx:xx:xx:xx # Don’t changeTYPE=EthernetUUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Don’t changeONBOOT=yes # no -> yesNM_CONTROLLED=no # yes -> noBOOTPROTO=none # dhcp -> none# add bellow IPADDR=192.168.56.10NETMASK=255.255.255.0

DHCP:

DEVICE="eth0"HWADDR=MAC Address*System MAC*NM_CONTROLLED="no"ONBOOT="yes"BOOTPROTO="dhcp"

Debian-based(Ubuntu):

1)ls /sys/class/net会看到几个网络适配器,比如: eth1 , eth2 (and lo, the loopback interface)2) sudo vi /etc/network/interfaces将内容编辑为如下:# The loopback network interfaceauto loiface lo inet loopback# 静态ipauto eth1iface eth1 inet static address 192.168.126.101 netmask 255.255.255.0 network 192.168.126.0 broadcast 192.168.126.255# 动态IPauto eth2iface eth2 inet dhcp

—service network restart 重启网络服务(也可以当作检测,如果有错误查看哪一步错误了)ifconfig eth0 up 打开eth0 网卡ifup etho up 打开网络接口

14 enable sudo ,make wheel group has root privilege

su is switch user command. to join regular user to wheel group, he/she can switch root user.—take CentOS6 as example:# visudo## Allows people in group wheel to run all commands%wheel ALL=(ALL) ALL# vi /etc/pam.d/su# Uncomment the following line to require a user to be in the "wheel" group.auth required pam_wheel.so use_uid

# usermod -G wheel $user

15 disbale SElinux

RMP-based:

# vi /etc/selinux/configSELINUX=disabled # enforcing -> disabledDebian-based:

Ubuntu does not use SELinux. If the directory is annoying you, remove it with:sudo rmdir /selinux

16. 关闭防火墙

—RH

# /etc/init.d/iptables stop

–Ubuntu

# ufw disable

17. 建立无密码ssh

1. #ssh-keygen 然后一路回车,或者ssh-keygen -t dsa -P ” -f ~/.ssh/id_dsa

2.将id_rsa.pub或id_dsa.pub拷贝到 目标机对应账号的.ssh目录下

scp ~/.ssh/id_rsa.pub remotehost:~/.ssh

3. 然后在目标机执行 (.ssh目录下)

cat id_rsa.pub >> authorized_keys

4. 如果是 RMP based Linux,比如Redhat/centOS 还需在目标机执行:

chmod 644 ~/.ssh /authorized_keys (或者 chmod 600 ~/.ssh /authorized_keys 这样更安全)

如果是建立到本机的无密码ssh,直接执行下面2行命令即可:

$ ssh-keygen -t dsa -P ” -f ~/.ssh/id_dsa$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

18. 修改hosts文件

/etc/hosts

sudo /etc/init.d/networking restart

19. 修改主机名

Ubuntu:

/etc/hostname

RPM based Linux:

/etc/sysconfig/network

20 常用杂项命令

# uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo # 查看CPU信息# hostname # 查看计算机名# lspci -tv # 列出所有PCI设备# lsusb -tv # 列出所有USB设备# lsmod # 列出加载的内核模块# env # 查看环境变量# free -m # 查看内存使用量和交换区使用量# df -h # 查看各分区使用情况# du -sh < 目录名> # 查看指定目录的大小# grep MemTotal /proc/meminfo # 查看内存总量# grep MemFree /proc/meminfo # 查看空闲内存量# uptime # 查看系统运行时间、用户数、负载# cat /proc/loadavg # 查看系统负载# mount | column -t # 查看挂接的分区状态# fdisk -l # 查看所有分区# swapon -s # 查看所有交换分区# hdparm -i /dev/hda # 查看磁盘参数(仅适用于IDE设备)# dmesg | grep IDE # 查看启动时IDE设备检测状况# ifconfig # 查看所有网络接口的属性# iptables -L # 查看防火墙设置# route -n # 查看路由表# netstat -lntp # 查看所有监听端口# netstat -antp # 查看所有已经建立的连接# netstat -s # 查看网络统计信息# ps -ef # 查看所有进程# top # 实时显示进程状态# w # 查看活动用户# id < 用户名> # 查看指定用户信息# last # 查看用户登录日志# cut -d: -f1 /etc/passwd # 查看系统所有用户# cut -d: -f1 /etc/group # 查看系统所有组# crontab -l # 查看所有用户的定时任务

曾经一直想让别人知道自己的心情,那些沉重,

Linux 常用技巧 – tonyhuang

相关文章:

你感兴趣的文章:

标签云: