超实用Linux工具与命令|长期更新

254 阅读5分钟

这是我参与8月更文挑战的第1天,活动详情查看:8月更文挑战


1.本地复制到远程

cd  /var/image/
scp test.txt ubuntu@192.168.1.135:/home/ubuntu/

2.Linux压缩拆包与解压

压缩

tar -zcvf cm-11.tar.gz cm-11

拆包

split -b 4000M -d -a 1 cm-11.tar.gz cm-11.tar.gz

压缩拆包一体

tar -zcvf  cm-11 | split -b 4000M -d -a 1 - cm-11.tar.gz.

例子:

tar -zcvf - debianXFCE-TPOT | split -b 3072m -d -a 1 - debianXFCE-TPOT.tar.gz.

合并文件

cat cm-11.tar.gz.0 cm-11.tar.gz.1 > cm-11.tar.gz

例子:

cat debianXFCE-TPOT.tar.gz.0 debianXFCE-TPOT.tar.gz.1 > debianXFCE-TPOT.tar.gz

合并文件解压一体

cat cm-11.tar.gz* | tar xvf -

3.生成随机密码

[root@ubuntu~]# openssl rand -base64 6 
Jxxg8glb
[root@ubuntu ~]# openssl rand -base64 8
h27/kPKxnMU=
[root@ubuntu ~]# openssl rand -base64 16
7qZ7yqrxc3Om8zNdD04CPQ==
[root@ubuntu ~]# openssl rand -base64 32
9IfRZGLbRaocZ6I++0ZoNbhLztwlNVyHh3b+yTFr5l4=
[root@ubuntu ~]#

4.ubuntu默认Python版本设置

python版本建立软连接设置

ln -s /usr/local/python3/bin/python3 /usr/bin/python

如果第一次出错,想要再次建立软连接,覆盖

ln -sf /usr/local/python3/bin/python3 /usr/bin/python

5.计算当前文件夹中文件数量

查看当前目录下有多少个文件及文件夹需在终端输入

ls | wc -w

查看当前目录下有多少个文件需在终端输入

ls | wc -c

查看当前文件夹下有多少个文件,多少个子目录需在终端输入

ls -l |wc -l

查看当前文件夹中文件的数量,则需在终端输入

/bin/ls -l |grep ^-|wc -l

6.windows中编写shell脚本,在Linux运行出错

问题原因 windows下编译的shell文件,每一行的结尾是\n\r,而在linux下文件的结尾是\n。 blog.csdn.net/lin_bingfen… 解决思路 利用正则表达式: 在Linux中单个文件时输入命令 :

sed -i 's/\r$//'  filename.sh  

在Linux中多个文件时输入命令 :

sed -i 's/\r$//'  *.sh       //利用通配符

参数解释

sed -i 's/\r$//' filename.sh
# -i    //插入
# s     //替代模式
# \r$   //表示任何以\r结束的字符
# //整句意思是把以\r结束的字符换成空白

7.history 历史命令显示时间用户

在文件末尾添加以下代码,或者bashrc文件的上半部也有,可以修改添加。

当前用户

vim ~/.bashrc

全局

vim /etc/profile
HISTFILESIZE=2000000      #设置保存历史命令的文件大小
HISTSIZE=20000000         #保存历史命令条数
HISTTIMEFORMAT="%Y/%m/%d-%H:%M:%S by `whoami`: "   #记录每条历史命令的执行时间和执行者
export HISTTIMEFORMAT

添加完保存后

source ~/.bashrc

例子:

 18 # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
 19 HISTSIZE=1000000
 20 HISTFILESIZE=2000000
 21 HISTTIMEFORMAT="%Y/%m/%d-%H:%M:%S by `whoami`: "
 22 export HISTTIMEFORMAT

至此,history查看就可以看到历史命令含时间和使用的用户了

8.Ubuntu18.04设置源、网卡

阿里源

cp /etc/apt/sources.list /etc/apt/sources.list.bak
vim /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

最后执行如下命令更新源

##更新
sudo apt-get update
sudo apt-get upgrade

9.Ubuntu18.04设置网卡

这个新的系统和ubuntu 16在网络配置上有些不一样 1,配置文件不再使用/etc/network/interfaces,而是使用

