Linux复习笔记:05.系统配置
2024/11/24大约 2 分钟
ln命令
语法
ln [-s] src_file dist_file参数说明
-ssoft软链接- src_file 文件或文件夹的绝对路径
- dist_file 文件或文件夹的绝对路径或相对路径
案例1:软链接
root@iZj6cin9q6x5p5bh4ofx79Z:~# mkdir temp1/temp2 -p
root@iZj6cin9q6x5p5bh4ofx79Z:~# ln -s /root/log.txt temp1/temp2/log.txt
root@iZj6cin9q6x5p5bh4ofx79Z:~# tree temp1/
temp1/
└── temp2
└── log.txt -> /root/log.txt
1 directory, 1 file
root@iZj6cin9q6x5p5bh4ofx79Z:~# cat temp1/temp2/log.txt
hello world
root@iZj6cin9q6x5p5bh4ofx79Z:~# rm log.txt
root@iZj6cin9q6x5p5bh4ofx79Z:~# cat temp1/temp2/log.txt
cat: temp1/temp2/log.txt: No such file or directory案例2:硬链接
root@iZj6cin9q6x5p5bh4ofx79Z:~# ln /root/log.txt temp1/temp2/log.txt
root@iZj6cin9q6x5p5bh4ofx79Z:~# tree ./
./
├── log.txt
├── main.txt
└── temp1
└── temp2
└── log.txt
root@iZj6cin9q6x5p5bh4ofx79Z:~# rm log.txt
root@iZj6cin9q6x5p5bh4ofx79Z:~# tree ./
./
├── main.txt
└── temp1
└── temp2
└── log.txt
root@iZj6cin9q6x5p5bh4ofx79Z:~# cat temp1/temp2/log.txt
hello world
root@iZj6cin9q6x5p5bh4ofx79Z:~# rm log.txt
root@iZj6cin9q6x5p5bh4ofx79Z:~# cat temp1/temp2/log.txt
hello world时间、时区、同步
date命令
语法格式
date [-d] [+格式化字符串]选项说明
-d用于做日期计算- 支持
yearmonthhourminutesecond - 示例:
date -d "+1 hour"date -d "-1 hour"
- 支持
+格式化字符串控制输出时间格式- 支持
%Y0000年%y00年%m00月%d00日%H00时%M00分%S00秒%s时间戳->自1970-01-01 00:00:00 UTC到现在的秒数%::z时区时间 如+08:00:00%Z时区字符串 如CSTUTC
- 支持
示例
######################################################
root@iZj6cin9q6x5p5bh4ofx79Z:~# date
Sun 24 Nov 2024 04:12:22 PM CST
######################################################
root@iZj6cin9q6x5p5bh4ofx79Z:~# date "+%Y-%m-%dT%H:%M:%S"
2024-11-24T16:13:26
######################################################
root@iZj6cin9q6x5p5bh4ofx79Z:~# date -d "+1 year" "+%Y-%m-%dT%H:%M:%S"
2025-11-24T16:14:23
######################################################
root@iZj6cin9q6x5p5bh4ofx79Z:~# date -d "-1 year" "+%Y-%m-%dT%H:%M:%S"
2023-11-24T16:14:28
######################################################修改时区
root@iZj6cin9q6x5p5bh4ofx79Z:~# rm /etc/localtime
root@iZj6cin9q6x5p5bh4ofx79Z:~# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@iZj6cin9q6x5p5bh4ofx79Z:~# ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Sep 14 2020 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghaintp自动对时
安装启动
# centos
yum -y install ntp
# debian
apt -y install ntp ntpdate启动
# ntpd启动后会定期自动联网校准系统时间
systemctl start ntp
systemctl enable ntp手动对时
root@iZj6cin9q6x5p5bh4ofx79Z:~# ntpdate -u ntp.aliyun.com
24 Nov 16:31:52 ntpdate[616757]: adjust time server 203.107.6.88 offset 0.011946 sec修改主机名
查看主机名
root@iZj6cin9q6x5p5bh4ofx79Z:~# hostname
iZj6cin9q6x5p5bh4ofx79Z修改主机名
root@iZj6cin9q6x5p5bh4ofx79Z:~# hostnamectl set-hostname dingdingdang.online
root@iZj6cin9q6x5p5bh4ofx79Z:~# su
root@dingdingdang:~# hostname
dingdingdang.online## 本地地址记录
hosts文件
- Windows
c:\Windows\System32\drivers\etc\hosts - Linux
/etc/hosts
root@ubuntu2204:~# sudo cat /etc/hosts
127.0.0.1 localhostip配置
centos配置静态ip
适用于 centos7
编辑:/etc/sysconfig/network-scripts/ifcfg-xxx
sudo vi /etc/sysconfig/network-scripts/ifcfg-ens33
# 修改内容:
ONBOOT=yes
BOOTPROTO=static
# 添加内容:
IPADDR=192.168.1.2
# NETWORK=192.168.1.0
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=223.5.5.5
DNS2=8.8.8.8
# BROADCAST=192.168.1.255
# HWADDR=00:01:02:03:04:05
# PREFIX=24重启网络
systemctl restart network