Linux基本命令

Ubuntu安装python3
sudo apt install python3
sudo apt install python3-pip
1.安装软件
1.解压软件  unzip
2. ./configure
    使用./configure出错执行下面命令
         autoreconf  -iv    自动生成configure文件
3.make
4. make install 

注意,由于xshell远程连接ubuntu是通过ssh协议的,所以,确保ubuntu安装ssh服务器:

输入以下命令进行安装远程ssh服务

# sudo apt-get install openssh-server
若没有ssh,需要执行

# sudo apt-get install ssh

ubuntu安装paramiko需先安装依赖  sudo apt-get install libssl-dev

;  # 注释内容
安装python
1.下载https://www.python.org/downloads/source/
2.解压 tar -zxvf /usr/local/python3
3.配置编译安装 ./configure --prefix=/usr/local/python3
            make && make install
4.建立软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

cat /etc/redhat-release:查看系统版本
        cat a.txt>b.txt   #将a中的内容覆盖到b中   
        cat a.txt>>b.txt  #将a中的内容追加到b中
hostname:查看主机名
init 0 
exit
man == -h == --help
whoami
who
useradd                         # 添加新用户
passwd                          # 修改用户密码
clear
ls 
ls -l
ls -l /目录
ll -d                           # 查看目录的权限
ls -a                           # 查看隐藏文件 点开头
cal                             # 查看日历
date
date                            # 修改时间 格式:'月日时分年'
su                              # 切换账户
pwd                             # 显示当前目录
cp                              # 复制
    语法:cp [-adfilprsu]  source destination
    参数解释:

    a:相当于-pdr的意思
    d:若来源档为连结档的属性(link file),则复制连结档属性而非文件本身;

    f:为强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次;

    -i:若目标档(destination)已经存在时,在覆盖时会先询问动作的进行(常用)

    -l:进行硬式连结(hard link)的连结档创建,而非复制文件本身;

    -p:连同文件的属性一起复制过去,而非使用默认属性(备份常用);
    -r:递归持续复制,用於目录的复制行为;(常用)
    -s:复制成为符号连结档 (symbolic link),亦即『捷径』文件;

    -u:若 destination 比 source 旧才升级 destination !

    a)复制文件到指定目录
    命令:cp source destination
    实例:cp  11 test

    b)复制目录到目录
    命令:cp -r source destination 或者 cp -a source destination
    实例:cp -a 22 test

    或者:cp -r 22 test

    c)将多个源文件复制到目录
    命令:cp file1 file2 file3  destination
    实例:cp qq ww ee cpdir

    d)将多个目录复制到目录
    命令:cp  -r  dir1 dir2 dir3 destination 或者 cp  -a dir1 dir2 dir3 destination
    实例:cp -r 11 22 cpdir

    或者:cp -a 11 22  cpdir
alias                           # 别名
mv 
mkdir                           # 创建目录
touch                           # 创建文件
cat                             # 查看文件内容
head/tail                       # 查看指定行数的内容 tail -f 动态查看

关于用户创建
    /etc/passwd    root:x:0:0:root:/root:/bin/bash
                   用户名:密码:userid:groupid:描述信息:家目录:可登录/bin/bash,不可登录/sbin/nologin
    创建一个用户:test会自动在以下目录中创建文件
    /etc/passwd   创建一条用户信息
    /etc/shadow   
    /etc/group
    /etc/gshadow
    /home
    /var/spool/mail
    # 查看用户id信息
    [root@localhost ~]# id tim
    uid=1001(tim) gid=1001(tim) 组=1001(tim)
    [root@localhost ~]# id why
    uid=1000(why) gid=1000(why) 组=1000(why),10(wheel),1001(tim)
    userdel -r
    usermod -d # 修改家目录
    usermod -G # 将用户添加到指定组中,-aG追加
关于文件:
    ls -l 
    -rw-------. 1 root root 1547 7月   6 05:16 anaconda-ks.cfg
    权限信息 硬连接数 属主 属组 创建日期 文件名
    权限信息:
        1:文件类型
            -:普通文件
            d:目录文件
            l:软链接(快捷方式)
            b:设备
            p:管道文件
        2-4:属主权限,读r、写w、执行x
        5-7:属组权限
        8-10:
    修改权限:
        chmod u,g,o = --- 或者 +- --- 文件名

