當前位置:首頁 > IT技術(shù) > 其他 > 正文

Centos-基礎(chǔ)操作
2022-05-11 11:00:33

配置yum源

sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
sudo yum makecache

初始化配置

shell腳本

查看端口占用

# 網(wǎng)絡(luò)工具netstat
yum install net-tools -y
# 查看監(jiān)聽的端口
netstat -lnpt
# 查看端口占用
netstat -lnpt |grep 5672

防火墻常用命令

# 查看firewall服務(wù)狀態(tài)
systemctl status firewalld
# 查看firewall運行狀態(tài)
firewall-cmd --state
# 開啟
service firewalld start
# 重啟
service firewalld restart
# 關(guān)閉
service firewalld stop
# 查看防火墻規(guī)則
firewall-cmd --list-all
# 查詢端口是否開放
firewall-cmd --query-port=8080/tcp
# 開放80端口 常用80,443,22,21,3306,8080
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
# 重啟防火墻(修改配置后要重啟防火墻)
firewall-cmd --reload
# 參數(shù)解釋
1、firwall-cmd:是Linux提供的操作firewall的一個工具;
2、--permanent:表示設(shè)置為持久;
3、--add-port:標識添加的端口;

修改主機名/DNS

# 查看主機信息
hostnamectl
# 設(shè)置主機名
hostnamectl set-hostname xxx

查看DNS

cat /etc/resolv.conf

VIM

# 搜索xxx
/xxx
取消搜索結(jié)果高亮
:noh

SED

添加
# 在第1行的上一行追加內(nèi)容
sed -i 1i追加內(nèi)容 index.conf
# 在第1行的下一行追加內(nèi)容
sed -i 1a追加內(nèi)容 index.conf

# 正則匹配
# 在匹配到的行的上/下一行追加內(nèi)容
sed -i '/內(nèi)容/i222' index.conf
sed -i '/內(nèi)容/a222' index.conf

# 在最后一行添加內(nèi)容
sed -i '$a內(nèi)容' index.conf

刪除
# 刪除第1行
sed -i 1d index.conf
# 從第一行開始刪除,每隔2行就刪掉一行,即刪除奇數(shù)行
sed -i '1~2d' index.conf
# 刪除1~2行
sed -i '1,2d' index.conf
 #刪除1~2之外的所有行
sed -i '1,2!d' index.conf
 #刪除最后一行
sed -i '$d' index.conf  
#刪除空行
sed -i '/^$/d' index.conf   
#刪除匹配到
sed -i '/123/d' index.conf 

替換
# 替換第一行的內(nèi)容
sed -i 1c替換內(nèi)容 index.conf

# 正則匹配
# 替換匹配到的行全部內(nèi)容
sed -i '/111/c222' index.conf
# 替換最后一行
sed -i '$c333' index.conf

建議使用'' 包括內(nèi)容

SCP

scp -P 端口 c://xxxx.txt user@ip:/home/root

1、從服務(wù)器上下載文件
scp username@servername:/path/filename /var/www/local_dir(本地目錄)

 例如scp root@192.168.0.101:/var/www/test.txt  把192.168.0.101上的/var/www/test.txt 的文件下載到/var/www/local_dir(本地目錄)


2、上傳本地文件到服務(wù)器
scp /path/filename username@servername:/path   

例如scp /var/www/test.php  root@192.168.0.101:/var/www/  把本機/var/www/目錄下的test.php文件上傳到192.168.0.101這臺服務(wù)器上的/var/www/目錄中

3、從服務(wù)器下載整個目錄
scp -r username@servername:/var/www/remote_dir/(遠程目錄) /var/www/local_dir(本地目錄)

例如:scp -r root@192.168.0.101:/var/www/test  /var/www/  

4、上傳目錄到服務(wù)器
scp  -r local_dir username@servername:remote_dir
例如:scp -r test  root@192.168.0.101:/var/www/   把當前目錄下的test目錄上傳到服務(wù)器的/var/www/ 目錄

SSH免密

本地機器執(zhí)行 ssh-keygen -t rsa 命令,生成密鑰文件其中id_rsa為私鑰文件,id_rsa.pub為公鑰文件

本地機器執(zhí)行命令如:ssh-copy-id -i ~/.ssh/id_rsa.pub root@服務(wù)器IP, 將公鑰文件傳輸?shù)倪h程機器,并生效

查看遠程機器的~/.ssh/authorized_keys文件,可以看到對應(yīng)的變化:本地機器的公鑰已經(jīng)增加到遠程機器的配置文件中了。

如果你現(xiàn)在以本地的root身份生成密鑰并上傳到服務(wù)器的話,你用root去SSH連接服務(wù)器的話是不需要密碼的,但是別的用戶連接是仍然需要密碼的:
因為免密碼登錄的處理是用戶對用戶的,切換其他用戶后,仍然需要輸入密碼,這個時候可以用別的用戶再次生成密鑰上傳到服務(wù)器就行。

公鑰傳到遠程機器并生效的操作,可用其他方式實現(xiàn),如scp后修改authorized_keys。
遠程機器的.ssh目錄需要700權(quán)限,authorized_keys文件需要600權(quán)限。

Cron

root用戶下 輸入 crontab -l 顯示no crontab for root
這個問題非常簡單,同樣在 root 用戶下輸入 crontab -e
按 Esc 按: wq 回車
在輸入 crontab -l 就沒有問題了
主要原因是由于這個liunx服務(wù)器 第一次使用 crontab ,還沒有生成對應(yīng)的文件導(dǎo)致的,執(zhí)行了 編輯后 就生成了這個文件

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >