Bash: System Commands

3 minute read

Description:

These commands have to do with system resources, user accounts, permissions, ect.

User commands

  • To change owners for files
# -R in this case is recursive
chown -R apache:apache /var/www/html/*
  • To add a group:
groupadd newGroup
  • To change group membership for files
chgrp (groupname) files

# Change the owning group of /office/files, and all subdirectories, to the group staff.
chgrp -hR staff /office/files
  • To change access permissions
    chmod (mode) files # see here for more examples
chmod -R 777 root /var/www/html

# Commonly used to run scripts you create
chmod +x /path/to/your/filename.extension

# Commonly used to secure a file or directory
chmod 644
  • To see your user id
# id (username) - view uid, guid, and groups
id gerry
  • To add a new user:
useradd newUser
  • To delete a user:
# Remove root privileges if needed.
 userdel newUsersudo visudo
  • To add your user to a group:
usermod -a -G (groupname) username

# other options

  • To see permissions for a file
getfacl
  • To list all groups
lid -g (groupname)
  • To list all users
cut -d: -f1 /etc/passwd
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd

Or this link

  • To list actively logged on users
w
  • Add user to sudo:
# If the user doesn't have rights you either need to add to /etc/sudoers file 
# Or add to wheel group: 
adduser testUser
usermod -aG wheel testUser
usermod -aG sudo testUser

System commands (to be ran as root)

  • To run a command as root
# Most common command you will probably use
sudo (command)

# Just type this command by itself to run all other command past it as root. 
# Then type "exit" to get back to your user prompt.
su
  • To install an application (depending on the package manager and distro):
# Installs a Linux application, in this case – xchat or IRC.
sudo apt-get install xchat

# For CentOS
sudo yum install xchat

# For Fedora
sudo dnf install xchat
  • To Add A New Path To Path Variable:

This order from all users to most specific
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile

PATH="$PATH:/opt/puppetlabs/bin";export PATH
  • New way (preferred)
echo 'pathmunge /opt/puppetlabs/puppet/bin' > /etc/profile.d/puppet.sh
chmod +x /etc/profile.d/puppet.sh
. /etc/profile
  • To remove software:
sudo yum remove (packageName)

# Removes a couple programs at once
sudo yum remove orca gedit evince empathy 

# Remove all packages that don't have dependencies:
# Shows them
sudo package-cleanup --leaves 
# Removes them
sudo yum remove `package-cleanup --leaves`
  • To change a password for a user
# If left blank, changes current users password
passwd (username)
passwd root
  • To change the date
date (options)

# Change the year but keep the same time
date -s "2014-12-25 $(date +%H:%M:%S)"
  • To Shutdown
shutdown -h now

# Shuts down at 12:30
shutdown -h 12:30
  • To logout
logout
  • To Reboot
reboot
shutdown -r

Interesting article between shutdown, halt, and poweroff

  • To start a service
sudo systemctl start httpd
  • To see a service status
sudo systemctl status httpd
  • To set a service to run on startup
sudo systemctl enable httpd

# This enables the ssh service to start on runtime
update-rc.d ssh enable
  • To restart a service
sudo systemctl restart httpd
  • To have a GUI for services:
# Use spacebar to enable whichever you want. You can also run “sysv-rc-conf”. These are called TGUI programs.

# For Debian based: 
sudo rcconf

# For SysV Services (RPM based): 
sudo ntsysv

# For SystemD Services (default): 
systemctl list-unit-files | less | grep enabled

# For almost any Linux distro: 
chkconfig --list
  • To open a port on your firewall
# Check which zones are active
firewall-cmd get-active-zones

# Add the port
firewall-cmd permanent zone=public add-port=3389/tcp

# Reload the firewall
firewall-cmd reload
  • To close a port on your firewall
# Check which zones are active
firewall-cmd get-active-zones

# Remove the port
firewall-cmd permanent zone=public remove-port=3389/tcp

# Reload the firewall
firewall-cmd reload
  • To View Credentials to Shares/ Passwords if you are using Nautilus.
seahorse

Misc

  • To see a history of your commands
history
  • To clear the screen
clear
  • To see a calendar of a previous year
cal 2010