Manjaro Difference between revisions of "Linux Security"

Difference between revisions of "Linux Security"

From Manjaro
imported>Fhdk
m (add template Box...)
 
(39 intermediate revisions by 6 users not shown)
Line 1: Line 1:
== Users ==
<languages/>
Linux is a system build for networked multi user environments where access control is a vital part of the infrastructure.
__TOC__


As such any Linux based system requires users to be identified by username and access credentials. A more thorough explanation of users and groups is available at the [[Users_%26_Groups|Users & Groups]] page.
<translate>
=Overview= <!--T:1-->


The toplevel user is '''root''' and is the most important user to protect from abuse or malicious usage.
<!--T:2-->
System security is a complicated topic that individuals study for many years.  It would be impractical to impart even a fraction of that knowledge in a Wiki article.  What this page will attempt to do is provide a primer in the most basic elements of Linux security and identify common pitfalls for beginners


If your '''root''' user is compromised you might as well restore your system either through reinstall or restore from an uncompromized backup.


A '''superuser''' account is equal problematic if not protected.
=Users= <!--T:3-->


== Groups ==
<!--T:4-->
User accounts are used to log into the system and provide one of the basic building blocks for permissions.  You could loosely categorize users into a few categories:
* Regular user accounts like the one created for you during install.
* Accounts used to run specific processes.  These users are often named after the service they run.  For example the {{ic|dbus}} users is user to run the master dbus process.
* The {{ic|root}} account.


Users on a Linux system is commonly arranged in groups. A user group is a convenient way of assigning a user access to a common task like sound, media, printing and mounting of removable drives etc.


A list of available groups can be seen on the system by opening a terminal and executing the command below.
<!--T:5-->
The root account is an administrator or superuser account.  This account to everything in the system and be used with extreme care.  In most cases, it shouldn't be used at all.  Instead use {{ic|sudo}}.


$ cat /etc/group


Likewise a user can see which groups the user is part of by executing a <code>gropus</code> which will display the users groups.
=sudo= <!--T:6-->


  $ groups
<!--T:7-->
The command {{ic|sudo}} lets you run a command as the root user without actually switching to the root user. In many cases this is safer than using the root user directly as only a single command is being run as root.  For example, your normal user account would not be able the file {{ic|/etc/fstab}} because it is owned by root.  However, you can edit it with sudo like this:
sudo nano /etc/fstab


== Passwords ==


The most common credential is the password.  
<!--T:8-->
When you run this command, you will be asked for a password, this will be the password of your normal user account.<br />
For more information about editing configuration files owned as root see [[Viewing_and_editing_configuration_files|this article on configuration files]].


The best practice for creating passwords is


* Use a combination of '''upper''- and '''lowercase letters''' mixed with '''digits''' and special characters like '''!#%&'''.
==sudo vs su== <!--T:9-->
* Do not use words which can be found in a dictionary. Dictionary password attacks is a most effective way of breaking passwords.
* Ensure your password have a reasonable length of at least 8 characters and preferable more.


You might think it is easier for you to have the same password for your superuser and root account since you are the only one using the system. You are in charge but be advised that you might weaken your security since you will not know when your system in response to an action to be performed is asking for your superuser pass or your root pass.
<!--T:10-->
While {{ic|sudo}} and {{ic|su}} look similar and both involve root access they are very different.  {{ic|sudo}} runs a single command as another user and requests the password of your normal user account.  {{ic|su}} lets you *become* root and requests the password of the root user. In general, it is usually safer to use sudo than to use su.


== Administrative tasks ==
<!--T:11-->
{{BoxWarning|Warning|Never run a graphical program as root or with sudo, it should only be used with command line programs}}


Common administrative tasks like installing software, setting up printers, modifying configurations will often require the use of an administrative role.


On a Manjaro system the first user is created upon installation an that user will by default get assigned to the administrative role of the computer. That implies that the user is able to execute commands prefixed with <code>sudo</code> and upon supplying the users password the tasks can be executed.
==Why am I Asked for a Password== <!--T:12-->


It is a common perception that ''root = su = superuser'' and that will be correct for most situations.
<!--T:13-->
Sometimes you will take an action in the terminal or through a GUI application and will get prompted for your password.  This is because the action you are trying to take cannot be completed by you user and requires elevated rights.  Whenever you get a password prompt like this it is important to pause and think if the action you are taking *should* be asking for elevated rights before entering your password.


However situations exist where the system will ask for your root password and not your superuser password and you have no way of knowing which one is asked for.
<!--T:14-->
{{BoxInfo|Note|Usually these password prompts will be looking for the password of your normal user account but occasionally they will need the password of the root account}}


