- UNIX / Linux List Current Logged In Users
- Linux Command To List Current Logged In Users
- How to find currently logged in users in Linux
- Using w command to list current logged in users under Unix or Linux
- Understanding w command outputs
- Display all logged in users using who command
- Getting help with the whois command
- users command
- Vieing logged in users with last command
- Linux User info gathering commands
- Linux Change or Rename User Name and UID
- Linux Change or Rename User Command Syntax
- List all users in Linux system
- How to Change or Rename Username and UID in Linux
- View current user and group membership for user named tom
- Rename and change username from tom to jerry on Linux
- A note about running process
- Rename and change primary groupname from tom to jerry
- How to change user home directory from /home/tom/ to /home/jerry
- How to change user tom UID from 5001 to 10000
- Getting help about usermod command
- Conclusion
UNIX / Linux List Current Logged In Users
H ow do I print the user names of users currently logged in to the current UNIX / Linux host / server from a command prompt?
You need to use any one of the following command line tools to list currently logged in users on Linux or Unix-like systems.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | w or who command on Linux and Unix |
Est. reading time | 5m |
The following two file keep login records on Linux and Unix-like systems:
- /var/run/utmp – Keeps and allows us to discover information about who is currently using the system. Please note that there may be more L inux and Unix users currently using the system, because not all programs use utmp logging . In other words, poorly written app, hidden programs, malware, and other bad stuff will not be useful to list logged in users.
- /var/log/wtmp – Keeps records all logins and logouts.
We simply cannot read these files using cat command/grep command/egrep command as file is in binary database format. Hence, we use the following commands to find currently logged in users in Linux and Unix-like systems.
Linux Command To List Current Logged In Users
- w command – Shows information about the users currently on the machine, and their processes.
- who command – Display information about users who are currently logged in.
- users command – See the login names of the users currently on the system, in sorted order, space separated, on a single line. It reads all information from /var/run/utmp file.
How to find currently logged in users in Linux
Open a terminal (or login into remote server using ssh command) and type the following commands.
Using w command to list current logged in users under Unix or Linux
Open the terminal application and then type the w command:
$ w
Fig.01: w command in action.
Understanding w command outputs
From Fig.01 we see the following for each user:
- USER – Linux or Unix login name.
- TTY – The tty name.
- FROM The remote host or IP address.
- @Login – Login time.
- IDEL – Idle time.
- JCPU – The JCPU time is the time used by all processes attached to the tty. However, it does not include past background jobs, but does include currently running background jobs.
- PCPU – The PCPU time is the time used by the current process, named in the “what” field.
- WHAT – The command line of that users current process.
To see info about a user named tom, enter:
$ w tom
Tell w command not print header:
$ w -h
$ w —no-header
We can also ignore current process username by passing the -u or —no-current to the w command:
$ w -u
$ w —no-current
Want to see remote hostname field? Try:
$ w -f
Show IP address instead of hostname for from field:
$ w -i
We can also old style output. In other words old outputs prints blank space for idle times less than one minute:
$ w -o
Display all logged in users using who command
The who command works on all Unix like operating systems such as macOS, *BSD, Linux and so on. The syntax is pretty simple:
# who
Here is what we see:
The who command displays the following information:
- root – The username
- pts/0 – Type of the terminal device. In this example, we see pseudoterminal pts/0 used by root user.
- 2013-03-12 15:10 – User login date and time stamp.
- (10.1.3.177) – The remote IP address from which the user logged into this server.
We can pass the -a option to who command as follows to see time of last system boot, display dead processes, system login processes, active processes spawned by init/systemd, print current runlevel, print last system clock change, show user’s message status, and list users logged in to Linux or Unix box:
# who -a
Here is output from older Linux system (pre Systemd):
Sample outputs from Systemd based Linux sysetem:
Getting help with the whois command
You can pass the following options to the who command (taken from the who command man page):
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
users command
Open a terminal or login over the ssh session and enter the following users command:
$ users
Output who is currently logged:
Vieing logged in users with last command
Want to see a listing of last logged in users? Use the last command to lookup binary database called /var/log/wtmp and displays a list of all users logged in (and out) since that file was created. For instance see history for user named ‘vivek’:
$ last vivek
Источник
Linux User info gathering commands
MONITORING USERS
User-management is always one of the basic tasks for Linux administrators , here we are going to see some basics user related “Monitoring Commands”.
1) “finger” is a command which will give full details about user properties such as name, login , shell what he is using etc.
Syntax:
#finger username
Example:
[root@localhost
]# finger root
Login : root Name: root
Directory: /root Shell: /bin/bash
On since Sat Jun 6 19:20 (EDT) on tty 1 4 hours 12 minutes idle
On since Sun Jun 7 02:18 (EDT) on pts /0 from :0.0
On since Sun Jun 7 03:48 (EDT) on pts /1 from :0.0
16 minutes 9 seconds idle
New mail received Sun Jun 7 04:02 2009 (EDT)
Unread since Sat Jun 6 22:17 2009 (EDT)
No Plan.
Here you can see so many user related information
one security information you can see is when he loged in last time and from where he loged in etc.
2)“id” is one more command which will show the user details such as his primary group and his secondary group.
Syntax:
#id username
Example :
[root@localhost
]# id root
uid =0(root) gid =0(root) groups=0(root),1(bin),2(daemon),3( sys ),4( adm ),6(disk),10(wheel) context=root:system_r:unconfined_t: SystemLow – SystemHigh
[root@localhost
3)“ chage “ is one more command which is used to see user related “ threshold details” such as user disable time etc.
Syntax:
# chage -l username
Example:
]# chage -l root
Last password change : Jun 06, 2009
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
[root@localhost
]#
4)“who” is one more command to see who other people logged in and from where they logged in.
Syntax:
#who
Example:
[root@localhost
]# who
root tty 1 2009-06-06 19:20
root pts /0 2009-06-07 02:18 (:0.0)
root pts /1 2009-06-07 03:48 (:0.0)
[root@localhost
5)“w” is one more command which is similar to “who” command but will give some more details.
Example:
[root@localhost
]# w
06:36:49 up 11:23, 3 users, load average: 0.54, 0.38, 0.42
USER TTY FROM LOGIN @ IDLE JCPU PCPU WHAT
root tty 1 – 19:20 4:27m 0.69s 0.69s -bash
root pts /0 :0.0 02:18 0.00s 0.16s 0.02s w
root pts /1 :0.0 03:48 31:25 0.10s 0.10s bash
[root@localhost
]#
6)“groups” is one more wonderful command which will show which user belongs to what groups, we can say it’s a subset of id command
Syntax:
#groups username
Example:
]# groups root
root : root bin daemon sys adm disk wheel
[root@localhost
]#
7)”users” is one more monitoring command to see which users loged in at present in to the system
Example :
[root@localhost
]# groups root
root : root bin daemon sys adm disk wheel
[root@localhost
]#
Please add if you know some other user related monitoring commands.
Источник
Linux Change or Rename User Name and UID
Linux Change or Rename User Command Syntax
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | Linux terminal |
Est. reading time | 5 mintues |
The syntax is as follows to rename by user name:
usermod -l login-name old-name
- We use the usermod command in Linux to rename user account. The name of the user will be changed from the old-name to login_name. Nothing else is changed. In particular, the user’s home directory name should probably be changed to reflect the new login name.
The syntax is as follows to rename by a UID (user ID):
usermod -u UID username
Where,
- The numerical value of the user’s ID (UID) . This value must be unique unless the -o option is used. The value must be non-negative. Values between 0 and 99 are typically reserved for system accounts. Any files which the user owns and which are located in the directory tree rooted at the user’s home directory will have the file user ID changed automatically. Files outside of the user’s home directory must be altered
manually.
List all users in Linux system
Type the following cat command:
cat /etc/passwd
One can use the grep command to filter out only user names:
grep -w ‘^username’ /etc/passwd
grep -w ‘^jerry’ /etc/passwd
Another option is to use the cut command:
cut -d: -f1 /etc/passwd
Sample outputs:
How to Change or Rename Username and UID in Linux
Let us see how to rename user login. First, make sure user name is not logged into the server and any other process is not running under the same user name. I also recommend that you backup any data or server files before changing user names.
View current user and group membership for user named tom
First get user identity using the id command:
id tom
Next use the grep command to grab login info about user named tom from the /etc/passwd file
grep ‘^tom:’ /etc/passwd
See group info about user named tom using the groups command:
grep ‘tom’ /etc/group
groups tom
Find home directory permissions for user named tom, run the following ls command:
ls -ld /home/tom/
Finally, see all Linux process owned by user and group named tom using the ps command:
ps aux | grep tom
ps -u tom
Fig.01: Getting info about user named ‘tom’ on a Linux based system
Rename and change username from tom to jerry on Linux
Type the usermod command as follows:
# id tom
# usermod -l jerry tom
## Verify ###
# id tom
# id jerry
# ls -ld /home/tom
A note about running process
You may see an error as follows if tom is logged in and running jobs:
You need to kill all Linux process owned by user named tom and forcefully logged them out of the system:
Rename and change primary groupname from tom to jerry
Type the usermod command as follows:
# id tom
# groupmod -n jerry tom
## Verify it ###
# id tom
# ls -ld /home/tom
Sample outputs:
Fig.02: Sample session renaming user on a Linux based server
How to change user home directory from /home/tom/ to /home/jerry
The syntax is as follows:
# usermod -d /home/jerry -m jerry
# id jerry
# ls -ld /home/jerry
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How to change user tom UID from 5001 to 10000
Type the usermod command as follows:
# id tom
# usermod -u 10000 tom
# id tom
Getting help about usermod command
You can pass the —help option to the usermod command. For instance, type the following command at the shell prompt in Linux:
usermod —help
Options | Description |
---|---|
-c OR —comment | COMMENT new value of the GECOS field |
-d OR —home | HOME_DIR new home directory for the user account |
-e OR —expiredate | EXPIRE_DATE set account expiration date to EXPIRE_DATE |
-f OR —inactive | INACTIVE set password inactive after expiration to INACTIVE |
-g OR —gid | GROUP force use GROUP as new primary group |
-G OR —groups | GROUPS new list of supplementary GROUPS |
-a OR —append | append the user to the supplemental GROUPS mentioned by the -G option without removing the user from other groups |
-h OR —help | display this help message and exit |
-l OR —login | NEW_LOGIN new value of the login name |
-L OR —lock | lock the user account |
-m OR —move-home | move contents of the home directory to the new location (use only with -d) |
-o OR —non-unique | allow using duplicate (non-unique) UID |
-p OR —password | PASSWORD use encrypted password for the new password |
-R OR —root | CHROOT_DIR directory to chroot into |
-P OR —prefix | PREFIX_DIR prefix directory where are located the /etc/* files |
-s OR —shell | SHELL new login shell for the user account |
-u OR —uid | UID new UID for the user account |
-U OR —unlock | unlock the user account |
-v OR —add-subuids | FIRST-LAST add range of subordinate uids |
-V OR —del-subuids | FIRST-LAST remove range of subordinate uids |
-w OR —add-subgids | FIRST-LAST add range of subordinate gids |
-W OR —del-subgids | FIRST-LAST remove range of subordinate gids |
-Z OR —selinux-user | SEUSER new SELinux user mapping for the user account |
Conclusion
In this tutorial, you learned how to change or rename username and UID in Linux using the usermod command. Read man pages of usermod(8) and groupmod(8) commands for more information see this page.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник