# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装基础开发工具
sudo apt install -y build-essential cmake git python3-pip
# 设置时区(根据生产环境位置调整)
sudo timedatectl set-timezone Asia/Shanghai
# 禁用不必要的服务
sudo systemctl disable bluetooth.service
sudo systemctl disable avahi-daemon.service
# 查找可用的实时内核
sudo apt search linux-image-rt
# 安装实时内核(示例)
sudo apt install -y linux-image-rt-5.15 linux-headers-rt-5.15
# 设置grub默认启动项为实时内核
sudo grub-set-default "Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-rt56-generic"
sudo update-grub
# 编辑limits.conf
sudo nano /etc/security/limits.conf
# 添加以下内容:
* - rtprio 99
* - memlock unlimited
* - nice -20
# 配置sysctl参数
sudo nano /etc/sysctl.conf
# 添加:
kernel.sched_rt_runtime_us = 950000
vm.swappiness = 10
fs.inotify.max_user_watches = 524288
# 安装open62541 (OPC UA开源实现)
sudo apt install -y libopen62541-dev
# 或从源码编译最新版本
git clone https://github.com/open62541/open62541.git
cd open62541
mkdir build && cd build
cmake -DUA_ENABLE_AMALGAMATION=ON ..
make
sudo make install
# 安装libmodbus
sudo apt install -y libmodbus-dev
# 或从源码安装
wget https://libmodbus.org/releases/libmodbus-3.1.10.tar.gz
tar -xzf libmodbus-3.1.10.tar.gz
cd libmodbus-3.1.10
./configure && make
sudo make install
# 安装IgH EtherCAT主站
wget https://gitlab.com/etherlab.org/ethercat/ethercat-stable/-/archive/stable-1.5/ethercat-stable-stable-1.5.tar.gz
tar -xzf ethercat-stable-stable-1.5.tar.gz
cd ethercat-stable-stable-1.5
./configure --prefix=/usr/local/ethercat --enable-8139too=no
make
sudo make install
# 添加到系统路径
echo 'export PATH=$PATH:/usr/local/ethercat/bin' >> ~/.bashrc
echo '/usr/local/ethercat/lib' | sudo tee /etc/ld.so.conf.d/ethercat.conf
sudo ldconfig
# 设置源
sudo apt install -y software-properties-common
sudo add-apt-repository universe
sudo apt update && sudo apt install -y curl
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
# 添加ROS2源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
# 安装ROS2
sudo apt update
sudo apt install -y ros-humble-desktop ros-humble-ros2-control ros-humble-ros2-controllers ros-humble-moveit
# 安装Gazebo
sudo apt install -y gazebo libgazebo-dev
# 安装Webots
wget https://github.com/cyberbotics/webots/releases/download/R2023b/webots_2023b_amd64.deb
sudo apt install ./webots_2023b_amd64.deb
# 安装OpenCV
sudo apt install -y libopencv-dev python3-opencv
# 安装Halcon(需商业许可)
# 下载后运行安装程序
./halcon-20.11-linux.tar.gz
# 安装CODESYS(需下载安装包)
wget https://store.codesys.com/codesys/CODESYS%20Development%20System%20V3.5.17.0.deb
sudo dpkg -i CODESYS*.deb
sudo apt --fix-broken install
# 安装时序数据库(用于生产数据记录)
sudo apt install -y influxdb
sudo systemctl enable influxdb
sudo systemctl start influxdb
# 安装MySQL/MariaDB
sudo apt install -y mariadb-server
sudo mysql_secure_installation
# 禁用IPv6(如工业网络不需要)
sudo nano /etc/sysctl.conf
# 添加:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
# 应用配置
sudo sysctl -p
# 设置静态IP(根据网络环境调整)
sudo nano /etc/netplan/01-netcfg.yaml
# 示例配置:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
# 安装基础安全工具
sudo apt install -y fail2ban ufw
# 配置防火墙
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 4840/tcp # OPC UA
sudo ufw allow 502/tcp # Modbus
sudo ufw enable
# 配置SSH安全
sudo nano /etc/ssh/sshd_config
# 修改以下参数:
PermitRootLogin no
PasswordAuthentication no
AllowUsers your_username
# 基础系统监控
sudo apt install -y htop iotop iftop nmon
# 工业专用监控
sudo apt install -y cockpit cockpit-machines cockpit-docker
sudo systemctl enable --now cockpit.socket
# 安装logrotate
sudo apt install -y logrotate
# 为工业应用创建专用日志配置
sudo nano /etc/logrotate.d/industrial_app
# 示例配置:
/var/log/industrial/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
# 安装Docker
sudo apt install -y docker.io docker-compose
sudo systemctl enable --now docker
sudo usermod -aG docker $USER
# 安装NVIDIA容器工具(如需GPU加速)
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
&& curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -fsSL https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt update
sudo apt install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
# 安装cyclictest
sudo apt install -y rt-tests
# 运行实时性测试(在另一个终端运行压力测试)
cyclictest -t1 -p80 -n -i 10000 -l 10000
# 压力测试
sudo apt install -y stress-ng
stress-ng --cpu 4 --io 2 --vm 1 --vm-bytes 1G --timeout 60s
# 安装网络测试工具
sudo apt install -y iperf3 ethtool
# 测试本地网络延迟
ping -c 10 192.168.1.1
# 测试带宽
# 在一台机器运行服务器:
iperf3 -s
# 在另一台机器运行客户端:
iperf3 -c server_ip -t 20
# 检查当前运行的内核
uname -a
# 检查内核配置
zcat /proc/config.gz | grep PREEMPT_RT
# 检查实时性指标
cat /proc/sys/kernel/sched_rt_runtime_us
# 检查EtherCAT模块加载
lsmod | grep ec
# 查看EtherCAT主站状态
ethercat master
# 查看从站状态
ethercat slaves
# 检查ROS环境变量
printenv | grep ROS
# 测试ROS通信
ros2 topic list
ros2 topic echo /topic_name
通过以上配置,您的Linux系统将能够为工业机器人和自动化生产开发提供强大的支持环境。根据具体项目需求,可能还需要安装特定的硬件驱动或专有软件。