Different users in linux

How to switch between users on one terminal?

I’d like to log in as a different user without logging out of the current one (on the same terminal). How do I do that?

10 Answers 10

How about using the su command?

If you want to log in as root, there’s no need to specify username:

Generally, you can use sudo to launch a new shell as the user you want; the -u flag lets you specify the username you want:

There are more circuitous ways if you don’t have sudo access, like ssh username@localhost, but sudo is probably simplest, provided that it’s installed and you have permission to use it.

/.Xauthority` file is: -rw——- . I made a copy and that let me run gedit as an experiment.

Generally you use sudo to launch a new shell as the user you want; the -u flag lets you specify the username you want:

There are more circuitous ways if you don’t have sudo access, like ssh username@localhost , but I think sudo is probably simplest if it’s installed and you have permission to use it

This command prints the current user. To change users, we will have to use this command (followed by the user’s password):

After entering the correct password, you will be logged in as the specified user (which you can check by rerunning whoami .

If you’re running Ubuntu, and if the user you want to login as doesn’t have a password set:

Enter your own password and you should be set. Of course, this requires that your user has rights to gain root privileges with sudo .

To switch the terminal session to a different user, where that user can’t exit back into the original user, use exec:

This will technically login the new user in a new term process, and close out the current one. That way when the user attempts exit or Ctrl-D, the terminal will close as though that user was the one who instantiated it, i.e., the user can’t exit back into the original user’s term. Kind of pointless, considering they can still just start a new terminal session and automatically be in the original user term login, but there it is.

EDIT: For what it’s worth, you can use linux vlock command in your

/.bashrc to lock terminal sessions by default, requiring the password of the term session user to unlock. This would somewhat prevent the aforementioned term restart under the original user context, given the term isn’t instantiated using the non-default

Источник

How to Use the su Command in Linux with Examples

Home » SysAdmin » How to Use the su Command in Linux with Examples

In Linux, the su command (switch user) is used to run a command as a different user.

In this guide, you will learn how to use the su command, with practical examples.

Читайте также:  Языковой файл для windows 10

  • A system running Linux
  • Access to a terminal window/command-line (Ctrl-Alt-T, Ctrl-Alt-F2)

How the su Command Works

The su command is used to run a function as a different user. It is the easiest way to switch or change to the administrative account in the current logged in session.

Some versions of Linux, like Ubuntu, disable the root user account by default making the system more secure. But, this also restricts the user from running specific commands.

Using su to temporarily act as a root user allows you to bypass this restriction and perform different tasks with different users.

Note: A root account is a master administrator account with full access and permissions in the system. Because of the severity of changes this account can make, and because of the risk of it being compromised, most Linux versions use limited user accounts for normal use.

su Command Syntax

To use the su command, enter it into a command-line as follows:

If a username is specified, su defaults to the superuser (root). Simply find the user you need and add it to the su command syntax.

su Command Options

To display a list of commands, enter the following:

Here are some common options to use with the su command:

  • Username – Replace usernamewith the actual username you want to log in with. This can be any user, not just root.
  • –c or –command [command] – Runs a specific command as the specified user.
  • or –l or –login [username] – Runs a login script to change to a specific username. You’ll need to enter a password for that user.
  • –s or –shell [shell] – Allows you to specify a different shell environment to run in.
  • –h or –help – Show the help file for the su command.
  • –p or ––preserve–environment – Preserve the shellenvironment (HOME, SHELL, USER, LOGNAME).

su Command Examples

Switch to a Different User

To switch the logged-in user in this terminal window, enter the following:

You’ll be asked for a password. Enter it, and the login will change to that user.

If you omit a username, it will default to the root account. Now, the logged-in user can run all system commands. This will also change the home directory and path to executable files.

Use the whoami command to verify you switched to a different user.

Note: If you are having issues with authentication, you can change the root or sudo password in a couple of simple steps.

Run Specific Command as a Different User

To run a specific command as a different user, use the –c option:

The system will respond by asking you for the user password.

When you enter this example, the system will use the specified account to run the ls (list directory contents) command.

Use a Different Shell

To use a different shell, or operating environment, enter the following:

This command opens a root user account in Z shell.

Use a Different User in the Same Environment

You can keep the environment of the current user account with the –p option:

Replace [other_user] with the actual username you want to switch to.

The user account will switch, but you’ll keep the same home directory. This is useful if you need to run a command as a different user, but you need access to the current user’s data.

To verify you remained in the same home environment, use the echo $HOME command that will display the directory you are working in.

Command Comparison: su vs sudo

sudo Command

The sudo command grants a one-time or limited-time access to root functionality. Typically, the sudo command is used to quickly run an administrative command, then return to the user account’s regular permissions.

To provide sudo access, the user has to be added to the sudo group.

Читайте также:  Модем мтс 829f подключение разорвано windows 10

Note: By default, some versions of Linux (such as Ubuntu) disable the root account. That means there’s no password assigned to the root user. However you can switch to root by running the following command and entering the currently logged-in user’s password:

su Command

The su command lets you switch the current user to any other user. If you need to run a command as a different (non-root) user, use the –l [username] option to specify the user account. Additionally, su can also be used to change to a different shell interpreter on the fly.

su is an older but more fully-featured command. It can duplicate the functionality of sudo by use of the –c option to pass a single command to the shell.

You now know how to use the su command to temporarily change users and execute commands in Linux. Use the examples provided to get started.

Источник

Clone Linux user (copy user, based on another one)

How can I create a new system user, an exact copy of another one (having the same groups, permissions, privileges and settings), but with different username, password and home directory?

5 Answers 5

This script will do it (updated as per comments):

It gets the source user’s groups (not including the group that’s the same as their login) and shell, then creates a new user with the same shell and secondary groups.

There is no error checking, it’s just a quick and dirty clone script.

  • Edit /etc/passwd and duplicate the line of the user you want an exact copy of. Modify the logon name, real name and the home directory.
  • Edit /etc/shadow and again duplicate the line of the original user. Modify the logon name.
  • Finally execute passwd newuser to modify the password.

Be aware that system wise both users are the same (same UID), so one will be able to enter the other one’s home directory and modify at will.

For Linux Mint 18 Mate

Based on Mike Anderson’s script, I made one that asks questions about the new user, the old user, the new password, and then copies the old user’s home directory and replaces all instances of the old user’s name in the new home directory with the new user’s name.

The main difference in my script regarding the useradd line is that the passwd fails in Linux Mint 18, replaced by chpasswd. To get the password to work I had the create a new line: echo $newuser:$newpassword | chpasswd.

Another difference is that I couldn’t get —create-home to work so I just used mkdir in a new line instead.

Watch out if you have a huge old user’s home directory.

Take what you need and leave the rest. You are responsible for any code you copy — make backups!

Thanks to everyone in the world who helped me with this script. John in Oregon

Источник

How to Add User to a Group in Linux

Home » SysAdmin » How to Add User to a Group in Linux

In Linux, a group is a unit in which you can manage privileges for several users simultaneously. Linux groups allow you to manage multiple user permissions quickly and easily.

In this tutorial learn how user groups work in Linux, and how to add users to specific groups.

  • A system running Linux
  • A user account with sudo or root privileges
  • Access to a terminal window/command line (Ctrl-Alt-T, Ctrl-Alt-F2)

What is a User Group in Linux

In Linux, different users have different roles or responsibilities. Some users might need the ability to execute applications, while others are restricted from accessing specific files and folders.

Groups let you create categories of users with pre-set permissions. Instead of managing permissions for each user account, you can simply add a user to a group to grant the appropriate permissions.

Читайте также:  Junction point in windows

Primary Group

The primary group is set to the logged-in user. Any files the user creates are automatically added to that group. A user can only belong to one primary group at a time. A primary group with the same name as the user is created, and any files created by the user are included in that group.

Secondary Groups

A user can belong to any number of secondary groups (including none). Secondary groups are created to manage individual files and software applications. Members of the group inherit the read, write, and execute privileges for that group.

How to Create a User Group

To create a new group, enter the following:

Replace new_group with the name you want for your new group.

How to Add User to Group

Add an Existing User to an Existing Group

Use the adduser command to add a user to a group:

Use the useradd command to add a user:

You can also use the usermod command to add a user to a group:

The usermod command uses the –append and –group options to append the user to a particular group. Without using –append , the user could be dropped from other groups.

Add a User to Multiple Groups at Once

Use the usermod command to specify multiple groups to add to:

Create a User and Add to Group

This is useful for creating a new user on the fly for a specific software application. Enter the following:

Next, assign a password to the new user:

Change a Users Primary Group

All previous commands have been used to manage the secondary groups a user belongs to. In most cases, a user’s primary group is the same as their username.

To change a users primary group, enter the command:

The lower-case –g specifies the primary group. (Upper-case –G refers to a secondary group.) A user can only have one primary group, so the old primary group user_name won’t be primary anymore for this user.

How to Remove a User From a Group

The gpasswd tool is used for managing groups. To remove a user from a group:

Note: The gpasswd tool can also be used for other administrative tasks such as defining group administrators and setting a password for access to group resources. Use the Linux man command man gpasswd for details.

Delete a Group

To delete a group, use the command:

How to List Groups in Linux

Linux comes with several different groups by default. Some of these, like the sudo group, can be used to grant permissions. Others are hidden, used for system tasks.

To view a list of groups on your system by displaying the /etc/groups file:

To display the groups that a user belongs to with the groups command:

The image above shows the groups that the logged-in user ‘sofija’ belongs to. You can display groups for a different user by specifying the username:

Another method to display the groups a user belongs to, including user ID (uid) and group ID (gid), is to use the id command:

Other Common Groups

There are a several common group names you might encounter in Linux:

  • sudo – A member of this group can use the sudo command to elevate their privileges
  • wheel – This is an older method of granting sudo-like privileges
  • cdrom – Allows the user to mount the optical drive
  • adm – Allows the user to monitor Linux system logs
  • lpadmin – Allows the user to configure printers
  • plugdev – Allows the user to access external storage devices

You should now have a good understanding of Linux groups and how to add and remove members from those groups. For more information on specific commands, you can enter the man command to display a manual in your terminal window.

Источник

Оцените статью