创建一个新用户tim
在root用户下创建一个文件c.txt
修改文件权限
在why用户下创建一个w.txt
修改文件权限使其他人没有任何权限
用tim用户修改这个文件
将tim用户添加到why组中
再次修改这文件
执行文件的方法
sh 文件路径
. 文件路径
bash 文件路径
./ 文件路径
创建于~目录下的文件不会在其他用户下出现
chown 属主.属组 文件名/目录
chown -R 递归修改,目录下所有文件修改
mkdir -p /a/b/c/d 递归创建
r->4 w->2 x->1
> 覆盖符号
>> 追加符号
wc -l 显示文件有多少行
| 管道符 上一步命令的结果当作下一步命令的操作对象
cat a.txt b.txt > new.txt | wc -l 
归档:将多个文件打包在一个文件中
tar -c(创建)f(文件名)v(显示创建 过程) test.tar 需要打包的文件名 空格
tar -tf 查看
tar xvf 文件名 -C 指定位置 解包
压缩:
两种压缩算法: gzip bzip2
vim:
命令行模式下
dd 删除光标所在行
u 撤销操作
u 撤销操作
yy复制光标当前行
p粘贴到光标下一行
P粘贴到光标上一行
set nu:显示行数

top: 查看进程
free: 查看内存
             total        used        free      shared  buff/cache   available
Mem:        2031888      687352      829444        9964      515092     1134900
Swap:       2097148           0     2097148
ps aux :查看所有进程
pstree:查看进程树
grep '内容': 筛选 (-v过滤) 
kill -9 PID :强制杀死
fg %工作号:后台程序调到前台
bg %工作号:后台暂停运行
程序名 & 程序后台运行
jobs:查看工作号

系统启动顺序
BIOS:找到硬盘
MBR:主引导目录 512b 446引导信息 64分区信息 2结束符
grub:找内核
kernel:
init:进入的级别 0,1,2,3,4,5,6


添加一块硬盘的流程:
fdisk -l :查看硬盘
    磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x0e2ab367

       设备 Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    10487807     5242880   83  Linux

1.对硬盘分区:
fdisk /dev/硬盘名(sda,sdb):进入一个程序在其中进行磁盘分区等操作
    [root@localhost ~]# fdisk /dev/sdb
    欢迎使用 fdisk (util-linux 2.23.2)。

    更改将停留在内存中,直到您决定将更改写入磁盘。
    使用写入命令前请三思。

    Device does not contain a recognized partition table
    使用磁盘标识符 0x0e2ab367 创建新的 DOS 磁盘标签。

    命令(输入 m 获取帮助):m
    命令操作
       a   toggle a bootable flag
       b   edit bsd disklabel
       c   toggle the dos compatibility flag
       d   delete a partition
       g   create a new empty GPT partition table
       G   create an IRIX (SGI) partition table
       l   list known partition types
       m   print this menu
       n   add a new partition
       o   create a new empty DOS partition table
       p   print the partition table
       q   quit without saving changes
       s   create a new empty Sun disklabel
       t   change a partition's system id
       u   change display/entry units
       v   verify the partition table
       w   write table to disk and exit
       x   extra functionality (experts only)

        
n:进行分区
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)  主分区 :最多只能创建四个主分区,MBR中64b的引导信息所限制
       e   extended   :扩展分区:属于四个主分区,在其中可以分更多的逻辑分区
    Select (default p): 

命令(输入 m 获取帮助):n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): p
    分区号 (1-4,默认 1):1
    起始 扇区 (2048-41943039,默认为 2048):
    将使用默认值 2048
    Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+5G
    分区 1 已设置为 Linux 类型,大小设为 5 GiB

p:查看分区

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0e2ab367

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

w:保存退出
2.更新磁盘表
partprobe:更新磁盘列表
3.格式化磁盘,创建文件系统
mkfs.ext4 /dev/sdb1
    [root@localhost ~]# mkfs.ext4 /dev/sdb1
    mke2fs 1.42.9 (28-Dec-2013)
    文件系统标签=
    OS type: Linux
    块大小=4096 (log=2)
    分块大小=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    327680 inodes, 1310720 blocks
    65536 blocks (5.00%) reserved for the super user
    第一个数据块=0
    Maximum filesystem blocks=1342177280
    40 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

    Allocating group tables: 完成                            
    正在写入inode表: 完成                            
    Creating journal (32768 blocks): 完成
    Writing superblocks and filesystem accounting information: 完成 

