Linux 系统管理常用命令(不定时更新)

Catalogue
  1. 1. 运行脚本的方法
    1. 1.1. 常用运行方法:
    2. 1.2. shell script的追踪与Debug
  2. 2. 脚本输入输出例子
  3. 3. test命令
  4. 4. 关闭apache
    1. 4.1. 版本一
    2. 4.2. 版本二
  5. 5. 查看端口
  6. 6. 进程相关
    1. 6.1. top
    2. 6.2. ps
    3. 6.3. 查看运行进程的绝对路径
  7. 7. 文件编码
  8. 8. 文件统计
  9. 9. 用户操作
    1. 9.1. 增加用户
    2. 9.2. 添加组
    3. 9.3. 移除组
  10. 10. 无密码ssh
  11. 11. tmux工具
  12. 12. 开机自启动
    1. 12.1. Example:
  13. 13. 端口映射
  14. 14. 更改文件权限
  15. 15. git
    1. 15.1. 初始化远程仓库
    2. 15.2. 初始化本地仓库
    3. 15.3. 查看、添加、修改远程仓库
    4. 15.4. 修改github仓库

运行脚本的方法

常用运行方法:

1
2
3
4
5
6
$ bash a.sh
$ chmod +x a.sh
$ ./a.sh
$ source a.sh

用bash和用source的不同在于:

  • 用bash执行时,shell script其实是在在父程序bash下新建了一个 bash子程序,之后这个子程序中执行,当程序执行完后,shell script里定义的变量都会随子程序的结束而消失;
  • 用source执行时,是在父程序bash中执行,shell script里定义的变量都还在。

shell script的追踪与Debug

1
2
sh -n xx.sh # 语法检查
sh -x xx.sh # 列出xx.sh的执行过程

脚本输入输出例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# !/bin/bash
# Program:
# This program is used to ouput parameter of the shell script
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
echo "The script's name is ==> $0"
echo "Total parameter number is ==> $#"
# Check whether number of the parameter is less than 2
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2.Stop here." && exit 0
echo "The whole parameter is ==> '$@'"
echo "The first parameter is ==> $1"
echo "The first parameter is ==> $2"
exit 0

输入和输出为:

1
2
3
4
5
6
$ bash sh05.sh 1a 2b 3c 4d
The script's name is ==> sh05.sh
Total parameter number is ==> 4
The whole parameter is ==> '1a 2b 3c 4d'
The first parameter is ==> 1a
The first parameter is ==> 2b

test命令

1
test -e a.txt && echo "exists" || echo "not exists"

当a.txt存在时,执行echo “exists”,否则执行echo “not exists”,多个命令用&&连接即可。改变test参数可以进行多种决策判断,可以研究一下。

关闭apache

版本一

1
/etc/init.d/httpd stop

版本二

1
/etc/init.d/apache2 stop

查看端口

1
2
3
4
5
6
7
netstat -anp | grep 9200
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 192.168.0.1:53570 192.168.0.1:9200 ESTABLISHED -
tcp6 0 0 192.168.0.1:9200 :::* LISTEN -
tcp6 0 0 192.168.0.1:9200 192.168.0.1:53570 ESTABLISHED -
unix 3 [ ] STREAM CONNECTED 105509200 -

参数说明:

1
2
3
-p process
-a all,显示所有连接和监听端口。
-n 以数字显示地址和端口(否则忽略虚拟机动态ip,127.0.0.1会以localhost显示)

进程相关

top

相关命令:c,m,u等

1
2
3
4
// 实时查看某个进程创建的线程情况
$ top -H -p <pid>
// 类似有,但还是top信息更多
$ ps -T -p <pid>

ps

显示瞬间行程(process)的动态

使用权限:所有使用者
使用方式:

1
ps [options] [--help]

参数:
ps的参数非常多,在此仅列出几个常用的参数并大略介绍含义

1
2
3
4
-A 列出所有的进程
-w 显示加宽可以显示较多的资讯
-au 显示较详细的资讯
-aux 显示所有包含其他使用者的行程

可添加过滤用户:

1
| grep xx

杀死进程:

1
2
3
kill -s 9 xxx(pid)
# 等价于
kill -9 xxx(pid)

查看运行进程的绝对路径

先通过top或其它命令查看pid,再通过以下命令查看真实运行路径:

1
2
$ ls -al /proc/24416/exe
lrwxrwxrwx 1 hadoop hadoop 0 Apr 1 21:51 /proc/24416/exe -> /home/hadoop/jdk1.8.0_40/bin/java

文件编码

当将可执行的文件从windows系统搬到linux下,可能会报类似’\r’ 找不到命令的错误,那是因为shell命令中格式与dos命令格式不对造成的,只需要运行:

