您的位置:首页 > 运维架构 > Linux

Linux课程第六天学习笔记

2016-10-18 17:40 417 查看
#############################
##### 12.不同系统之间的文件传输 #####
#############################

####################
[root@foundation50 Desktop]# scp -r /etc/ root@172.25.50.100:/mnt/ ##"-r"拷贝目录
root@172.25.50.100's password:
...... ##速度太慢,按"ctrl+c"强行中断
[root@foundation50 Desktop]# du -sh /etc/ ##查看文件或目录的大小
36M /etc/
[root@foundation50 Desktop]# tar cf etc.tar /etc/ ##把"/etc/"进行归档
tar: Removing leading `/' from member names
--------------------------------------------------------------------------------
注意:如果不去掉"/",那么打包的就是绝对路径。解压时会把"/etc"覆盖,所以必须要去掉"/"。
并且如果是绝对路径,解压时就不能再指定路径。
--------------------------------------------------------------------------------
[root@foundation50 Desktop]# ls
etc.tar
[root@foundation50 Desktop]# du -sh etc.tar ##查看文件或目录的大小
32M etc.tar
[root@foundation50 Desktop]# scp -r etc.tar root@172.25.50.100:/mnt/ ##"-r"拷贝目录
root@172.25.50.100's password:
etc.tar 100% 32MB 31.7MB/s 00:00 ##几乎瞬间就完成
[root@foundation50 Desktop]# ssh root@172.25.50.100
root@172.25.50.100's password:
Last login: Fri Oct 14 03:07:01 2016 from 172.25.50.250
[root@localhost ~]# ls /mnt/
etc etc.tar
[root@localhost ~]# rm -fr /mnt/*
[root@localhost ~]# ls /mnt
####################

####################1.文件归档####################
1.文件归档,就是把多个文件变成一个归档文件

2.
tar c ##创建
f ##指定归档文件名称
t ##显示归档文件中的内容
r ##向归档文件中添加文件
--get ##取出单个文件
--delete ##删除单个文件
-x ##取出归档文件中的所有内容
-C ##指定解档目录
-z ##gz格式压缩
-j ##bz2格式压缩
-J ##xz格式压缩

可以使用"man tar"来查看用法

####################
[root@localhost Desktop]# touch file{1..5}
[root@localhost Desktop]# tar cf file.tar file{1..5}
[root@localhost Desktop]# ls
file1 file2 file3 file4 file5 file.tar
[root@localhost Desktop]# rm -fr file?
[root@localhost Desktop]# tar xf file.tar
[root@localhost Desktop]# ls
file1 file2 file3 file4 file5 file.tar
[root@localhost Desktop]# rm -fr file?
[root@localhost Desktop]# touch westos
[root@localhost Desktop]# tar rf file.tar westos
[root@localhost Desktop]# tar tf file.tar
file1
file2
file3
file4
file5
westos
[root@localhost Desktop]# ls
file.tar westos
[root@localhost Desktop]# tar f file.tar --get file1
[root@localhost Desktop]# tar tf file.tar
file1
file2
file3
file4
file5
westos
[root@localhost Desktop]# ls
file1 file.tar westos
[root@localhost Desktop]# tar f file.tar --delete westos
[root@localhost Desktop]# tar tf file.tar
file1
file2
file3
file4
file5
[root@localhost Desktop]# tar xf file.tar -C /mnt/
[root@localhost Desktop]# ls /mnt/
file1 file2 file3 file4 file5
####################

####################
[root@localhost Desktop]# touch file{1..5}
[root@localhost Desktop]# tar cvf file.tar file{1..5}
file1
file2
file3
file4
file5
"tar cvf" ##把文件归档并在输出栏列出
####################

####################2.压缩####################
gz
gzip etc.tar ##压缩成gz格式
gunzip etc.tar.gz ##解压gz格式压缩包
tar zcf etc.tar.gz /etc ##把文件归档为tar并压缩成gz
tar zxf etc.tar.gz ##解压并解挡gz格式压缩包

bz2
bzip2 etc.tar ##压缩成bz2格式
bunzip2 etc.tar.bz2 ##解压bz2格式压缩包
tar jcf etc.tar.bz2 /etc ##把文件归档为tar并压缩成bz2
tar jxf etc.tar.bz2 ##解压并解挡bz2格式压缩包

xz
xz etc.tar ##压缩成xz格式
unxz etc.tar.xz ##解压xz格式压缩包
tar Jcf etc.tar.xz /etc ##把文件归档为tar并压缩成xz
tar Jxf etc.tar.xz ##解压并解挡xz格式压缩包

zip
zip -r etc.tar.zip etc.tar ##压缩成zip格式
unzip etc.tar.zip ##解压zip格式压缩包

注意:zip打包
不能直接打包成".zip",要先归档成".tar"
打包以后,归档文件没有被删除
解包以后,打包文件没有被删除

####################
[root@localhost Desktop]# tar cf etc.tar /etc/
tar: Removing leading `/' from member names
[root@localhost Desktop]# ls
etc.tar
[root@localhost Desktop]# du -sh etc.tar
30M etc.tar