可创建的文件系统类型
    mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat    
    mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs:处理海量数据   
4.挂载磁盘
mount:将磁盘挂载在指定目录下,在该目录中创建的文件或者目录将被写道磁盘的该分区中
umount:卸载磁盘
mount /dev/sdb1 /sdb1
[root@localhost /]# mkdir sdb1
[root@localhost /]# mount /dev/sdb1 /sdb1
df:查看挂载关系
[root@localhost /]# df
    文件系统                   1K-块    已用     可用 已用% 挂载点
    /dev/mapper/centos-root 17811456 3626800 14184656   21% /
    devtmpfs                 1000024       0  1000024    0% /dev
    tmpfs                    1015944       0  1015944    0% /dev/shm
    tmpfs                    1015944    9200  1006744    1% /run
    tmpfs                    1015944       0  1015944    0% /sys/fs/cgroup
    /dev/sda1                1038336  164004   874332   16% /boot
    tmpfs                     203192       4   203188    1% /run/user/42
    tmpfs                     203192      24   203168    1% /run/user/0
    /dev/sdb1                5029504   20472  4730504    1% /sdb1

    
du:统计一个目录下文件有多大

[root@localhost ~]# df -i :查看inode编号  -h:查看空间大小
文件系统                  Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/mapper/centos-root 8910848  122880 8787968       2% /
devtmpfs                 250006     401  249605       1% /dev
tmpfs                    253986       1  253985       1% /dev/shm
tmpfs                    253986     584  253402       1% /run
tmpfs                    253986      16  253970       1% /sys/fs/cgroup
/dev/sda1                524288     327  523961       1% /boot
tmpfs                    253986       6  253980       1% /run/user/42
tmpfs                    253986      17  253969       1% /run/user/0
/dev/sdb1                327680      11  327669       1% /sdb1


ln -s :软链接   ln -s /a/sou.txt /b/des.txt
特点:
1.就相当于快捷方式
2.删除目标文件,源文件无影响
3.删除源文件,目标文件消失
4.无论修改哪一个,都共同被修改
5.inode号不一样
6.指向文件名
7.可跨分区链接
ln :硬链接
特点:
2.删除目标文件,源文件无影响
3.删除源文件,目标文件无影响
4.无论修改哪一个,都共同被修改
5.inode号一样
6.指向inonde号
7.不可跨分区

ifconfig:查看网卡  ip a
route -n:查看网关
arping -I eth0 192.168.11.11 :返回mac地址,检查是否有冲突,有冲突则返回两个mac地址
ifdown :关闭网卡
ifup:开启网卡   ifup ens33
网卡配置文件 :/etc/sysconfig/network-scripts/
重启网络服务:systemctl restart network

linux中软件包有两种形式
tar.gz:源码,需要编译安装
rpm :红帽Redhat的一种软件包格式,本身就是二进制,也就是编译好的程序
rpm -ivh:安装rpm包   最后加--force(覆盖安装)
rpm -qa:显示已安装的所有rpm包 -ql查询软件包安装位置 -qi:信息
rpm -e:卸载
wget:下载rpm软件包

自定义yum源
安装createrepo
mkdir /test
cp 软件包 /test
createrepo /test
修改/etc/yum.repos.d/local/repos
baseurl = /test

yum的模糊安装
yum install *http*
yum erase 软件包名
yum clean all:删除缓存
gcc-* glibc*

部署一个服务 apache
    准备环境
        关闭防火墙(centos6:service iptables stop,开机就关闭:chkconfig iptables off)
                    centos7:systemctl stop firewalld.service
        关闭selinux :setenforce 0
        配置ip
    装软件包 yum install httpd -y 
    修改配置文件vim /etc/httpd/conf/httpd.conf 
            Listen端口
            DocumentRoot:文档根目录  
            显示界面文件:/var/html
    重启服务
    systemctl restart httpd.service
    测试 
    查看主机名:hostname ,如果修改直接在后面加要修改的名 还要修改 /etc/hosts
        通过查看welcome.conf这个配置文件,可以发现apache默认页面文件为
        /usr/share/httpd/noindex/index.html