You will at times find yourself in a situation where you are absolutely sure you are inputting the correct password and the system refuses to comply and thereby drive you crazy.


If you make a habit of having different passwords for superuser and root you will just have to switch to the other and the system will comply.
=Changing Passwords= <!--T:15-->


<!--T:16-->
To change the password of the user account you are logged in as you can use the command:
passwd


[[Category:Contents Page]]
 
<!--T:17-->
To change the password of a different user on the same system you can use sudo:
sudo passwd USERNAME
 
 
=Groups= <!--T:18-->
 
<!--T:19-->
Users on a Linux system are commonly arranged in groups. A user group is a convenient way of assigning more users access to a common task like sound, media, printing and mounting of removable drives etc.
 
 
<!--T:20-->
A list of the current groups can be seen on the system with the command:
getent group | awk -F : '{print $1}'
 
 
<!--T:21-->
To see which groups a given user belongs to use the command
groups USERNAME
 
 
==Primary Groups== <!--T:22-->
 
<!--T:23-->
A user can be a member of any number of groups but they have only one primary group.  The primary group is the group used when files are created.
 
 
=File Permissions= <!--T:24-->
 
<!--T:25-->
At the most basic level, files are designated as '''r'''ead, '''w'''rite or e'''x'''ecute to the '''u'''ser(owner), the '''g'''roup and '''o'''ther.  To understand how this works let's look at a real world example.
 
<!--T:26-->
To get the permissions on the file we can use the command {{ic|ls -l}}.
ls -l /etc/fstab
-rw-r--r-- 1 root root 539 Dec 26 23:07 /etc/fstab
 
 
<!--T:27-->
That first group of letters and dashes indicate the permissions.  It is 10 characters long and the dashes indicate a lack of permissions.
 
