# linux note
> [TOC]
>
> [time=Tue, Oct 24, 2023 3:47 PM]
## 更改主機名稱
hostname 主機名稱
```linux=
hostnamectl set-hostname mycentos7-1(名稱)
```
bash 刷新
## 檢查伺服器運作
```linux=
systemctl (xxx) sshd
```
status 顯示指定的系統服務狀態
start 開啟
stop 停止
restart 重啟
reload 重新加載
disable 關閉開機時自動啟動
enable 開機時自動啟動
---
**0.0.0.0 是指任意介面的位址**
---
## 連線失敗
### 1. 檢查server是否runing
```linux=
systemctl status server(sshd)
```
### 2. 檢查埠號
```linux=
netstat tulnpi grep ssh
```
正常為22號埠
### 3. 檢查selinux
```linux=
getenforce
```
若為Enforcing,需要編輯檔案:
```linux=
vi /etc/selinux/config
```

找到以下一行:
SELINUX=Enforcing
改成:
SELINUX=Disabled
### 4. 檢查firewalld是否關閉
```linux=
systemctl status firewalld
```
```linux=
systemctl stop firewalld
```
## 基礎指令功能
### echo 用於字串的輸出
#### 單引號與雙引號
在使用**echo**輸出字符串的時候, 會用引號(單/雙引號)將字符串括起來,在沒有**特殊字符如$等**的時候,只是普通文本內容,使用單/雙引號沒有區別,下面說下不同的地方。
##### 單引號
如果字符串中存在**特殊字符**,想要作爲普通字符原樣輸出就使用單引號,這樣就不會把後面的**特殊字符**作爲變量解釋輸出
##### 雙引號
在對待**特殊字符**上,剛好和單引號的處理相反,對於**特殊字符**緊跟的字符串作爲引用的變量解釋

### 更改ens

```linux=
vim /etc/resolv.conf
```
## NFS 課程日期10/3
### NFS

















### 參考
https://qizhanming.com/blog/2018/08/08/how-to-install-nfs-on-centos-7
## VPN server 課程日期11/14
---
金門大學vpn server:60.248.63.195








vim /etc/pptpd.conf =>

vim /etc/ppp/chap-secrets =>





### 參考
> [配置PPTP VPN服务端到客户端]https://help.aliyun.com/zh/ecs/how-do-i-configure-a-connection-between-a-pptp-vpn-server-and-a-pptp-vpn-client-on-a-centos-7-instance
> [pptpd]https://github.com/stereomp3/note/blob/main/linux/111semester01/7-VPN.md#pptpd
## Linux 建立自訂 Systemd 服務 課程日期11/28
安裝python3:
```liunx=
yum install python3
```
1. 編輯/opt/echo_server.py :
```linux=
gedit /opt/echo_server.py
```
2. 撰寫一個 echo 伺服器 :
```python=
#!/usr/bin/env python3
import socket
# 建立 socket
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 綁定所有網路介面的 9000 連接埠
serv.bind(('0.0.0.0', 9000))
# 開始接受 client 連線
serv.listen()
while True:
# 接受 client 連線
conn, addr = serv.accept()
print('Client from', addr)
while True:
# 接收資料
data = conn.recv(1024)
# 若無資料則離開
if not data: break
# 傳送資料
conn.send(data)
conn.close()
print('Client disconnected')
```
3. 建立好 /opt/echo_server.py 之後,開啟執行權限:
```liunx=
chmod +x /opt/echo_server.py
```
測試 echo 伺服器:
```liunx=
/opt/echo_server.py
```
使用 nc 指令連線至 9000 連接埠:
```liunx=
yum install nc
nc localhost 9000
```
4. 建立 Systemd 服務單位設定檔
建立一個新的 Systemd 服務單位設定檔/etc/systemd/system/echo_server.service:
```liunx=
gedit /etc/systemd/system/echo_server.service
```
```/etc/systemd/system/echo_server.service
[Unit]
Description=Echo Server
[Service]
Type=simple
ExecStart=/opt/echo_server.py
Restart=always
[Install]
WantedBy=multi-user.target
```
權限要設定為 644:
```liunx=
sudo chmod 644 /etc/systemd/system/echo_server.service
```
如果在開發過程中,有修改過 Systemd 的服務單位設定檔,記得重新載入 daemon 讓新設定生效:
```liunx=
# 重新載入 Systemd 設定檔
sudo systemctl daemon-reload
```
接著就可以使用 systemctl 指令啟動自訂的 echo 伺服器:
```liunx=
# 啟動自訂的 echo 伺服器
sudo systemctl start echo_server
```
查看 echo 伺服器的狀態:
```liunx=
# 查看 echo 伺服器狀態
systemctl status echo_server
```
```
● echo_server.service - Echo Server
Loaded: loaded (/etc/systemd/system/echo_server.service; disabled; vendor pre
Active: active (running) since Thu 2019-09-19 06:59:46 UTC; 1min 46s ago
Main PID: 20142 (python3)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/echo_server.service
└─20142 python3 /opt/echo_server.py
Sep 19 06:59:46 test-vm systemd[1]: Started Echo Server.
```
### 參考
> [Systemd服務教學]https://blog.gtwang.org/linux/linux-create-systemd-service-unit-for-python-echo-server-tutorial-examples/
### 安装”Telnet“服务
#### 安装Telnet服务前需要检测centOS系统是否安装了xinetd和tenkbet-server
```
rpm-qa telnet-server
rpm-qa xinetd
```
#### 如果没有安装,则开始安装
```
yum-y install telnet-server
yum-y install telnet
yum-y install xinetd
```
#### 进行配置安装好的“telnet”想要启动telnet以及xinetd必须设置开启后自动启动
```
systemctl enable xinetd.service
systemctl enable telnet.socket
```
#### 接下来启动服务
```
systemctl start telnet.socket
systemctl start xinetd
```
#### 配置防火墙规则,或者关闭防火墙
```
firewall-cmd--permanent--add-port=23/tcp
firewall-cmd--reload
systemctl restart iptables
systemctl disable firewalld
systemctl stop firewalld
```
#### 默认下的root是无法进行远程访问的,所以首先需要修改配置文件,文件名称为”/etc/securetty“以下为修改方法:
而且centos7下面安装telnet没有生成/etc/xinetd.d/telnet文件
vi/etc/securetty
在末尾添加
pst/0
pst/1
保存退出,在windows系统内使用cmd或putty工具测试“telnet”是否生成
#### 查看日志
```
tail-f/var/log/secure
```
### 參考
> [安装”Telnet“服务]https://cloud.tencent.com/developer/article/2274078
## LAMP 課程日期12/5
更新centos套件
```liunx=
$ sudo yum update
```
---
### Apche
安裝Apache
```liunx=
$ sudo yum install httpd
$ sudo systemctl start httpd.service
```
關閉防火牆
```liunx=
$ setenforce 0
$ systemctl stop firewalld
```
```liunx=
$ vim /etc/selinux/config
```
把SELINUX改成disable,然後重新開機
```
SELINUX = disable
```
檢查配置檔案,可以改Listen把port改成想要的位置
```liunx=
$ cat /etc/httpd/conf/httpd.conf | grep Listen
```
安裝完成後,在瀏覽器測試是否運行
http://your_server_IP_address/