[root@localhost Desktop]# gzip etc.tar
[root@localhost Desktop]# ls
etc.tar.gz
[root@localhost Desktop]# du -sh etc.tar.gz
8.4M etc.tar.gz
[root@localhost Desktop]# gunzip etc.tar.gz
l[root@localhost Desktop]# ls
etc.tar

[root@localhost Desktop]# bzip2 etc.tar
[root@localhost Desktop]# ls
etc.tar.bz2
[root@localhost Desktop]# du -sh etc.tar.bz2
15M etc.tar.bz2
[root@localhost Desktop]# bunzip2 etc.tar.bz2
[root@localhost Desktop]# ls
etc.tar.gz

[root@localhost Desktop]# xz etc.tar
[root@localhost Desktop]# ls
etc.tar.xz
[root@localhost Desktop]# du -sh etc.tar.xz
7.9M etc.tar.xz
[root@localhost Desktop]# unxz etc.tar.xz
[root@localhost Desktop]# ls
etc.tar

[root@localhost Desktop]# zip -r etc.tar.zip etc.tar
adding: etc.tar (deflated 72%)
[root@localhost Desktop]# ls
etc.tar etc.tar.zip
[root@localhost Desktop]# du -sh etc.tar.zip
15M etc.tar.zip
[root@localhost Desktop]# rm -fr etc.tar
[root@localhost Desktop]# ls
etc.tar.zip
[root@localhost Desktop]# unzip etc.tar.zip
Archive: etc.tar.zip
inflating: etc.tar
[root@localhost Desktop]# ls
etc.tar etc.tar.zip
zip压缩和解压都不会删除原文件