<!--T:28-->
* The first character "-", represents the file type, "-" indicates that it is a normal files.
* The next three characters "rw-" indicate the permissions for the user or owner of the file.  In this case reading and writing are allowed but not executing.
* The next three characters "rw-" indicate the permissions for members of the group who owns the file.  In this case reading and writing are allowed but not executing.
* The next three characters "r--" indicate the permissions for other users.  In this case reading is allowed but not writing or executing.
 
 
<!--T:29-->
From more detailed information on how file permissions are broken down take a look at [https://en.wikipedia.org/wiki/File_system_permissions#Traditional_Unix_permissions this Wikipedia article]
 
 
==Changing File Permissions== <!--T:30-->
 
<!--T:31-->
The command {{ic|chmod}} can be used to change permissions on a file or directory.  It is probably easier to demonstrate than explain.
 
 
<!--T:32-->
Add read rights to the user(owner) of the file
chmod u+r filename
 
 
<!--T:33-->
Remove execute rights to members of the group owner of filename
chmod g-x filename
 
 
<!--T:34-->
Set the rights for the other group to read only
chmod o=r filename
 
 
<!--T:35-->
Of course, in normal use you would combine everything like this:
chmod u+rw,g=r,o-rwx filename
 
 
<!--T:36-->
This adds read and write to the owner, set the group as read only and remove read, write and execute from other users
 
 
<!--T:37-->
The chmod command can do a lot more than that.  For more information take a look at [https://en.wikipedia.org/wiki/Chmod Wikipedia's chmod reference]
 
 
=Firewalls= <!--T:38-->
 
<!--T:39-->
The [[Firewalls]] article has a full description of the Firewall solutions available on Manjaro.
 
 
=File Integrity Monitoring= <!--T:40-->
 
<!--T:41-->
Your first line of defense should always be security practices that prevent an intrusion such as firewalls, intrusion prevention systems and keeping your system patched and up-to-date.  However, it is also useful to try to ensure that your system has not been compromised.  One way to help with this is by using a file integrity monitoring solution.  These solutions work by comparing the checksums or the files on your system to their previous versions and alerting about changes.
 
<!--T:42-->
An open source tool which provides this service is [https://aide.github.io/ AIDE](Advanced Intrusion Detection Environment.  You can install it with the command:
 
<!--T:43-->
pamac install aide
 
 
 
=Sandboxing= <!--T:45-->
 
<!--T:46-->
A '''Sandbox''' is a security mechanism for separating running programs, usually in an effort to mitigate system failures or software vulnerabilities from spreading.
 
 
<!--T:47-->
One method of sandboxing is using Firejail.  Please the [[Firejail|Firejail Wiki page]] for more information on installing and configuring Firejail.
</translate>
 
[[Category:Contents Page{{#translation:}}]]

Latest revision as of 10:05, 18 January 2023

Other languages:
English • ‎Türkçe • ‎русский

Overview

System security is a complicated topic that individuals study for many years. It would be impractical to impart even a fraction of that knowledge in a Wiki article. What this page will attempt to do is provide a primer in the most basic elements of Linux security and identify common pitfalls for beginners


Users

User accounts are used to log into the system and provide one of the basic building blocks for permissions. You could loosely categorize users into a few categories:

  • Regular user accounts like the one created for you during install.
  • Accounts used to run specific processes. These users are often named after the service they run. For example the dbus users is user to run the master dbus process.
  • The root account.


The root account is an administrator or superuser account. This account to everything in the system and be used with extreme care. In most cases, it shouldn't be used at all. Instead use sudo.


sudo

The command sudo lets you run a command as the root user without actually switching to the root user. In many cases this is safer than using the root user directly as only a single command is being run as root. For example, your normal user account would not be able the file /etc/fstab because it is owned by root. However, you can edit it with sudo like this:

sudo nano /etc/fstab


When you run this command, you will be asked for a password, this will be the password of your normal user account.
For more information about editing configuration files owned as root see this article on configuration files.


sudo vs su

While sudo and su look similar and both involve root access they are very different. sudo runs a single command as another user and requests the password of your normal user account. su lets you *become* root and requests the password of the root user. In general, it is usually safer to use sudo than to use su.


Warning
Never run a graphical program as root or with sudo, it should only be used with command line programs


Why am I Asked for a Password

Sometimes you will take an action in the terminal or through a GUI application and will get prompted for your password. This is because the action you are trying to take cannot be completed by you user and requires elevated rights. Whenever you get a password prompt like this it is important to pause and think if the action you are taking *should* be asking for elevated rights before entering your password.


Note
Usually these password prompts will be looking for the password of your normal user account but occasionally they will need the password of the root account


Changing Passwords

To change the password of the user account you are logged in as you can use the command:

passwd


To change the password of a different user on the same system you can use sudo:

sudo passwd USERNAME


Groups

Users on a Linux system are commonly arranged in groups. A user group is a convenient way of assigning more users access to a common task like sound, media, printing and mounting of removable drives etc.


A list of the current groups can be seen on the system with the command:

getent group | awk -F : '{print $1}'


To see which groups a given user belongs to use the command

groups USERNAME


Primary Groups

A user can be a member of any number of groups but they have only one primary group. The primary group is the group used when files are created.


File Permissions

At the most basic level, files are designated as read, write or execute to the user(owner), the group and other. To understand how this works let's look at a real world example.

To get the permissions on the file we can use the command ls -l.

ls -l /etc/fstab
-rw-r--r-- 1 root root 539 Dec 26 23:07 /etc/fstab


That first group of letters and dashes indicate the permissions. It is 10 characters long and the dashes indicate a lack of permissions.

  • The first character "-", represents the file type, "-" indicates that it is a normal files.
  • The next three characters "rw-" indicate the permissions for the user or owner of the file. In this case reading and writing are allowed but not executing.
  • The next three characters "rw-" indicate the permissions for members of the group who owns the file. In this case reading and writing are allowed but not executing.
  • The next three characters "r--" indicate the permissions for other users. In this case reading is allowed but not writing or executing.


From more detailed information on how file permissions are broken down take a look at this Wikipedia article


Changing File Permissions

The command chmod can be used to change permissions on a file or directory. It is probably easier to demonstrate than explain.


Add read rights to the user(owner) of the file

chmod u+r filename


Remove execute rights to members of the group owner of filename

chmod g-x filename


Set the rights for the other group to read only

chmod o=r filename


Of course, in normal use you would combine everything like this:

chmod u+rw,g=r,o-rwx filename


This adds read and write to the owner, set the group as read only and remove read, write and execute from other users


The chmod command can do a lot more than that. For more information take a look at Wikipedia's chmod reference


Firewalls

The Firewalls article has a full description of the Firewall solutions available on Manjaro.


File Integrity Monitoring

Your first line of defense should always be security practices that prevent an intrusion such as firewalls, intrusion prevention systems and keeping your system patched and up-to-date. However, it is also useful to try to ensure that your system has not been compromised. One way to help with this is by using a file integrity monitoring solution. These solutions work by comparing the checksums or the files on your system to their previous versions and alerting about changes.

An open source tool which provides this service is AIDE(Advanced Intrusion Detection Environment. You can install it with the command:

pamac install aide


Sandboxing

A Sandbox is a security mechanism for separating running programs, usually in an effort to mitigate system failures or software vulnerabilities from spreading.


One method of sandboxing is using Firejail. Please the Firejail Wiki page for more information on installing and configuring Firejail.

Cookies help us deliver our services. By using our services, you agree to our use of cookies.