安全配置远程连接服务器


How to set a certain port in Ubuntu so that only specified mac addresses can be accessed. Use pycharm or vscode to connect remotely and limit access to the mac address on the client.


To set up a specific port on an Ubuntu system so that only specified MAC addresses can access it, you’ll need to employ a combination of firewall rules and network configuration. Here’s a step-by-step guide to achieve this:

  1. Install and Configure Firewall (UFW or iptables):

    • Ubuntu usually comes with UFW (Uncomplicated Firewall) which is a user-friendly interface for managing iptables rules. If not installed, you can install it using sudo apt-get install ufw.
  2. Allow Specific MAC Addresses:

    • Unfortunately, UFW does not directly support filtering by MAC address. You’ll need to use iptables for MAC address filtering.

    • Here’s an example iptables command to allow a specific MAC address on a specific port:

      sudo iptables -A INPUT -p tcp --dport [PORT] -m mac --mac-source [MAC_ADDRESS] -j ACCEPT
      
    • Replace [PORT] with the port number you want to secure and [MAC_ADDRESS] with the allowed MAC address. Repeat this command for each MAC address you want to allow.

  3. Deny All Other Access:

    • After specifying the allowed MAC addresses, ensure that you set up a rule to deny all other access to the port:

      sudo iptables -A INPUT -p tcp --dport [PORT] -j DROP
      
  4. Save the iptables Rules:

    • The rules added to iptables are not persistent by default. You can make them persistent across reboots by installing iptables-persistent:

      sudo apt-get install iptables-persistent
      
    • During installation, it will ask if you want to save current rules, choose ‘Yes’.

  5. Connect Remotely Using PyCharm or VSCode:

    • To connect remotely using PyCharm or VSCode, you will need to set up SSH on your Ubuntu server if not already done.
    • Ensure the client device from which you are connecting has the MAC address that is allowed in your iptables rules.
    • In PyCharm or VSCode, use the SSH configuration to connect to your Ubuntu server, specifying the host, port, username, and authentication details.
  6. Testing the Configuration:

    • After setting everything up, test your configuration by trying to access the specified port from a device with an allowed MAC address and then from a device with a non-allowed MAC address. The former should succeed, and the latter should fail.

Please note that MAC addresses can be spoofed, so relying solely on MAC address filtering for security is not recommended. It should be part of a layered security approach. Additionally, MAC address filtering is most effective in a controlled environment, such as a local network, and is not usually applicable for internet-facing services.


You:

so relying solely on MAC address filtering for security is not recommended. , how to solve that


To enhance the security of your Ubuntu server beyond just relying on MAC address filtering, you can implement a multi-layered security approach. Here are some additional measures you can take:

  1. Use Strong Authentication Methods:

    • Implement SSH key-based authentication instead of password-based authentication for SSH access. This is more secure as it requires someone to have the private key corresponding to the public key on the server.
    • For web applications, ensure strong password policies and consider multi-factor authentication (MFA).
  2. Network Level Security:

    • Use a Virtual Private Network (VPN) for accessing your server. This adds an extra layer of security as it encrypts the traffic between your client and the server.
    • Implement a Virtual Local Area Network (VLAN) to segregate network traffic and restrict access to certain parts of your network.
  3. Firewall Configuration:

    • Besides MAC address filtering, configure your firewall to allow only necessary ports and services.
    • Use stateful packet inspection (SPI) to monitor the state of active connections and determine which network packets to allow through the firewall.
  4. Regular Updates and Patches:

    • Regularly update your operating system and all installed applications to ensure all security patches are applied.
  5. Intrusion Detection and Prevention Systems:

    • Implement an Intrusion Detection System (IDS) or an Intrusion Prevention System (IPS) to monitor network activities for malicious activities or policy violations.
  6. Endpoint Protection:

    • Use antivirus and anti-malware solutions to protect against various types of malware.
  7. Limit User Access and Privileges:

    • Implement the principle of least privilege (PoLP) by providing users with the minimum levels of access—or permissions—they need to perform their work.
  8. Monitoring and Logging:

    • Set up system and network monitoring to detect and alert on suspicious activities.
    • Ensure that logs are maintained and reviewed regularly for signs of attempted or successful unauthorized accesses.
  9. Secure Application Deployment:

    • If you are running web applications, ensure they are securely coded. Follow best practices like input validation, output encoding, and use of prepared SQL statements to prevent common vulnerabilities like SQL injection, Cross-Site Scripting (XSS), etc.
  10. Backup and Recovery:

    • Regularly backup critical data and ensure that you have a recovery plan in case of a breach or data loss.

