Linux常用命令

文件下载/复制

1
2
3
4
5
6
7
wget http://dfkdl/file.zip

-- 本地文件复制到远程主机
scp a.zip user@ip:/home/user

-- 复制远程文件到本地
scp user@ip:/home/user/a.zip ~

yum/apt 配置

centos/oracle linux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#cd /etc/yum.repo.d
#vim aliyun.repo
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=0

清理缓存
#yum clean all

更新软件
#yum update

安装
#yum install java -y

卸载
#yum remove java* -y

ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
配置apt源
$sudo vi /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted
deb http://mirrors.aliyun.com/ubuntu/ bionic universe
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates universe
deb http://mirrors.aliyun.com/ubuntu/ bionic multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

更新依赖
$sudo apt-get update

升级软件
$sudo apt-get upgrade -y

安装软件
$sudo apt-get install nginx

查看操作系统版本

1
2
3
4
5
6
lixl@DESKTOP-GRDL0QL:/etc/apt$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.2 LTS
Release: 18.04
Codename: bionic
1
2
3
4
5
6
7
$ yum install -y redhat-lsb
[oracle@enmoedu1 ~]$ lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 6.4 (Santiago)
Release: 6.4
Codename: Santiago

操作系统内核及架构

1
uname -a

查看内存

1
2
3
4
lixl@DESKTOP-GRDL0QL:/etc/apt$ free -h
total used free shared buff/cache available
Mem: 11G 5.5G 5.7G 17M 223M 5.8G
Swap: 12G 80M 12G

top命令

查看磁盘空间

1
2
3
4
5
6
7
8
[oracle@enmoedu1 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_enmoedu1-lv_root
20G 5.1G 14G 28% /
tmpfs 2.0G 245M 1.8G 12% /dev/shm
/dev/sda1 477M 65M 387M 15% /boot
/dev/mapper/vg_enmoedu1-lv_u01
35G 8.0G 26G 24% /u01

查看文件(目录)占用空间

1
2
3
4
5
6
7
8
9
10
lixl@lixldeMacBook mydocs % du -sh dataset 
1.8G dataset

lixl@lixldeMacBook mydocs % ls -alh dataset
total 3710672
drwxr-xr-x 6 lixl staff 204B Jul 19 16:22 .
drwxr-xr-x 3 lixl staff 102B Jul 19 16:22 ..
-rw-r--r-- 1 lixl staff 3.3M Jul 19 16:22 links.csv
-rw-r--r-- 1 lixl staff 1.8G Jul 19 16:25 ratings.csv
-rw-r--r-- 1 lixl staff 5.8M Jul 19 16:22 restaurants.csv

修改主机名hostname

centos系列

1
2
3
4
5
6
vim /etc/sysconfig/network
vim /etc/hosts


hostnamectl set-hostname myserver03
hostname -f 查看主机名设置

查看进程

1
2
3
$ps -ef| grep java

$jps

查看文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
读取全部内容
$cat a.txt

控制台行
$tail -f a.log

查看最后10
$tail -10 a.log

查看前10
$head -10 a.log


看日志
cat xx.csv 看整个文件
tail -f xx.log 从尾部查看文件内容
head -10 xx.csv 查看前10
grep -5 'sss' xx.log 搜索并打印前后5
sed -n '1,10p' xx.csv 查看1-10

文件解压、压缩

1
2
3
4
5
6
7
8
解压a.zip到当前目录
$unzip a.zip

解压到指定目录
$unzip -d /temp a.zip

压缩temp文件夹
$zip -r temp.zip temp/
1
2
3
解压tar.gz
$tar zxvf package.tar.gz
$tar zxvf package.tar.gz -C target_dir

查看端口占用情况

1
2
3
$ps -ef|grep 8080

# lsof -i:22

后台运行命令

例:

  1. 后台执行docker pull oraclelinux:7.7

    1
    nohup docker pull registry.cn-beijing.aliyuncs.com/lixl_repo/oracle_database:11.2.0.4.0 > out.log 2>&1 &

out.log是输出的日志文件

  1. jobs 查看当前有多少在后台运行的命令
    jobs -l选项可显示所有任务的PID,jobs的状态可以是running, stopped, Terminated。
    但是如果任务被终止了(kill),shell 从当前的shell环境已知的列表中删除任务的进程标识

开机执行命令

配置开机自启动
vim /etc/rc.d/rc.local

1
su - nexus -c "/nexus/nexus-3.13.0-01/bin/nexus start"

切换到nexus用户来执行命令

服务器时间设置(ntp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装ntp服务
yum install ntp
# 开机启动服务
systemctl enable ntpd
# 启动服务
systemctl start ntpd
# 设置亚洲时区
timedatectl set-timezone Asia/Shanghai
# 启用NTP同步
timedatectl set-ntp yes
# 重启ntp服务
systemctl restart ntpd
# 手动同步时间
ntpq -p

ssh登录

使用共享盘

挂载共享磁盘(samba)

1
2
3
4
5
# 挂载
mount -t cifs //192.168.5.200/share/p2m/lixl /media/ -o "rw,username=oracle,password=123456"

#卸载
umount media/

NFS使用

服务端

  • nfs 服务安装

    1
    yum install nfs-utils -y

systemctl start nfs-server

systemctl enable nfs-server

1
- 暴露共享目录

vim /etc/exports #编辑配置文件

1 /mnt *(sync,rw,anonuid=1001,anongid=1001)

#共享目录 对所有用户共享(同步数据,读写权限,指定用户ID,组ID’如建立的文件属于此用户’)
2 /nfsshare 172.25.151.0/24(sync) 172.25.151.50(sync,rw)

#共享目录 对此网段的共享(数据同步)对此 IP 进行共享(数据同步,读写)

编辑完成后执行:exportfs -rv 进行刷新
exportfs #维护导出的NFS文件系统的表
-r #重新导出所有目录
-v #示导出选项列表

1
2
3

## 客户端
- 挂载nfs共享目录

查看服务端都有哪些共享文件夹

showmount -e 192.168.18.147

挂载到本地目录(如/data)

mount 192.168.18.147:/nfshare /data

1
2

- 开机自动挂载

vim /etc/fstab

192.168.18.147:/data/sharedir /data nfs defaults 0 0