[root@localhost Desktop]# rm -fr *
[root@localhost Desktop]# tar zcf etc.tar.gz /etc/
tar: Removing leading `/' from member names
[root@localhost Desktop]# ls
etc.tar.gz
[root@localhost Desktop]# tar jcf etc.tar.bz2 /etc/
tar: Removing leading `/' from member names
[root@localhost Desktop]# ls
etc.tar.bz2 etc.tar.gz
[root@localhost Desktop]# tar Jcf etc.tar.xz /etc/
tar: Removing leading `/' from member names
[root@localhost Desktop]# ls
etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]# file * ##查看那文件的类型
etc.tar.bz2: bzip2 compressed data, block size = 900k
etc.tar.gz: gzip compressed data, from Unix, last modified: Fri Oct 14 04:03:53 2016
etc.tar.xz: XZ compressed data

[root@localhost Desktop]# tar zxf etc.tar.gz
[root@localhost Desktop]# ls
etc etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]# rm -fr etc/
[root@localhost Desktop]# ls
etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]# tar jxf etc.tar.bz2
[root@localhost Desktop]# ls
etc etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]# rm -fr etc/
[root@localhost Desktop]# ls
etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]# tar Jxf etc.tar.xz
[root@localhost Desktop]# ls
etc etc.tar.bz2 etc.tar.gz etc.tar.xz
####################

####################3.系统中的文件传输####################
scp /dir/file username@ip:/dir ##上传
scp username@ip:/dir/file /dir ##下载

scp可以本机拷贝,也可以远程拷贝。“:”激活远程访问

rsync [参数] file username@ip:/dir
rsync -r ##同步目录
-l ##不忽略链接
-p ##不忽略文件权限
-t ##不忽略文件时间戳
-g ##不忽略文件所有组
-o ##不忽略文件所有人
-D ##不忽略设备文件

####################
ln是功能是为某一个文件在另外一个位置建立一个同不的链接
建立软链接:ln –s [源] [目的]。在选定的位置上生成一个文件的镜像,不占用磁盘空间,
建立硬链接:ln [源] [目的],没有参数-s。在选定的位置上生成一个和源文件大小相同的文件
无论是软链接还是硬链接,文件都会保持同步变化。
####################

####################
[root@localhost Desktop]# ll /dev/pts/
total 0
crw--w----. 1 root tty 136, 1 Oct 14 04:17 1
c---------. 1 root root 5, 2 Oct 13 21:33 ptmx ##发现块设备
[root@localhost Desktop]# rsync -r /dev/pts /mnt/
skipping non-regular file "pts/1"
skipping non-regular file "pts/ptmx"
[root@localhost Desktop]# rsync -Dr /dev/pts /mnt/
[root@localhost Desktop]# ll /mnt/pts
total 0
crw-------. 1 root root 136, 1 Oct 14 04:18 1
c---------. 1 root root 5, 2 Oct 14 04:18 ptmx ##块设备被同步了过来

[root@localhost Desktop]# rm -fr /mnt/*
[root@localhost Desktop]# rsync -Dr /dev/pts/ /mnt/
[root@localhost Desktop]# ls /mnt/
1 ptmx
注意:"/dev/pts/"是把目录下的文件拷贝出来,"/dev/pts"把目录本身和里面文件一起拷贝出来
####################

#################
#### 11.管理网络 ####
#################

####################1.ip基础知识####################
1.ipv4
2进制32位-----10进制

172.25.0.10/255.255.255.0
172.25.0.10:ip地址
255.255.255.0:子网掩码
子网掩码255位对应的ip位为网络位
子网掩马0位对应的ip位为主机位

####################
查看ip的命令:
1."ifconfig",如果某个网卡上配置了双IP,则使用"ifconfig"是看不到的
2."ip addr show",能看到所有的IP
####################

####################2.配置ip####################
1.图形化
nm-connection-editor

2.文本化
nmtui

<<命令>>
ifconfig 网卡 ip [netmask 子网掩码] ##临时设定,子网掩码可以不写

####################
[root@localhost Desktop]# ifconfig eth0 172.25.50.101 netmask 255.255.255.0
[root@localhost Desktop]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.50.101 netmask 255.255.255.0 broadcast 172.25.50.255 ##IP变成"101"
inet6 fe80::5054:ff:fe00:320a prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:32:0a txqueuelen 1000 (Ethernet)
RX packets 40231 bytes 392882507 (374.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 29748 bytes 3009571 (2.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@localhost Desktop]# systemctl restart network ##重启网络服务
[root@localhost Desktop]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.50.100 netmask 255.255.255.0 broadcast 172.25.50.255 ##IP变回"100"
inet6 fe80::5054:ff:fe00:320a prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:32:0a txqueuelen 1000 (Ethernet)
RX packets 40231 bytes 392882507 (374.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 29769 bytes 3012561 (2.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
####################

nmcli connection add type ethernet con-name westos ifname eth0 autoconnect yes
nmcli connection add type ethernet con-name westos ifname eth0 ip4 ip/24
nmcli connection delete westos
nmcli connection show
nmcli connection down westos
nmcli connection up westos
nmcli connection modify "westos" ipv4.addresses newip/24
nmcli connection modify "System eth0" ipv4.method <auto|manual>
nmcli device connect eth0
nmcli device disconnect eth0
nmcli device show
nmcli device status

####################
"add"和"modify"后记得"systemctl restart network"

nmcli connection down westos = nmcli device disconnect eth0
nmcli connection up westos = nmcli device connect eth0
nmcli device status = nmcli device

[root@localhost Desktop]# nmcli connection modify "System eth0" ipv4.
ipv4.addresses ipv4.dns ipv4.may-fail
ipv4.dhcp-client-id ipv4.dns-search ipv4.method
ipv4.dhcp-hostname ipv4.ignore-auto-dns ipv4.never-default
ipv4.dhcp-send-hostname ipv4.ignore-auto-routes ipv4.routes
[root@localhost Desktop]# nmcli connection modify "System eth0" ipv4.method --help
Error: failed to modify ipv4.method: '--help' not among [auto, link-local, manual, shared, disabled].
nmcli的好处是随时可以双击"Table",随时可以使用"--help"
####################

<<文件>>
dhcp ##动态获取
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 ##接口使用设备
BOOTPROTO=dhcp ##网卡工作模式
ONBOOT=yes ##网络服务开启时自动激活
NAME=eth0 ##网络接口名称
:wq
systemctl restart network

static|none ##静态网络
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 ##接口使用设备
BOOTPROTO=static|none ##网卡工作模式
ONBOOT=yes ##网络服务开启时自动激活
NAME=eth0 ##网络接口名称
IPADDR=172.25.0.100 ##IP地址
NETMASK=255.255.255.0 | PREFIX=24 ##子网掩码
:wq
systemctl restart network

如果要在网卡上设定多个IP(好像最多能设99个),不能用NETMASK,只能用PREFIX

"setup"命令 ##红帽6及以前的版本可以使用"setup"命令来配置ip,红帽7则不行

####################
[root@localhost Desktop]# systemctl restart network ##重启网络基本服务
[root@localhost Desktop]# systemctl restart NetworkManager ##重启网络智能管理服务

[root@localhost Desktop]# systemctl reload network
Job for network.service canceled.
重启网络服务不能使用reload
####################
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux 学习