1
dos2unix file.sh

文件统计

  • 对当前路径下文件的存储进行从大到小的排序并打印,显示G,MB,K等

    1
    du -sh * | sort -rh
  • 统计第二行逗号出现的次数

    1
    sed -n '2 p' test.csv/part-00000 |grep -o ","|wc -l
  • 合并操作
    感受下paste的魅力:

    1
    paste -d " " - - - - < 16_1.fastq

用户操作

增加用户

  • 方法1

    1
    2
    useradd -d /data/home/user1 -s /bin/bash -m user1
    passwd user1
  • 方法2

    1
    2
    adduser --home /software/home/xxx xxx
    vim /etc/passwd

添加组

1
2
# add to hadoop groups
sudo usermod -a -G hadoop user1

移除组

1
gpasswd -d 用户名 组名

无密码ssh

  • 一般做法
  1. 生成两个重要文件
    1
    ssh-keygen -t rsa

id-rsa #私钥
id-rsa.pub #公钥

  1. 将公钥中的内容追加到另一台机器文件中
    1
    ~/.ssh/authorized_keys

这样另一台机器就可以无密码ssh到生成公钥对应的机器。同理,其它机器也要这样做,那么假如有n台机器,每台机器authorized_keys文件需要n-1行,代表其它n-1台机器的公钥。优点麻烦,用第二种方法。

  • 共用公私钥
  1. 生成两个重要文件
    1
    ssh-keygen -t rsa

id-rsa #私钥
id-rsa.pub #公钥

  1. 复制和分发
    新建一个authorized_keys文件,内容为id-rsa.pub中的一行,然后将这三个文件共享到其它节点的~/.ssh目录下,切记,3个文件都要拷贝。

tmux工具

  • 删除panel:
    C-x + x
  • 迅速切换排版(水平或垂直):
    C-x + 空格
  • 创建新窗口(panel)
    C-x + c
  • 进行切换:
    C-x + 数字键
  • 复制粘贴:
  1. C-x [ 进入复制模式
  2. 参考上表移动鼠标到要复制的区域,移动鼠标时可用vim的搜索功能”/“,”?”
  3. 安空格键开始选择复制区域
  4. 选择完成后安enter键退出
  5. C-x ] 粘贴
  • 清除区域内容:
    C-l

开机自启动

Example:

Ubuntu下,作为利用apt-get的常规软件,以Nginx为例

  • 编辑开机nginx启动脚本(自动安装的忽略)

    1. /usr/local/nginx.sh
    2. 在/etc/init.d目录下创建链接文件到前面的脚本: ln -s /usr/local/nginx.sh /etc/init.d/nginx
  • 设置nginx脚本可执行权限(自动安装的忽略)

    1
    chmod u+x /etc/init.d/nginx
  • 进入/etc/init.d目录,将该脚本设为开启自启动

    1
    update-rc.d -f nginx defaults [startnum] [killnum]

其中的[startnum]表示启动顺序,[killnum]表示退出顺序,都为可选参数,取值范围是0-99。序号越大的越晚执行。

  • 如果想取消开机自启动
    1
    update-rc.d -f nginx remove

-f选项表示强制执行。

端口映射

nohup portforward :8080 cu01:8080 &

更改文件权限

chmod [who] [+ | - | =] [mode] 文件名

  • 权限范围:
    u :目录或者文件的当前的用户
    g :目录或者文件的当前的群组
    o :除了目录或者文件的当前用户或群组之外的用户或者群组
    a :所有的用户及群组

  • 权限代号:
    r :读权限,用数字4表示
    w :写权限,用数字2表示
    x :执行权限,用数字1表示
    s :特殊权限,如root般操作该文件

e.g 为所有用户添加执行test.log的权限

1
chmod a+x test.log

用代号是最灵活的方式,但当只涉及对所有用户的,并且是直接设定某个权限,用数字是最直接的。

1
chmod 764 test.log

表示用户拥有所有权限,所在组其它成员拥有读写权限,其它用户只有读权限。

git

初始化远程仓库

1
2
3
4
cd /SHARE/repositories/
mkdir webpy & cd webpy
mkdir webpy.git & cd webpy.git
git init --bare

初始化本地仓库

1
2
3
4
cd local_project
git init
git add .
git commit -m "xxx"

查看、添加、修改远程仓库

1
2
3
4
5
git remote -v
git remote add origin gujw@192.168.0.2:/SHARE/repositories/webpy/webpy.git
git remote set-url origin gujw@192.168.0.2:/SHARE/repositories/webpy/webpy.git

Tip:对于共享目录的仓库而言,修改client端的ip即可,远程仓库地址不变。

修改github仓库

1
2
git remote add origin git@github.com:username/RepoName.git
git push origin master
Comments