如何在 Linux 系统上创建软连接和硬连接 ?
nanshan 2024-10-25 13:11 20 浏览 0 评论
在本指南中,我将介绍什么是链接以及如何更好地使用它们以及所需的概念。
通过执行 man ln 命令,可以看到这是在文件之间建立链接,而没有提及是软链接或硬链接。
shashi@linuxtechi ~}$ man ln
类似地,命令 man link 描述为调用链接功能创建文件。
Soft Link
软链接顾名思义就是创建到新文件的新链接。在这种情况下,新文件的节点号将指向旧文件。
Hard Link
在这种情况下,旧文件和新文件都将指向相同的索引节点号。
Symbolic Link
在某些 Unix/Linux 系统中,符号和软链接都被视为相同的。但是实际的差异是,新的文件和旧文件将指向一个新的 Inode 号码,这将完全取决于实施。
注意: 在许多情况下,符号和软链接术语可以互换使用,但是人们必须知道什么时候用什么。
Creating Hard Link
(1) man ln 命令输出如下
shashi@linuxtechi ~}$ man ln
ln - make links between files
(2) ln 命令无参数,输出如下
shashi@linuxtechi ~}$ln
ln: missing file operand
Try 'ln --help' for more information.
(3) 在两个文件之间创建硬链接,首先检查文件是否存在,否则将创建文件,然后链接它们。
shashi@linuxtechi ~}$ ls -la
total 24
drwx------ 4 root root 4096 Feb 6 15:23 .
drwxr-xr-x 23 root root 4096 Jan 25 16:39 ..
-rw-r--r-- 1 root root 3122 Jan 25 16:41 .bashrc
drwx------ 2 root root 4096 Feb 6 15:23 .cache
-rw-r--r-- 1 root root 0 Jan 25 16:41 .hushlogin
-rw-r--r-- 1 root root 148 Aug 17 2015 .profile
drwxr-xr-x 2 root root 4096 Jan 25 16:41 .ssh
(4) 使用 touch 命令创建文件
shashi@linuxtechi ~}$ touch 123.txt
shashi@linuxtechi ~}$ ls -l
total 0
-rw-r--r-- 1 root root 0 Feb 6 15:51 123.txt
(5) 使用 cat 命令将内容输入到文件中,然后按 ctrl+c 保存并退出。
shashi@linuxtechi ~}$ cat > 123.txt
Welcome to this World!
^C
(6) 创建 123.txt 和 321.txt 文件之间的硬链接
shashi@linuxtechi ~}$ ln 123.txt 321.txt
shashi@linuxtechi ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt
-rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt
(7) 检查文件的索引节点号
shashi@linuxtechi ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt
794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt
$ cat 321.txt
Welcome to this World!
(8) 再创建一个名为 456.txt 的文件,并使用 ln 命令将其链接到 321.txt
shashi@linuxtechi ~}$ ls -li
total 12
794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 123.txt
794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 321.txt
794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 456.txt
$ cat 456.txt
Welcome to this World!
shashi@linuxtechi ~}$ ls -l
total 12
-rw-r--r-- 3 root root 23 Feb 6 15:52 123.txt
-rw-r--r-- 3 root root 23 Feb 6 15:52 321.txt
-rw-r--r-- 3 root root 23 Feb 6 15:52 456.txt
(9) 当源文件或这些文件中的任何一个被删除时,它不会影响其他文件。
shashi@linuxtechi ~}$ rm 123.txt
shashi@linuxtechi ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt
shashi@linuxtechi ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt
794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt
shashi@linuxtechi ~}$ cat 456.txt
Welcome to this World!
(10) 不允许跨目录创建硬链接。
shashi@linuxtechi ~}$ls -l
total 8
-rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt
shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln abc def
ln: abc: hard link not allowed for directory
(11) 对一个文件内容的任何更改都将影响并相应地更改其他文件的内容。
shashi@linuxtechi ~}$ vi 321.txt
Welcome to this World!
You are welcomed to this new world
:wq
shashi@linuxtechi ~}$ ls -l
total 12
-rw-r--r-- 2 root root 59 Feb 6 16:24 321.txt
-rw-r--r-- 2 root root 59 Feb 6 16:24 456.txt
drwxr-xr-x 2 root root 4096 Feb 6 16:18 abc
shashi@linuxtechi ~}$ cat 456.txt
Welcome to this World!
You are welcomed to this new world
Creating Soft Link:
(1) 使用 touch 命令创建文件 src.txt,使用 cat 命令输入内容,然后按 ctrl+c 保存并退出。
shashi@linuxtechi ~}$ touch src.txt
shashi@linuxtechi ~}$ cat > src.txt
Hello World
^C
shashi@linuxtechi ~}$ ls -l
total 4
-rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(2) 将目标文件创建为 dst.txt,并使用 ln -s 命令创建符号链接 (也称为软链接)。查看 dst.txt 文件的内容,可以看到与 src.txt 相同的内容。
shashi@linuxtechi ~}$ ln -s src.txt dst.txt
shashi@linuxtechi ~}$ ls -l
total 4
lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
shashi@linuxtechi ~}$ cat dst.txt
Hello World
(3) 在符号链接的情况下,源文件和目标文件的 inode 号不同。在权限中出现字母 l,表明这些是链接。dst.txt ->src.txt 将是现在建立的新链接。
shashi@linuxtechi ~}$ ls -li
total 4
794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(4) 允许对目录进行符号创建
shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln -s abc def
$ ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc
lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc
lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(5) 源和目标的 Inode 编号不同
shashi@linuxtechi ~}$ ls -li
total 8
794585 drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc
794586 lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc
794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(6) 如前所述,可以为目录创建符号链接。一旦创建了这些带有符号链接的目录,就可以在这些目录中创建文件。当在源目录中创建文件时,同样的情况也会反映在目标目录中。
shashi@linuxtechi ~}$ $ cd abc
shashi@linuxtechi ~}$ touch 123.txt
shashi@linuxtechi ~}$ vi 123.txt
Hello
:wq!
shashi@linuxtechi ~}$ touch 456.txt
shashi@linuxtechi ~}$ cd ..
shashi@linuxtechi ~}$ ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb 6 16:36 abc
lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc
lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
shashi@linuxtechi ~}$ cd def
shashi@linuxtechi ~}$ ls -l
total 4
-rw-r--r-- 1 root root 6 Feb 6 16:37 123.txt
-rw-r--r-- 1 root root 0 Feb 6 16:36 456.txt
shashi@linuxtechi ~}$ cat 123.txt
Hello
Removing Soft Link / Symbolic Links
使用 rm 和 unlink 命令删除软链接或符号链接
# rm <soft-link-filename>
# unlink <soft-link-filename>
删除软链接目录
# rm <soft-link-directory>
# unlink <soft-link-directory>
我的开源项目
- course-tencent-cloud(酷瓜云课堂 - gitee 仓库)
- course-tencent-cloud(酷瓜云课堂 - github 仓库)
相关推荐
- ssh终端xshell日志查看命令(xshell怎么看日志)
-
现在我们云服务器运维较多用的是SSH工具,其中常用的包括PUTTY、XSHELL等,其实大同小异界面UI稍微不同,但是都可以进入远程连接。这里有朋友提到如何查看服务器的日志文件,这个其实和是否使用XS...
- 使用 Fail Ban 日志分析 SSH 攻击行为
-
通过分析`fail2ban`日志可以识别和应对SSH暴力破解等攻击行为。以下是详细的操作流程和关键分析方法:---###**一、Fail2ban日志位置**Fail2ban的日志路径因系统配置...
- 如何高效读取Linux日志文件?这些命令要熟记于心!
-
在Linux系统中,日志文件通常存储在/var/log目录下。比如,/var/log/syslog(或/var/log/messages,视发行版而定)记录系统整体事件,/var/log/a...
- Windows服务器远程登录日志查询方法,linux查看登录日志方法
-
概述本文介绍Windows、Linux服务器查询系统的远程登录日志方法。根据服务器所使用的操作系统不同,有以下两种查询方法。Linux操作系统的登录日志查询通过远程连接登录Linux服务器,使用roo...
- iptables防火墙如何记录日志(防火墙日志查看)
-
例如:记录所有ssh服务的登录的日志首先,我们需要了解如何将所有的iptables的INPUT链数据包记录到/var/log/messages中。如果你已经有一些iptables规则了,那么将记录日志...
- 如何安全管理SSH密钥以防止服务器被入侵
-
SSH密钥安全管理实施指南(2025年更新版)一、密钥生成与存储规范高强度密钥生成bashCopyCodessh-keygen-ted25519-a100#生成ED25519算法密钥(比...
- 在CentOS上安装nginx服务器(centos搭建代理服务器)
-
一、环境描述1.虚拟机配置CPU:单核内存:2GB硬盘:120GBIP:10.24.17.1082.操作系统版本:CentOS6.6x86_64安装方式:Minimal3.虚拟化环境VM...
- CentOS7安全加固的一份整理规划建议
-
◆更新系统:及时更新CentOS7操作系统版本和安全补丁,确保系统以最新状态运行。◆关闭不必要的服务:在运行系统时,应关闭不需要的服务和端口,以减少系统暴露的攻击面。◆安装防火墙:使用iptables...
- 第四十七天-二叉树,centOS安装tomcat,Maven,vsftpd
-
学习笔记:1.Maven是Apache下的一个纯Java开发的开源项目。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。Maven...
- Linux远程桌面连接使用教程 Widows终端远程连接Linux服务器
-
一、前言为什么不是远程连接Linux服务器?因为我不会,远程连接window我就用电脑自带的“远程桌面连接”。以下所述都是在CentOS操作系统下的。服务器刚换成Linux的时候很迷茫,感觉无从下手...
- CentOS 安全加固操作,保护你的操作系统
-
系统加固是保障系统安全的重要手段,对于维护企业数据安全、用户隐私以及系统稳定运行具有重要意义。加固后的系统更加健壮和稳定,能够有效减少因安全问题导致的系统故障和停机时间,提高系统的可用性和可靠性。通过...
- Dockerfile部署Java项目(docker如何部署java项目)
-
1、概述本文主要会简单介绍什么是Docker,什么是Dockerfile,如何安装Docker,Dockerfile如何编写,如何通过Dockerfile安装jar包并外置yaml文件以及如何通过do...
- CentOS7云主机部署Fail2ban阻断SSH暴力破解
-
关于Fail2banFail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽)例如:当有人在试探你的HTTP、SSH、SMTP、FTP密...
- 在CentOS7上用源码编译安装PostgreSQL
-
1、新建postgres用户#useraddpostgres&&passwdpostgres2、安装依赖包#yum-yinstallmakegccgcc-c++readline...
- pure-ftpd 使用(ftp prompt命令)
-
pure-ftpd是一个免费的ftp软件,其他介绍就不多说了。我们直接开始主题安装centosyuminstallepel-releaseyuminstallpure-ftpd配置备份原配置...
你 发表评论:
欢迎- 一周热门
-
-
如何在安装前及安装后修改黑群晖的Mac地址和Sn系列号
-
爱折腾的特斯拉车主必看!手把手教你TESLAMATE的备份和恢复
-
[常用工具] OpenCV_contrib库在windows下编译使用指南
-
Ubuntu系统Daphne + Nginx + supervisor部署Django项目
-
WindowsServer2022|配置NTP服务器的命令
-
WIN11 安装配置 linux 子系统 Ubuntu 图形界面 桌面系统
-
解决Linux终端中“-bash: nano: command not found”问题
-
NBA 2K25虚拟内存不足/爆内存/内存占用100% 一文速解
-
Linux 中的文件描述符是什么?(linux 打开文件表 文件描述符)
-
K3s禁用Service Load Balancer,解决获取浏览器IP不正确问题
-
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)