Implementing these measures will significantly increase the security of your server and reduce the risk of unauthorized access. Remember, no single security measure is foolproof, but a combination of practices greatly enhances overall security.


You:

how to realize it ,mplement SSH key-based authentication instead of password-based authentication for SSH access.


Implementing SSH key-based authentication instead of password-based authentication involves a few steps. Here’s a guide to set it up on an Ubuntu server:

On the Client Side

  1. Generate SSH Key Pair:

    • If you don’t already have an SSH key pair (which consists of a public key and a private key), generate one using the ssh-keygen command.
    • Open a terminal and run: ssh-keygen -t rsa -b 4096
    • You’ll be asked to enter a file path to save the key, and an optional passphrase for extra security.
  2. Copy the Public Key to the Server:

    • Use the ssh-copy-id command to copy your public key to the Ubuntu server.
    • Run: ssh-copy-id username@your_server_ip
    • Replace username with your actual username on the server and your_server_ip with the server’s IP address.
    • You’ll be prompted to enter the user’s password on the server.

On the Server Side

  1. Disable Password Authentication:

    • Once your key is copied, log in to your server: ssh username@your_server_ip
    • Open the SSH configuration file: sudo nano /etc/ssh/sshd_config
    • Find the line that says #PasswordAuthentication yes and change it to PasswordAuthentication no
    • If there is a line saying #PermitRootLogin yes, change it to PermitRootLogin no to disable root login.
    • Save the file and exit the editor.
  2. Restart SSH Service:

    • Apply the changes by restarting the SSH service: sudo systemctl restart sshd

Testing

  1. Test the SSH Key Authentication:
    • Log out from your server: exit
    • Try logging back in: ssh username@your_server_ip
    • If you set up everything correctly, you should log in without needing a password (if you set a passphrase for your key, you’ll need to enter that).

Important Notes

  • Backup Your Keys: Make sure to backup your private key and remember the passphrase if you set one. If you lose access to your private key, you won’t be able to log in to the server.
  • Security: Keep your private key secure and never share it.
  • Multiple Users: If you have multiple users, each user should have their own key pair.
  • Public Key Location: The public key is usually stored in ~/.ssh/authorized_keys on the server. Ensure correct permissions are set (read-only for the user).
  • Root Access: If you need to perform actions as root, use sudo instead of logging in as the root user.

Implementing SSH key-based authentication significantly enhances the security of your SSH access by relying on cryptographic keys rather than just passwords.

zh part

如何在Ubuntu中设置某个端口,使得只有指定的mac地址可以访问。使用pycharm或vscode进行远程连接,并限制对客户端mac地址的访问。

