日常开发基本都是在Linux上个进行的,有一个好的开发环境至关重要。rivergold整理一份关于Linux网络配置的笔记,主要包括以下内容:
防火墙配置
设置静态IP
代理配置
使用Flask搭建简易的文件服务器
NFS远程挂载
Linux防火墙命令
不同的Linux发行版有各自不同的防火墙命令,这里主要介绍两种:
Ubuntu:ufw
CentOS:firewall-cmd
安装
1 yum install firewalld firewall-config
启动
1 2 sudo systemctl enable firewalld.service sudo systemctl start firewalld.service
关闭
1 2 sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service
查看状态
1 sudo systemctl status firewalld
开启端口
基础使用
1 2 3 4 sudo ufw allow ssh sudo ufw allow 22/tcp sudo ufw allow 63200:63205/tcp
限制IP
1 2 3 4 sudo ufw allow from 192.168.1.5 sudo ufw allow from 192.168.1.5 to any port 80 sudo ufw allow from 192.168.1.5 to any port 80 proto tcp
简明教程|Linux中UFW的使用
1 2 firewall-cmd --zone=public --add-port=8050/tcp --permanent firewall-cmd --reload
关闭端口
删除规则
1 2 sudo ufw delete from 192.168.1.5 to any port 80 proto tcp
禁止端口
1 sudo ufw deny from 192.168.1.5 to any port 80 proto tcp
一般配置,只需执行
1 2 3 sudo apt install ufw sudo ufw enable sudo ufw default deny
之后根据个人需求在开启相关访问权限就可以了。
1 2 firewall-cmd --zone=public --remove-port=8050/tcp --permanent firewall-cmd --reload
IBM 学习: 使用 firewalld 构建 Linux 动态防火墙
Linux设置静态IP
新版本的Ubuntu(17.04以上)使用netplan
代替了/etc/network/interfaces
对网络进行管理。
创建netplan配置文件
netplan的配置文件放在/etc/netplan
目录下,配置文件采用yaml格式
1 2 3 cd /etc/netplancp 01-network-manager-all.yaml custom_static.yamlmv 01-network-manager-all.yaml 01-network-manager-all.yaml.backup
查看当前网卡和IP信息
1 2 3 4 5 $ ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link /ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff
该命令获取到了network interfaces信息,可以看到ens3(不同机器的名称会不一样)是正在使用的网卡,我们需要对其进行配置。在将其配置成静态IP前,我们需要获取当前的IP。
如果想重新获取新的IP地址,可以使用以下命令:
获取新IP地址
1 2 sudo dhclient -r sudo dhclient
stackoverflow: How do I request a new IP address from my DHCP server using Ubuntu Server?
修改配置文件
编辑之前创建的/etc/netplan/custom_static.yaml
文件
1 2 3 4 5 6 7 8 9 10 11 network: version: 2 renderer: networkd ethernets: enp0s31f6: dhcp4: false gateway4: <网关> addresses: - <IP地址> nameservers: addresses: [<dns1> ,<dns2> ,... ]
How to Configure Static IP Address on Ubuntu 20.04
Linux代理配置
使Linux能使用代理的方法有两种,一种为配置proxy环境变量,另一种为使用ProxyChains工具。
配置proxy环境变量
1 2 3 4 5 6 export http_proxy=http://127.0.0.1:7890export https_proxy=http://127.0.0.1:7890 unset http_proxyunset https_proxy
ProxyChains
ProxyChains 是一个开源的网络hook工具,支持scoks和HTTP代理
安装
rivergold倾向于从源码编译安装
1 git clone https://github.com/rofl0r/proxychains-ng.git
1 2 3 4 5 6 cd proxychains-ng./configure --prefix=/usr --sysconfdir=/etc make make install make install-config
配置
编辑/etc/proxychains.conf
1 2 3 4 5 socks5 <ip> <port> http <ip> <port>
使用
1 proxychains4 -q <your command >
-q
: makes proxychains quiet
Harker’ Blog: Centos 7 安装 Proxychains 实现 Linux 代理
基于Flask搭建简易文件服务器
由于经常需要对远程服务器进行上传和下载的操作,又不想安装过于复杂的软件,因此基于Flask及其插件卡开发了一个简易的文件服务器,基本满足日常需求。
基于Flask-AutoIndex可以使Flask支持文件目录展示功能,并可以进行下载
安装依赖
1 pip install flask flask_autoindex
文件结构如下:
1 2 3 4 5 flask_fileserver ├── __init__.py └── route_func ├── __init__.py └── upload.py
完成的代码rivergold放在了GitHub flask-fileserver 上,需要的小伙伴可跳转参考,这里列出主要的两个文件,分别是__init__.py
和route_func/upload.py
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import os.pathfrom flask import Flaskfrom flask_autoindex import AutoIndexfrom .route_func.upload import upload_filedef create_app (browse_root_dir=None , upload_dir=None ): app = Flask(__name__) AutoIndex(app, browse_root=browse_root_dir.as_posix()) app.config['upload_dir' ] = upload_dir app.add_url_rule('/upload' , endpoint='upload' , view_func=upload_file, methods=['GET' , 'POST' ]) return app
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 from pathlib import Pathimport flaskfrom flask import Flask, flash, request, redirect, url_forfrom werkzeug.utils import secure_filenameALLOWED_EXTENSIONS = {'txt' , 'pdf' , 'png' , 'jpg' , 'jpeg' , 'gif' , '.tar' , '.zip' } def allowed_file (filename ): return '.' in filename and \ filename.rsplit('.' , 1 )[1 ].lower() in ALLOWED_EXTENSIONS def upload_file (): if request.method == 'POST' : if 'file' not in request.files: flash('No file part' ) return redirect(request.url) file = request.files['file' ] if file.filename == '' : flash('No selected file' ) return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) print (flask.current_app.config) save_dir = Path(flask.current_app.config['upload_dir' ]) save_path = save_dir / filename file.save(save_path.as_posix()) return redirect(url_for('autoindex' )) return ''' <!doctype html> <title>Upload new File</title> <h1>Upload new File</h1> <form method=post enctype=multipart/form-data> <input type=file name=file> <input type=submit value=Upload> </form> '''
stackoverflow: python flask browsing through directory with files
Flask doc: Uploading Files
NFS远程挂载
基于 Linux 实现远程文件夹的挂载,可以把本地Linux的盘挂载到远程服务器上,从而使得二者间文件的读写更加方便。rivergold认为一个比较好的使用场景是,将本地的代码文件远程挂载到服务器上(或者是开发机上的代码文件挂载到另一个带有GPU的训练机器上),这样可以方便修改代码并进行实验。如果网络带宽够用,也可以把训练数据进行远程挂载。
NFS远程挂载配置,需要分别在Server端和Client端进行配置,Server端指的是提供盘的一方,Client端是执行挂载的一方。
Server端
安装
1 sudo apt install nfs-kernel-server
1 2 3 4 5 6 rpm -qa | grep nfs-utils rpm -qa | grep rpcbind yum -y install nfs-utils yum -y install rpcbind
修改配置
编辑/etc/exports
进行配置
1 2 3 4 /data/share_dir 192.168.1.5(rw,no_root_squash,no_all_squash,async)
关于参数的详细介绍可以参考nfs参数说明 ,写的特别详细。
启动NFS
1 2 3 sudo systemctl start rpcbind sudo systemctl start nfs
当/etc/exports
发生修改后,可以使用以下命令在不重启NFS服务的情况下使新配置的生效。
博客园: Linux 下配置 nfs 并远程挂载
如果是CentOS的话,需要开启相关端口
1 2 3 4 5 firewall-cmd --permanent --zone=public --add-port=111/tcp firewall-cmd --permanent --zone=public --add-port=111/udp firewall-cmd --permanent --zone=public --add-port=2049/tcp firewall-cmd --permanent --zone=public --add-port=2049/udp firewall-cmd --reload
StackExchange-serverfault: Which ports do I need to open in the firewall to use NFS?
Client
安装
1 sudo apt install nfs-common
1 yum -y install nfs-utils
挂载
1 sudo mount -t nfs <server_ip>:<server_folder_path> <local_mount_path>
卸载
1 2 3 sudo umount <local_mount_path> sudo umount -f -l <local_mount_path>
-f
: Force unmount (in case of an unreachable NFS system). (Requires kernel 2.1.116 or later.)
-l
: Lazy unmount. Detach the filesystem from the filesystem hierarchy now, and cleanup all references to the filesystem as soon as it is not busy anymore. (Requires kernel 2.4.11 or later.)
stackoverflow: How to unmount NFS when server is gone?
References: