The most effective virus prevention happens before infection, not after

A virus spreads because you run infected code on your machine. The best defense is to avoid running that code in the first place. This means keeping your operating system and software patched, running antivirus software that scans files before they execute, avoiding suspicious downloads and email attachments, and using a firewall. Most infections happen through email, malicious websites, or unpatched software vulnerabilities — all of which you can defend against with concrete steps.

Prevention is far cheaper and faster than recovery. A single infection can lock your files, steal your passwords, or turn your machine into a tool for attacking other computers. The steps below take minutes to set up and run mostly on their own after that.

Key Takeaways

  • Enable automatic updates for Windows, macOS, or Linux so security patches install without you having to remember.
  • Install antivirus software that scans files in real time — Windows Defender (built into Windows 10 and 11) is free and effective for most users.
  • Turn on your firewall, which blocks unauthorized incoming connections; it comes built into Windows, macOS, and most Linux distributions.
  • Never open email attachments or click links from senders you do not recognize, and be skeptical of urgent requests even from people you know.
  • read software only from the publisher's official website or a trusted app store, never from random read sites.

Keep your operating system and software patched

Viruses often exploit known security holes in Windows, macOS, or Linux. Once a hole is discovered, the publisher releases a patch — a small update that closes it. If you do not install the patch, you leave the door open.

Set your system to install updates automatically. On Windows 10 and 11, go to Settings > Update & Security > Windows Update and turn on "Automatic (recommended)". On macOS, go to System Settings > General > Software Update and check "Automatically keep my Mac up to date". On Linux, the process varies by distribution, but most modern systems offer automatic security updates in their settings or package manager.

The same applies to the software you use. Chrome, Firefox, Adobe Reader, Java, and other programs all release patches regularly. Most modern applications update themselves automatically, but check your software's settings to confirm. If a program asks you to update and you keep dismissing it, you are leaving yourself exposed.

Install and run antivirus software

Antivirus software scans files on your computer and blocks those that match known virus signatures or behave like malware. It runs in the background and checks files before they execute, which stops most infections before they start.

Windows 10 and 11 come with Windows Defender built in, and it is free and effective for most users. If you use Windows, check that Defender is turned on: go to Settings > Privacy & Security > Windows Security > Virus & threat protection. If you prefer a third-party option, Bitdefender, Norton, and Kaspersky all offer free versions with real-time scanning.

macOS users should enable the built-in XProtect, which runs automatically and scans files downloaded from the internet. You can also install third-party antivirus software like Bitdefender or Kaspersky if you want additional protection. Linux users have fewer viruses to worry about, but ClamAV is a free, open-source antivirus available for most distributions.

Do not rely on antivirus alone. It catches known threats, but new viruses appear constantly. The other steps in this guide — patching, avoiding suspicious files, and using a firewall — are just as important.

Enable your firewall

A firewall blocks unauthorized incoming connections to your computer. It does not stop viruses you read and run yourself, but it prevents attackers from reaching your machine over the network and exploiting vulnerabilities remotely.

Windows Firewall is built into Windows and turned on by default. To check it, go to Settings > Privacy & Security > Windows Security > Firewall & network protection. macOS has a built-in firewall; go to System Settings > Network > Firewall and turn it on. Most Linux distributions include a firewall (usually iptables or firewalld) that you can manage through your system settings or the command line.

If you use a router at home, it has its own firewall that protects your entire network. That is a good first layer, but your computer's firewall adds a second layer of protection.

Be cautious with email attachments and downloads

Email is the most common delivery method for viruses. An infected attachment looks like a normal file — a Word document, a PDF, a spreadsheet — but runs malicious code when you open it. The safest rule is straightforward: never open an attachment from someone you do not know, and be skeptical of attachments from people you do know if the message seems out of character.

Legitimate companies do not ask you to enable macros in a Word document or run an executable file attached to an email. If your bank, your employer, or a service you use sends you an attachment asking you to do either, it is almost certainly a phishing attack. Go to the company's website directly (do not click a link in the email) and log in to check your account.