To set up a specific port on an Ubuntu system so that only specified MAC addresses can access it, you’ll need to employ a combination of firewall rules and network configuration. Here’s a step-by-step guide to achieve this:要在 Ubuntu 系统上设置特定端口,以便只有指定的 MAC 地址可以访问它,您需要结合使用防火墙规则和网络配置。以下是实现此目标的分步指南:

  1. Install and Configure Firewall (UFW or iptables):安装和配置防火墙(UFW 或 iptables):

    • Ubuntu usually comes with UFW (Uncomplicated Firewall) which is a user-friendly interface for managing iptables rules. If not installed, you can install it using sudo apt-get install ufw.Ubuntu 通常附带 UFW (简单防火墙),这是一个用于管理 iptables 规则的用户友好界面。如果未安装,您可以使用 sudo apt-get install ufw 安装它。
  2. Allow Specific MAC Addresses:允许特定 MAC 地址:

    • Unfortunately, UFW does not directly support filtering by MAC address. You’ll need to use iptables for MAC address filtering.不幸的是,UFW不直接支持MAC地址过滤。您需要使用 iptables 进行 MAC 地址过滤。

    • Here’s an example

      iptables
      

      command to allow a specific MAC address on a specific port:

      下面是一个示例 iptables 命令,用于允许特定端口上的特定 MAC 地址:

      cssCopy code
      sudo iptables -A INPUT -p tcp --dport [PORT] -m mac --mac-source [MAC_ADDRESS] -j ACCEPT
      
    • Replace [PORT] with the port number you want to secure and [MAC_ADDRESS] with the allowed MAC address. Repeat this command for each MAC address you want to allow.将 [PORT] 替换为您要保护的端口号,将 [MAC_ADDRESS] 替换为允许的 MAC 地址。对您想要允许的每个 MAC 地址重复此命令。

  3. Deny All Other Access:拒绝所有其他访问:

    • After specifying the allowed MAC addresses, ensure that you set up a rule to deny all other access to the port:

      指定允许的 MAC 地址后,请确保设置规则以拒绝所有其他对该端口的访问:

      cssCopy code
      sudo iptables -A INPUT -p tcp --dport [PORT] -j DROP
      
  4. Save the iptables Rules:保存 iptables 规则:

    • The rules added to

      iptables
      

      are not persistent by default. You can make them persistent across reboots by installing

      iptables-persistent
      

      :

      默认情况下,添加到 iptables 的规则不是持久的。您可以通过安装 iptables-persistent 使它们在重新启动后保持不变:

      arduinoCopy code
      sudo apt-get install iptables-persistent
      
    • During installation, it will ask if you want to save current rules, choose ‘Yes’.安装过程中会询问是否保存当前规则,选择“是”。

  5. Connect Remotely Using PyCharm or VSCode:使用 PyCharm 或 VSCode 远程连接:

    • To connect remotely using PyCharm or VSCode, you will need to set up SSH on your Ubuntu server if not already done.要使用 PyCharm 或 VSCode 进行远程连接,您需要在 Ubuntu 服务器上设置 SSH(如果尚未完成)。
    • Ensure the client device from which you are connecting has the MAC address that is allowed in your iptables rules.确保您要连接的客户端设备具有 iptables 规则中允许的 MAC 地址。
    • In PyCharm or VSCode, use the SSH configuration to connect to your Ubuntu server, specifying the host, port, username, and authentication details.在 PyCharm 或 VSCode 中,使用 SSH 配置连接到 Ubuntu 服务器,指定主机、端口、用户名和身份验证详细信息。
  6. Testing the Configuration:测试配置:

    • After setting everything up, test your configuration by trying to access the specified port from a device with an allowed MAC address and then from a device with a non-allowed MAC address. The former should succeed, and the latter should fail.设置完所有内容后,通过尝试从具有允许的 MAC 地址的设备访问指定端口,然后从具有不允许的 MAC 地址的设备访问指定端口来测试您的配置。前者应该成功,后者应该失败。

Please note that MAC addresses can be spoofed, so relying solely on MAC address filtering for security is not recommended. It should be part of a layered security approach. Additionally, MAC address filtering is most effective in a controlled environment, such as a local network, and is not usually applicable for internet-facing services.请注意,MAC 地址可能会被欺骗,因此不建议仅依靠 MAC 地址过滤来确保安全。它应该是分层安全方法的一部分。此外,MAC 地址过滤在受控环境(例如本地网络)中最有效,并且通常不适用于面向互联网的服务。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

You

so relying solely on MAC address filtering for security is not recommended. , how to solve that因此,不建议仅依靠 MAC 地址过滤来确保安全。 ,怎么解决

