Ubuntu General Commands
General Commands (Most Used)
-
set user password
- passwd (options) username
- Will prompt for old password
-
Disable root user
- sudo passwd -l root (-l is small L) [root cannot login with password]
- usermod --expiredate 1 [account is disabled]
- sudo usermod —e 1 root
-
Enable root
- sudo passwd root
- (give it a password and it will be enabled)
-
Give current user sudo privledges
sudo -i
-
List all groups http://ubuntuforums.org/show/thread.php?t=1583028
cat /etc/group
-
List all users
cat /etc/passwd
-
list only directories
ls -d
-
SUDO
- *When you run a command, you are acting as root. Therefore directories created with sudo will belong to root.
-
su
- http://namhuy.net/44/add-delete-and-switch-user-in-ubuntu-
by-command-lines.html - http://manpages.ubuntu.com/manpages/precise/man1/su.1.html
-
change to the root user:
- su root
- (will prompt for root pass)
- http://namhuy.net/44/add-delete-and-switch-user-in-ubuntu-
-
show server version
cat /etc/issue
-
Change a files owner
sudo chown -R www-data:www-data some_directory/
Managing Users and Permissions
-
show users logged in and history
lastlog
-
Change user in terminal
- su [name of user]
-
Add users (Also see "Ubuntu Server Setup General Steps"
-
useradd vs adduser
-
useradd is a command, it will just add user
- ** useradd WILL NOT create /home/user directory unless you use a flag, see below.
- adduser is a script that will prompt for pass and other info. it WILL create a /home/user directory
-
useradd is a command, it will just add user
- https://help.ubuntu.com/commun
ity/AddUsersHowto - https://help.ubuntu.com/8.04/s
erverguide/C/user-management.h tml -
useradd (add user command line)
- -d set home directory
- -m force useradd to create the home directory
- -p set password
-
-G add to group
useradd -G groupname username
-
Example useradd: add the user, sajeev34, to the directory = domain.com group = webusertest1
- sudo useradd johndoe -d /var/www/public_html/domain.c
om/ -G webusertest1 - sudo useradd johndoe -d /var/www/sandbox.domain.com/ -
G www-data - you must still assign a password using passwd
- See ACL to assign access
- sudo useradd johndoe -d /var/www/public_html/domain.c
- adduser (prompt for details)
-
add a user for web (apache) access example:
useradd -d /var/www/public_html/domain.co
m/public -G www-data -p password USERNAME -
add user (already exists) to apache group
usermod -G www-data username
-
Delete user
- deluser username
- -remove-all-files remove all files owned by this user
- -remove-home remove the user's home directory
- deluser username
-
useradd vs adduser
-
Create a symbolic link from a users home directory to another directory
sudo ln -s /var/www/public_html/domin.com
/public /home/steve/Live - creates link called “Live” inside /home/steve/ to /var/www/public_html/domin.com
/public
- creates link called “Live” inside /home/steve/ to /var/www/public_html/domin.com
- http://unix.stackexchange.com/
questions/84175/create-a-symbo lic-link-relative-to-the-curre nt-directory
http://askubuntu.com/questions/214643/help-with-creating- symbolic-link
-
set user password
- passwd (options) username
- Will prompt for old password
- passwd (options) username
-
Add/Delete a user to a group
-
See the all of the users in a group
- cat /etc/group OR getent group groupname
-
Example, add a user to the www-data group
- sudo usermod -a -G GroupName UserName
- a - add to group (use with G)
- G - group name
- sudo usermod -a -G www-data jsmith
- sudo usermod -a -G GroupName UserName
-
Delete a user from a group
deluser <username> <groupname>
-
See the all of the users in a group
-
Give a user root permissions. Disable the root user
- *** Ideally add user to admin(or sudo) group which has sudo privileges.
-
If user doesn’t exist
- Create user with adduser. (above)
- Set the user password (above)
- Add user to /etc/sudoers
username ALL=(ALL:ALL) ALL
login as this user and Disable Root user
sudo passwd -l root (-l is small L)
-
Change document root to apache: change owner of directory
- sudo chgrp newgroup nameOFfile
- sudo chgrp -R www-data /directory/directory
-
change files owner and group
- sudo chown fileowner:filegroup nameOFfile
- sudo chown www-data:www-data qlaunch1
- change recursively (all sub files and folders)
- sudo chown -R www-data:www-data some_directory/
-
create a group
groupadd NameOFGroup
-
list groups
cat /etc/group
-
list members of a group
getent group www-data
-
Add a user to a group
- sudo usermod -a -G groupName www-data
- where groupName is the name of the group and www-data is the user
- -a is append
- -G group name
- sudo usermod -a -G groupName www-data
-
Change permissions of directory https://help.ubuntu.com/commu
nity/FilePermissions -
chmod
- -R change files and directories (all files in the directories)
-
change mod of files only in a directory
sudo find /path/to/someDirectory -type f -print0 | xargs -0 sudo chmod 644
-
change mod of directories only
sudo find /path/to/someDirectory -type d -print0 | xargs -0 sudo chmod 755
-
chmod
-
Set a folder so all files are owned by apache, no matter who edits them:
- http://askubuntu.com/questions
/196062/sftp-permission- denied-on-files-owned-by-www- data sudo adduser $USER www-data
sudo chgrp -R www-data /var/www
sudo chmod -R g+rw /var/www
sudo chmod -R g+s /var/www
- http://askubuntu.com/questions
Copying and Deleting, Managing Files
-
copy file
cp sourceFile targetFile
-
copy a directory
- cp -r source destination
-r or -R is recursive (include all sub folders and files) cp -r dir1 dir2
cp -option source destination
cp -option1 -option2 source destination
- cp -r source destination
-
copy all files from one directory to another
-
mv -vn olddirectory/* /newdirectory/newdirectory
- check man for attributes
-
Move all files up one directory:
-
mv * .[^.]* ..
- Spaces are needed.
-
mv * .[^.]* ..
-
in Mac, you must specify the entire directory path for source and target
- sudo mv /Users/userName/Sites-acquia-d
rupal/sites/whistlepunk.localh ost/whistlepunk/docroot/* .[^.]* /Users/userName/Sites-acquia-d rupal/sites/whistlepunk.localh ost/ - ** Note the use of /* .[^.]* to grab all files incl those that start with .
- sudo mv /Users/userName/Sites-acquia-d
-
mv -vn olddirectory/* /newdirectory/newdirectory
-
Move a directory
-
mv fromflderpath tofolderpath
-
- sudo mv /var/www/public_html/drupal8te
st1/public/drupal/* .[^.]* /var/www/public_html/drupal8te st1/public/
- sudo mv /var/www/public_html/drupal8te
-
-
mv fromflderpath tofolderpath
-
Rename a directory
mv /home/user/oldname /home/user/newname
-
Upload a file from the desktop to the server
- ** MAY HAVE PERMISSIONS ISSUES: Suggest upload to your home directory on the server then cp internally.
scp /Users/johnDoe/Desktop/Symfony
_Standard_Vendors_2.3.3.tgz [email protected]:/var/ww w/ scp /Users/johnDoe/Desktop/placeho
lder1.png [email protected]:/var/ www/sandbox/sites/default/file s - Run this from mac prompt
- Make sure jmahoney has write access on remote server.
-
also,
- sudo scp useraccountname@mymac:/Users/j
ohnDoe/Desktop/test.txt [email protected]:/var/w ww/sandbox/sites/default/files - useraccountname = your mac user
- mymac: you mac’s hostname.
- sudo scp useraccountname@mymac:/Users/j
-
NOT WORKING
sudo scp /Users/johnDoe/Desktop/ckedito
r-internal.css [email protected]:/var/ www/pcats-D7/sites/all/themes/ si_conexxus/css -
WORKS: (set ssh port)
scp -P 55432 /Users/johnDoe/ARC_ADMIN/Archi
ves/FileName.jpa [email protected]:/home /jrmaron325
- Examples
Copy the file "foobar.txt" from a remote host to the local host
$ scp [email protected]:foobar.txt/some/local/directory
Copy the file "foobar.txt" from the local host to a remote host
$ scp foobar.txt [email protected]:/some/remote/directory
Copy the directory "foo" from the local host to a remote host's directory "bar"
$ scp -r foo [email protected]:/some/remote/directory/bar
Copy the file "foobar.txt" from remote host "rh1.edu" to remote host "rh2.edu"
$ scp [email protected]:/some/remote/directory/foobar.txt \
[email protected]:/some/remote/directory/
Copying the files "foo.txt" and "bar.txt" from the local host to your home directory on the remote host
$ scp foo:.txt bar.txt [email protected]:~
Copy the file "foobar.txt" from the local host to a remote host using port 2264
$ scp -P 2264 foobar.txt [email protected]:/some/remote/directory
Copy multiple files from the remote host to your current directory on the local host
$ scp [email protected]:/some/remote/directory/\{a,b,c\ } .
$ scp [email protected]:~/\{foo.txt,bar.txt\} .
- Copy files from server to Mac desktop
- find a file locate
-
- ex. locate php.ini
-
-
Delete/Remove files and directory
- rm (filename)
-
remove directory and all files in that directory
- rm -r OR rm -R directoryname
- r/R => recurrsive
- -r will prompt y/n for each file UNLESS you use sudo
- rm -R directoryname
- rm -R -i directoryname
- rm -r OR rm -R directoryname
-
Remove EVERYTHING, force delete CAREFUL!
sudo rm -rf (name of directory)
-
find largest files
-
If you just need to find large files, you can use find with the -size option. The next command will list all files larger than 10MiB
- find / -size +10M -ls
- find / -size +10G -ls
-
If you want to find files between a certain size, you can combine it with a "size lower than" search. The next command find files between 10MiB and 12MiB:
- find / -size +10M -size -12M -ls
-
If you just need to find large files, you can use find with the -size option. The next command will list all files larger than 10MiB
-
find size of a directory, find largest directories
-
du / | sort -n
- last entries are largest files/directories
- du -h —max-depth=1
-
du --max-depth=7 /* | sort -n
- max depth = 7 directories
- [CentOS] du -m filename or du -h filename or du -ms foldername
-
du / | sort -n
-
get PHP version
- php-config --version
- dpkg -l | grrep php
- [CentOS] php -i
-
Compress and uncompress
-
uncompress .gz
-
sudo gunzip file.gz
-
If the file is file.tar.gz you will still need to un tar it
-
tar xvf file.gz
- x - extract
- f - use archive file
- v - verbosely
- ** This will create a folder called file and put all of the files in that folder.
-
tar xvf file.gz
-
If the file is file.tar.gz you will still need to un tar it
- http://www.cyberciti.biz/faq/h
owto-compress-expand-gz-files/ - http://askubuntu.com/questions
/25347/what-command-do-i-need- to-unzip-extract-a-tar-gz-file
-
sudo gunzip file.gz
-
Compress
-
tar czf new-tar-file-name.tar.gz fileORfolderTOarchive
- c create new archive
- z compress the archive using gzip
- f use archive file
-
tar czf new-tar-file-name.tar.gz fileORfolderTOarchive
- Uncompress .zip
-
uncompress .gz
-
Make a link to another file or directory (known as symbolic link)
- http://www.cyberciti.biz/faq/c
reating-soft-link-or-symbolic- link/ -
ln -s {target-filename} {symbolic-filename}
- For example create softlink for /webroot/home/httpd/test.com/i
ndex.php as /home/vivek/index.php, enter the following command: * -s allows link to directory. - ln -s /webroot/home/httpd/test.com/i
ndex.php /home/vivek/index.php - ls -l
- For example create softlink for /webroot/home/httpd/test.com/i
- http://www.cyberciti.biz/faq/c
Access Control/Permissions: ACL
- About: ACL is an add-on to Ubuntu that can help you manage permissions for different users. it is HIGHLY recommended that this is installed during initial setup.
-
Setup ACL
- (reference) https://help.ubunt
u.com/community/FilePermission sACLs - sudo apt-get install acl
-
make backup copy of fstab
- /etc/fstab
-
Add acl option to fstab file
- /dev/xvda1 / ext3 defaults,acl,errors=remount-ro
,noatime 0 1
- /dev/xvda1 / ext3 defaults,acl,errors=remount-ro
-
remount the partitions
- (run from root) mount -o remount /
-
verify acl is active
-
mount | grep acl
- You should see a line that includes "acl"
-
mount | grep acl
- (reference) https://help.ubunt
-
check user access
- getfacl /var/www/~~~~~
-
grant user access
- sudo setfacl -Rm u:johnDoe:rwx public/
- ** If a directory is not created by a web root user, they will lose access to it. Use this command to re-assign access.
-
grant group access
- sudo setfacl -Rdm g:webusers:rwx public/
- Allow user to change file permissions, upload download files to a directory
FIREWALL
- About: Use ufw Firewall to block general SSH access (Do not need to install, this is part of Ubuntu core, just enable). During setup, change the SSH port to something different than 22 RIGHT AWAY.
- https://help.ubuntu.com/commu
nity/UFW - If the port you want to open or close is defined in /etc/services, you can use the port name instead of the number. In the above examples, replace 22 with ssh.
- set default to allow
-
sudo ufw default allow
-
enable
- sudo ufw enable
-
disable
- sudo ufw disable
-
check status
- sudo ufw status
- sudo ufw status verbose
-
Check the rules as a dry run
-
sudo ufw --dry-run allow http
- shows what would be applied if an allow http rule were applied
-
sudo ufw --dry-run allow http
-
enable logging
- sudo ufw logging on
-
Read the log
- sudo cat /var/log/ufw.log
-
allow specific port and IP address
- sudo ufw allow from <ip address> to <protocol> port <port no>
-
ex. allow ip address 192.168.0.4 access to port 22 for all protocols
- sudo ufw allow from 192.168.0.4 to any port 22
-
default deny port 22
- sudo ufw deny 22
-
Open port 80 so the public can see your site
- sudo ufw allow 80
-
If you'll be using SSL, you need to open port 443
- sudo ufw allow 443
-
delete a rule
-
sudo ufw delete <rule>
- ex. sudo ufw delete deny 80/tcp
- ufw delete <number> where number is number of rule.
-
sudo ufw delete <rule>
-
limit the number of ssh login attempts to 6
-
sudo ufw limit ssh/tcp
- will deny connections if an IP address has attempted to initiate 6 or more connections in the last 30 seconds.
-
sudo ufw limit ssh/tcp
-
Block or Unblock an IP address that has been blocked
-
block an ip address
- sudo ufw block from xxx.xxx.xxx.xxx
-
allow from that address
- sudo ufw allow from xxx.xxx.xxx.xxx
-
block an ip address
-
enable
PERFORMANCE - MAINTENANCE - SERVER DOWN
- Are you using cloud caching? Make sure turn this off for trouble shooting.
-
Check harddrive usage
-
df -h
- If disk shows full even after deleting large file/s, reboot. This will clear processes that are still using the deleted file.
-
df -h
-
Reduce hard drive space
- See above to find large files and directories
-
Check access/error logs
- ex. /var/log/apache/access.log
- ex. /usr/local/apache/logs/suphp_l
og -
compress it then clear it
- gzip -c /usr/local/apache/logs/suphp_l
og > /usr/local/apache/logs/suphp_l og.gz - echo > /usr/local/apache/logs/suphp_l
og
- gzip -c /usr/local/apache/logs/suphp_l
-
check ram memory usage
-
free
- -m (show in megabytes)
- ** look at third line, -/+ buffers/cache
- http://www.rackspace.com/knowl
edge_center/article/memory- management-with-free
-
free
-
CPU usage
-
top
- http://www.tecmint.com/12-top-
command-examples-in-linux/ - q (quit)
- press z to display running process in color
- press c to see absolute path of process
- http://www.tecmint.com/12-top-
- top -u (username)
-
top
- trace system calls
- strace
- report current processes
- ps
- ps aux
- kill a process
- kill pidnumber
- apache Memory issue
- https://www.virtacoresupport.c
om/index.php?_m=knowledgebase& _a=viewarticle&kbarticleid=102 - http://chrisjohnston.org/tech/
configuring-a-lightweight-apac he-mysql-install-on-debian- ubuntu - http://forum.slicehost.com/ind
ex.php?p=/discussion/4824/apac he-using-too-much-ram/p1 - Retstart server
-
sudo reboot
- ** danger of corrupting db, shutdown mysql first.
- See Shut down section below!
- cat /proc/XXYY
- cat /proc/version
- cat /proc/cpuinfo
- cat /proc/meminfo
- Check 32 bit or 64 bit
- http://askubuntu.com/questions
/41332/how-do-i-check-if-i- have-a-32-bit-or-a-64-bit-os - Check for large size logs
- /var/www/public_html/domain.co
m/log/ - /var/log/
-
how to clean (to reduce size)
- compress the log files
- copy to a backup source (or just leave on server)
- delete the compressed file
-
recreate the file
- sudo nano access.log
- sudo nano error.log
- Use htop to check memory usage
-
htop
- install: sudo apt-get install htop
- Check server uptime
- uptime
-
SERVER DOWN steps
- login via SSH
- Check logs
- check services, apache, mysql
- login
- check free -m and hard drive space
-
check logs
- /www/public_html/siteName/logs
/
- /www/public_html/siteName/logs
-
Cannot connect to database:
- restart Mysql service
CONNECTION
- https://help.ubuntu.com/commun
ity/SSH/OpenSSH/Configuring -
Make a copy of your ssh config file
- sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-d
efault.12MAY11
- sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-d
- Set a banner
- ** See trouble shooting section and SSH section
-
Find host name of the machine
- hostname
APPLICATIONS/LIBRARIES
- About: Ubuntu has many many, did we say many, applications and libraries available. Research the ones you need BEFORE you setup the server.
-
search for an application
sudo aptitude search application
sudo aptitude search htop -
CURL
sudo apt-get install curl
sudo apt-get install php5-curl
>restart apache -
XSL support
sudo apt-get install php5-xsl
-
GD Library
sudo apt-get install php5-gd
-
List orphaned packages
sudo apt-get autoremove
- List install libraries
-
list all applications
aptitude search php5
-
Remove a library:
sudo aptitude purge XXYY where XXYY is the package name
-
Check php and mysql versions
php -v
mysql -V
SHUT DOWN REBOOT
-
Preferred Method:
- * If a database is being written to during shutdown it will get corrupted
- Suggest check google real-time analytics to make sure no one is using site.
-
shutdown apache (this will stop users from writing to db)
- sudo /etc/init.d/apache2 stop
-
reboot the server
sudo reboot
sudo shutdown -h now
sudo shutdown -h 0
-
sudo shutdown -h 18:45 "Server is going down for maintenance"
- Shuts it down at 6:45pm
-
HALT (server has been hacked and is out of control!)
-
sudo halt
- halt is less 'soft' than shutdown
-
sudo halt
- sudo reboot
-
sudo shutdown -r 0
- (same as sudo reboot)
Updates - Patches
-
Update repositories (always do before updating)
sudo apt-get update
-
upgrade ssl (heartbleed bug)
- sudo apt-get upgrade openssl
- check version
- openssl version -a (-a = all information)
IN A JAM(Trouble Shooting)
-
who -a
- show all logged in users
-
iptables -L INPUT -v -n
-
view blocked IP addresses
-
-
Can’t connect with SSH
- Turn off firewall
- Restart SSH
- SSH attempts returns “Connection to 50.56.xx.xx closed.”
- ssh_exchange_identification: read: Connection reset by peer
-
Sudo Broken
sudo: /etc/sudoers is mode 0470, should be 0440
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin- Rackspace) use rescue mode to fix sudoers file permissions
- http://www.rackspace.com/knowledge_center/article/rackspace-cloud-essentials-rescue-mode-on-linux-cloud-servers
http://www.rackspace.com/knowledge_center/article/rackspace-cloud-essentials-rescue-mode-on-linux-cloud-servers - change sudoers file permissions
- reinstall OpenSSH