The same caution applies to downloads. read software only from the publisher's official website or a trusted app store like the Microsoft Store, Apple App Store, or your Linux distribution's package manager. Websites that offer "free" versions of paid software or bundles of tools are common sources of malware. If a read site shows ads or asks you to disable your antivirus before downloading, leave when ready.

Recognize phishing and social engineering

Many viruses arrive because someone tricks you into running them. A phishing email might claim your bank account is locked and ask you to click a link to verify your password. A fake tech support popup might tell you your computer is infected and offer to clean it for you. Both are designed to make you act quickly without thinking.

Slow down. Legitimate companies do not ask for passwords via email or popup. If you receive an urgent message from a company you use, go to their official website directly (type the address yourself, do not click a link) and log in to check your account. If there is a real problem, you will see it there. If you see a popup claiming your computer is infected, close it by pressing Alt+F4 or force-quitting your browser — do not click anything in the popup itself.

Be especially skeptical of messages that create urgency or fear. "Your account will be closed in 24 hours", "Your computer has a virus", "Click here when ready" — these are red flags. Real security issues do not work on a 24-hour important date, and legitimate companies give you time to respond.

Use strong passwords and two-factor authentication

A virus that steals your passwords can do far more damage than one that just locks your files. Protect your accounts by using a unique, strong password for each one — at least 12 characters, mixing uppercase and lowercase letters, numbers, and symbols. A password manager like Bitwarden, 1Password, or KeePass generates and stores these for you so you do not have to remember them.

Enable two-factor authentication (2FA) on important accounts like email, banking, and social media. 2FA requires a second form of verification — usually a code from an app like Google Authenticator or Authy, or a text message — even if someone has your password. This stops an attacker from accessing your account even if a virus steals your login credentials.

Back up your files regularly

Prevention is the goal, but backups are your safety net. If a virus does infect your machine, a recent backup lets you restore your files without paying a ransom or losing your work. Back up to an external hard drive or a cloud service like OneDrive, Google Drive, or Backblaze. Set it to run automatically so you do not have to remember.

Keep at least one backup disconnected from your computer. If you back up only to a cloud service and a virus encrypts your files, the virus may also encrypt your cloud backup. An external drive that you connect once a week or once a month stays safe because the virus cannot reach it while it is unplugged.

Frequently Asked Questions

Do I need antivirus software if I use macOS or Linux?

macOS has built-in protections (XProtect and Gatekeeper) that catch most threats, so a third-party antivirus is optional for most users. Linux has far fewer viruses because it is less common as a target, but you can install ClamAV if you want extra protection. Windows users should run antivirus software because Windows is the most targeted operating system.

What should I do if I think my computer is infected?

Disconnect from the internet, run a full scan with your antivirus software, and let it remove any threats it finds. If your antivirus does not find anything but your computer still behaves strangely, restart in Safe Mode (or Recovery Mode on macOS) and scan again. If the problem persists, back up your important files to an external drive and consider reinstalling your operating system from official installation media.

Is it safe to use public WiFi if I have antivirus software?

Antivirus software protects against malware, but public WiFi exposes your passwords and data to anyone on the network. Use a VPN (virtual private network) like ProtonVPN, Mullvad, or NordVPN when you connect to public WiFi. A VPN encrypts your traffic so others cannot see what you are doing, even if they are on the same network.

Can I get a virus from visiting a website?

Yes, if the website hosts malicious code or exploits a vulnerability in your browser or plugins. This is called a drive-by read. Keep your browser and operating system patched, and consider disabling plugins like Flash (which is no longer supported anyway). Modern browsers like Chrome and Firefox are designed to isolate malicious code so it cannot spread to your whole system.

What is the difference between a virus, malware, and ransomware?

A virus is code that replicates itself and spreads to other files or computers. Malware is any malicious software, including viruses, spyware, and trojans. Ransomware is a type of malware that encrypts your files and demands payment to unlock them. The prevention steps in this guide protect against all three.