samba服务 网盘
    创建一个samba服务网盘
    关闭SElinux :

        [root@bogon ~]# sestatus           查看selinux状态
        SELinux status:                 enabled
        SELinuxfs mount:                /sys/fs/selinux
        SELinux root directory:         /etc/selinux
        Loaded policy name:             targeted
        Current mode:                   permissive
        Mode from config file:          enforcing
        Policy MLS status:              enabled
        Policy deny_unknown status:     allowed
        Max kernel policy version:      28

        临时关闭selinux 
        setenforce 0 
        永久关闭selinux 
        vi /etc/selinux/config 
        修改enforcing为disabled
    关闭防火墙:
        [root@bogon ~]# firewall-cmd --state                        查看防火墙状态
        running                                                     开启状态
        [root@bogon ~]# systemctl stop firewalld.service            关闭防火墙
        [root@bogon ~]# firewall-cmd --state 
        not running                                                 关闭状态

    1. yum install samba -y   
            # 被占用
            Another app is currently holding the yum lock; waiting for it to exit...
              另一个应用程序是:PackageKit
                内存:164 M RSS (1.0 GB VSZ)
                已启动: Fri Aug  2 10:21:06 2019 - 06:21之前
                状态  :睡眠中,进程ID:2489
            命令  rm -f /var/run/yum.pid

    2.进入配置文件 /etc/samba/smb.conf
        # See smb.conf.example for a more detailed config file or
        # read the smb.conf manpage.
        # Run 'testparm' to verify the config is correct after
        # you modified it.

        [global]

                security = user   # 用户模式user share
                passdb backend = tdbsam 


            添加一个网盘
            [public]    
            ;    comment = Public Stuff#描述信息
            ;    path = /home/samba    #目录
            ;    public = yes          #公开
            ;    writable = no         #是否可写
            ;    printable = no        #打印
            ;    write list = +staff   #允许谁写
    3.添加一个用户
        smbpasswd -a username # 用户必须是系统用户
        在windows中使用该用户登录网盘进入的是 /home/username
    4.开启服务和检查状态
        [root@bogon samba]# systemctl start smb
        [root@bogon samba]# systemctl status smb
    5.
        使用时在windows端 右键计算机--->映射网络驱动器--->
        \\192.168.1.120\company(填写网盘名)
        --->勾选使用其他凭据连接

netstat -an | grep 8000
netstat -tulnp 查看端口

通过密钥对免密连接:
    持有私钥的主机可以连接持有公钥的主机但不可反向
    密钥对保存在当前用户   ~/.ssh  中
    举例: A(root@192.168.145.137)主机连接B(why@192.168.145.138)主机
        1.在A主机上安装ssh-keygen
        2.在命令行执行ssh-keygen
            [root@why_centos7 .ssh]# ssh-keygen
            Generating public/private rsa key pair.
            Enter file in which to save the key (/root/.ssh/id_rsa): 
            Created directory '/root/.ssh'.
            Enter passphrase (empty for no passphrase):                                                        # 
            Enter same passphrase again: 
            Your identification has been saved in /root/.ssh/id_rsa.
            Your public key has been saved in /root/.ssh/id_rsa.pub.
            The key fingerprint is:
            SHA256:DfdqGXHduJjXFHuXFDMh8S61bLg+MiFVZyo0ULDfG9o root@why_centos7
            The key's randomart image is:
            +---[RSA 2048]----+
            |         o+. o.Bo|
            |          .o o+=*|
            |        ..+ + *==|
            |         +.*.+=++|
            |        S +.==o=.|
            |         . =o.*  |
            |          =..E   |
            |         . o..   |
            |            o..  |
            +----[SHA256]-----+
        3.将公钥发送到B主机上 有不同的发送方法这里使用ssh-copy-id     还有scp id_rsa.pub root@172.16.22.132:/root/.ssh 
            [root@why_centos7 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p 22 why@192.168.145.137 
            /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
            /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
            /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
            why@192.168.145.137's password: 

            Number of key(s) added: 1

            Now try logging into the machine, with:   "ssh -p '22' 'why@192.168.145.137'"
            and check to make sure that only the key(s) you wanted were added.
        
        4.免密连接
            ssh -p '22' 'why@192.168.145.137'


配置开关机
在家目录下有两个文件 .bashrc、.bash_logout,前者是开机执行,后者是关机执行

    
    

 

posted @ 2020-06-13 20:20  pywhy  阅读(156)  评论(0编辑  收藏  举报