To enhance the security of your Ubuntu server beyond just relying on MAC address filtering, you can implement a multi-layered security approach. Here are some additional measures you can take:为了增强 Ubuntu 服务器的安全性,而不仅仅是依赖 MAC 地址过滤,您可以实施多层安全方法。您可以采取以下一些额外措施:

  1. Use Strong Authentication Methods:使用强身份验证方法:
    • Implement SSH key-based authentication instead of password-based authentication for SSH access. This is more secure as it requires someone to have the private key corresponding to the public key on the server.对 SSH 访问实施基于 SSH 密钥的身份验证,而不是基于密码的身份验证。这更安全,因为它要求某人拥有与服务器上的公钥相对应的私钥。
    • For web applications, ensure strong password policies and consider multi-factor authentication (MFA).对于 Web 应用程序,请确保强大的密码策略并考虑多重身份验证 (MFA)。
  2. Network Level Security:网络级安全:
    • Use a Virtual Private Network (VPN) for accessing your server. This adds an extra layer of security as it encrypts the traffic between your client and the server.使用虚拟专用网络 (VPN) 访问您的服务器。这增加了额外的安全层,因为它加密了客户端和服务器之间的流量。
    • Implement a Virtual Local Area Network (VLAN) to segregate network traffic and restrict access to certain parts of your network.实施虚拟局域网 (VLAN) 以隔离网络流量并限制对网络某些部分的访问。
  3. Firewall Configuration:防火墙配置:
    • Besides MAC address filtering, configure your firewall to allow only necessary ports and services.除了 MAC 地址过滤之外,还应将防火墙配置为仅允许必要的端口和服务。
    • Use stateful packet inspection (SPI) to monitor the state of active connections and determine which network packets to allow through the firewall.使用状态数据包检测 (SPI) 监视活动连接的状态并确定允许哪些网络数据包通过防火墙。
  4. Regular Updates and Patches:定期更新和补丁:
    • Regularly update your operating system and all installed applications to ensure all security patches are applied.定期更新您的操作系统和所有已安装的应用程序,以确保应用所有安全补丁。
  5. Intrusion Detection and Prevention Systems:入侵检测和预防系统:
    • Implement an Intrusion Detection System (IDS) or an Intrusion Prevention System (IPS) to monitor network activities for malicious activities or policy violations.实施入侵检测系统 (IDS) 或入侵防御系统 (IPS) 来监控网络活动是否存在恶意活动或违反策略的行为。
  6. Endpoint Protection:端点保护:
    • Use antivirus and anti-malware solutions to protect against various types of malware.使用防病毒和反恶意软件解决方案来防御各种类型的恶意软件。
  7. Limit User Access and Privileges:限制用户访问和权限:
    • Implement the principle of least privilege (PoLP) by providing users with the minimum levels of access—or permissions—they need to perform their work.通过为用户提供执行工作所需的最低级别的访问或权限来实施最小特权 (PoLP) 原则。
  8. Monitoring and Logging:监控和记录:
    • Set up system and network monitoring to detect and alert on suspicious activities.设置系统和网络监控以检测可疑活动并发出警报。
    • Ensure that logs are maintained and reviewed regularly for signs of attempted or successful unauthorized accesses.确保定期维护和审查日志,以查找尝试或成功的未经授权访问的迹象。
  9. Secure Application Deployment:安全应用程序部署:
    • If you are running web applications, ensure they are securely coded. Follow best practices like input validation, output encoding, and use of prepared SQL statements to prevent common vulnerabilities like SQL injection, Cross-Site Scripting (XSS), etc.如果您正在运行 Web 应用程序,请确保它们的编码是安全的。遵循输入验证、输出编码和使用准备好的 SQL 语句等最佳实践,以防止 SQL 注入、跨站脚本 (XSS) 等常见漏洞。
  10. Backup and Recovery:备份与恢复:
    • Regularly backup critical data and ensure that you have a recovery plan in case of a breach or data loss.定期备份关键数据并确保在发生泄露或数据丢失时制定恢复计划。

Implementing these measures will significantly increase the security of your server and reduce the risk of unauthorized access. Remember, no single security measure is foolproof, but a combination of practices greatly enhances overall security.实施这些措施将显着提高服务器的安全性并降低未经授权访问的风险。请记住,没有任何一种安全措施是万无一失的,但实践的组合可以大大增强整体安全性。

实施基于 SSH 密钥的身份验证而不是基于密码的身份验证涉及几个步骤。以下是在 Ubuntu 服务器上设置它的指南:

