
sg Command in Linux
The sg command stands for "substitute group," and it enables users to run commands with the privileges of a specified group. This is akin to the newgrp command, but with some key differences. Unlike newgrp, which initiates a new shell session with the new group, sg executes the command in a subshell and then returns to the original shell session once the command completes. This makes sg a more convenient and flexible option for temporary group-based command execution.
Table of Contents
Here is a comprehensive guide to the options available with the sg command â
- Syntax of sg Command
- Key Features and Benefits of sg Command
- Examples of sg Command in Linux
- Configuration and Customization
- Relevant Files
Syntax of sg Command
The basic syntax for using the sg command is straightforward −
sg [group] [command]
Here −
- group − The name of the group you want to use.
- command − The command you wish to execute with the specified group's privileges.
Key Features and Benefits of sg Command
- Temporary Group Privileges − The sg command allows for temporary elevation of group privileges without initiating a new shell session.
- Flexibility − Since it operates in a subshell, sg provides a flexible way to execute group-specific commands and return to the original shell seamlessly.
- Efficiency − It eliminates the need for multiple steps or scripts to switch groups and run commands, enhancing overall efficiency.
Examples of sg Command on Linux
Let's explore a few practical examples of the Linux sg command −
- Listing Files with a Different Group
- Editing a File with a Different Group
Listing Files with a Different Group
Suppose you want to list files in a directory with the group privileges of the linux group. You can achieve this with the following command −
sg linux -c 'ls -l'
In this example, the ls -l command is executed with the linux group's privileges, and the detailed listing of files is displayed.

Editing a File with a Different Group
If you need to edit a file with specific group permissions, such as the linux group, you can use −
sg linux -c 'nano /path/to/file'
This command opens the file in the nano editor with the editor's group's privileges, allowing you to make necessary modifications.

Configuration and Customization
The behavior of the sg command can be influenced by configuration variables in /etc/login.defs. One such variable is SYSLOG_SG_ENAB, which enables syslog logging of sg activity. This feature is particularly useful for auditing and security purposes.
Relevant Files
Several system files play a role in the functionality of the sg command −
- /etc/passwd − Contains user account information.
- /etc/shadow − Holds secure user account information.
- /etc/group − Stores group account information.
- /etc/gshadow − Contains secure group account information.
Conclusion
The sg command is a powerful utility in Linux that allows users to execute commands with different group privileges. Its ability to run commands in a subshell and return to the original shell session makes it a flexible and efficient tool for managing group-based permissions.
Whether you need to list files, edit configurations, or perform other tasks, the sg command simplifies the process by temporarily elevating your group privileges without the need for additional steps.
By understanding and utilizing the sg command, you can enhance your workflow and effectively manage group permissions in your Linux environment.