---
### mysql
install
```liunx=
$ sudo yum install mariadb-server mariadb
```
啟動MariaDB
```liunx=
$ sudo systemctl start mariadb
```
設定root密碼,除了設定root外,其他選項Disallow root和Remove test選n,其他都Enter跳過
```liunx=
$ sudo mysql_secure_installation
```
設定開機啟動MariaDB
```liunx=
$ sudo systemctl enable mariadb.service
```
open sql
```liunx=
$ mysql -u root -p
```
sql command
#### db(資料庫)===>table(資料表)===>record(資料表裡面的資料)===>field(資料欄位col)
```
/* 顯示目前有的資料庫 */
show databases;
/* 創建資料庫 */
create database testdb;
/* 使用資料庫 */
use testdb;
/* 創建資料表 */
create table addrbook(name varchar(50) not null, phone char(10));
/* 加入資料 */
insert into addrbook(name, phone) values ("tom", "0912123456");
insert into addrbook(name, phone) values ("mary", "0912123567");
/* 選擇資料 */
select name,phone from addrbook;
/* */
update addrbook set phone="0987465123"
```

---
### PHP
install
```liunx=
$ sudo yum install php php-mysql php-fpm
```liunx=
重新啟動Apache,讓PHP運作
```liunx=
$ sudo systemctl restart httpd.service
```
測試PHP是否正常運作,建立 info.php 在/var/www/html/
```liunx=
$ sudo vim /var/www/html/info.php
```
info.php
```
<?php phpinfo(); ?>
```
http://your_server_IP_address/info.php
輸入後你將會看到此頁面

重新啟動 Apache
```liunx=
$ sudo systemctl restart httpd.service
```
SQL和PHP同一台主機上,所以servername寫127.0.0.1,不用寫其他名字或IP
如果無法連線到SQL,要設定:
```liunx=
$ setsebool httpd_can_network_connect_db=1
```
```
#vim /var/www/html/info.php
<?php
$servername="127.0.0.1";
$username="root";
$password="centos";
$dbname="testdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
else{
echo "connect OK!" . "<br>";
}
$sql="select name,phone from addrbook";
$result=$conn->query($sql);
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
echo "name: " . $row["name"] . "\tphone: " . $row["phone"] . "<br>";
}
} else {
echo "0 record";
}
?>
```
### 參考
> [Apache,mysql,PHP]
> https://github.com/stereomp3/note/blob/main/linux/111semester01/13-.md
## ipv6 課程日期12/12
### 架設網頁伺服器 前置

### https










# 腳本
### 比較兩數


### 四則運算
a+b=c
a-b=d
a*b=e
a/b=f