On the Client Side在客户端

  1. Generate SSH Key Pair:生成 SSH 密钥对:
    • If you don’t already have an SSH key pair (which consists of a public key and a private key), generate one using the ssh-keygen command.如果您还没有 SSH 密钥对(由公钥和私钥组成),请使用 ssh-keygen 命令生成一个。
    • Open a terminal and run: ssh-keygen -t rsa -b 4096打开终端并运行: ssh-keygen -t rsa -b 4096
    • You’ll be asked to enter a file path to save the key, and an optional passphrase for extra security.系统会要求您输入保存密钥的文件路径以及可选的密码以提高安全性。
  2. Copy the Public Key to the Server:将公钥复制到服务器:
    • Use the ssh-copy-id command to copy your public key to the Ubuntu server.使用 ssh-copy-id 命令将公钥复制到 Ubuntu 服务器。
    • Run: ssh-copy-id username@your_server_ip运行: ssh-copy-id username@your_server_ip
    • Replace username with your actual username on the server and your_server_ip with the server’s IP address.将 username 替换为您在服务器上的实际用户名,将 your_server_ip 替换为服务器的 IP 地址。
    • You’ll be prompted to enter the user’s password on the server.系统将提示您输入服务器上的用户密码。

On the Server Side在服务器端

  1. Disable Password Authentication:禁用密码验证:
    • Once your key is copied, log in to your server: ssh username@your_server_ip复制密钥后,登录到您的服务器: ssh username@your_server_ip
    • Open the SSH configuration file: sudo nano /etc/ssh/sshd_config打开SSH配置文件: sudo nano /etc/ssh/sshd_config
    • Find the line that says #PasswordAuthentication yes and change it to PasswordAuthentication no找到 #PasswordAuthentication yes 行并将其更改为 PasswordAuthentication no
    • If there is a line saying #PermitRootLogin yes, change it to PermitRootLogin no to disable root login.如果有一行显示 #PermitRootLogin yes ,请将其更改为 PermitRootLogin no 以禁用 root 登录。
    • Save the file and exit the editor.保存文件并退出编辑器。
  2. Restart SSH Service:重新启动 SSH 服务:
    • Apply the changes by restarting the SSH service: sudo systemctl restart sshd通过重新启动 SSH 服务来应用更改: sudo systemctl restart sshd

Testing测试

  1. Test the SSH Key Authentication

    :

    测试 SSH 密钥身份验证:

    • Log out from your server: exit从您的服务器注销: exit
    • Try logging back in: ssh username@your_server_ip尝试重新登录: ssh username@your_server_ip
    • If you set up everything correctly, you should log in without needing a password (if you set a passphrase for your key, you’ll need to enter that).如果一切设置正确,您应该无需密码即可登录(如果您为密钥设置了密码,则需要输入该密码)。

Important Notes重要笔记

  • Backup Your Keys: Make sure to backup your private key and remember the passphrase if you set one. If you lose access to your private key, you won’t be able to log in to the server.备份您的密钥:请务必备份您的私钥并记住密码(如果您设置了密码)。如果您无法访问私钥,您将无法登录服务器。
  • Security: Keep your private key secure and never share it.安全:确保您的私钥安全,切勿共享。
  • Multiple Users: If you have multiple users, each user should have their own key pair.多个用户:如果您有多个用户,每个用户都应该有自己的密钥对。
  • Public Key Location: The public key is usually stored in ~/.ssh/authorized_keys on the server. Ensure correct permissions are set (read-only for the user).公钥位置:公钥通常存储在服务器上的 ~/.ssh/authorized_keys 中。确保设置正确的权限(对用户只读)。
  • Root Access: If you need to perform actions as root, use sudo instead of logging in as the root user.Root 访问:如果您需要以 root 身份执行操作,请使用 sudo 而不是以 root 用户身份登录。

Implementing SSH key-based authentication significantly enhances the security of your SSH access by relying on cryptographic keys rather than just passwords.实施基于 SSH 密钥的身份验证可通过依赖加密密钥而不仅仅是密码来显着增强 SSH 访问的安全性。

猜你喜欢

转载自blog.csdn.net/chumingqian/article/details/134770258