vim /etc/netplan/????.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  ethernets:
          enp0s25:
                  dhcp4: no
                  dhcp6: no
                  addresses: [192.168.5.55/23]
                  gateway4: 192.168.4.1
                  nameservers:
                          addresses: [202.97.224.68]
                  optional: true
  version: 2
  renderer: networkd

注意:这里的网段是5.55,但是网关是4.1,所以子网掩码使用/23,同一子网内主机IP范围是:192.168.4.1~192.168.5.254,一共510个,额外2个是广播地址,如果使用/24,就无法和4.1通信。 修改完后使用下面指令生效

# netplan apply

使用路由命令查看,有下列显示才算正常

root@ubuntu:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.4.1     0.0.0.0         UG    0      0        0 enp0s25
192.168.4.0     0.0.0.0         255.255.254.0   U     0      0        0 enp0s25

10.Ubuntu:自定义开机自启动脚本

配置开机自启

 vim /usr/local/webgoat/startwebgoat.sh

脚本内容

#!/bin/sh
 sleep 30
 nohup java -jar /usr/local/webgoat/webgoat-server-8.0.0.M25.jar --server.port=8080 --server.address=0.0.0.0 &
 sleep 60 
 nohup java -jar /usr/local/webgoat/webwolf-8.0.0.M25.jar --server.port=9090 --server.address=0.0.0.0 &
 cp /usr/local/webgoat/startwebgoat.sh /etc/init.d/
 chmod 755 /etc/init.d/startwebgoat.sh
 cd /etc/init.d/
 update-rc.d startwebgoat.sh defaults 95

11.Ubuntu修改时区和更新时间

先查看当前系统时间

root@ubuntu:/# date -R   

1.运行tzselect

root@ubuntu:/# tzselect

依次选择亚洲 Asia,确认之后选择中国(China),最后选择北京(Beijing)

2.复制文件到/etc目录下

root@ubuntu:/# cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

3.再次查看时间

date -R

12.shell脚本加密工具shc

www.datsi.fi.upm.es/~frosal github.com/yanncam/UnS…

资源文件:shc-Unshc

(1).shc安装

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9b.tgz
tar -vxf shc-3.8.9b.tgz
cd shc-3.8.9b
make test
make strings
sudo mkdir -p /usr/local/man/man1/
sudo make install

(2).验证是否安装完成

which shc 

常用参数:

-e date (指定过期日期)
-m message (指定过期提示的信息) 

-f script_name(指定要编译的shell的路径及文件名)
-r   Relax security. (可以相同操作系统的不同系统中执行)

-v   Verbose compilation(编译的详细情况)

(3).生成动态链接的二进制可执行文件

shc -r -f deploy.sh
-f 指定要解密的文件
-r 指定一种安全的加密方式。

生成两个文件,deploy.sh.x和deploy.sh.x.c,前者是动态链接的二进制可执行文件,后者相应的是C程序。 注:这里产生一个问题,因为动态链接属性,前者deploy.sh.x运行时需要对应库的链接的支持,也就是说,目标运行环境也需要安装shc或者安装shc的库。具体解决办法见下后面的:生成静态链接的二进制文件。 (4)运行

./deploy.sh.x

可以直接

mv deploy.sh.x deploy

把.sh.x的文件类型信息简单隐藏掉。 使用

file  deploy.sh.x #可能会报错

(5).设置脚本过期时间

shc -e 14/09/2016  -f deploy.sh

(6).生成静态链接的二进制可执行文件

CFLAGS=-static shc -r -f  deploy.sh

(7).自定义返回信息

shc   -m  "hello world "  -f  deploy.sh

报错处理 原因:

You have to put the sha-bang (e.g. #!/bin/bash) at the beginning of the script, since shc need to know which shell is using. Here is an excerpt from the manual page

解释: 在脚本的第一行添加上沙邦

#!/bin/bash

13.Unshc解密shc

www.datsi.fi.upm.es/~frosal github.com/yanncam/UnS…

资源文件:shc-Unshc

14.从指定路径目录中,搜索文件中的关键字

grep "管理" /www/wwwroot/web/ -Rn